-2

In the below code, what is the meaning of the following line?

m.put(alpha, l=new ArrayList<String>());

Code (for finding Anagrams):

try {
    Scanner s = new Scanner(new File(args[0]));
    while (s.hasNext()) {
        String word = s.next();
        String alpha = alphabetize(word);
        List<String> l = m.get(alpha);
        if (l == null)
            m.put(alpha, l=new ArrayList<String>());
        l.add(word);
    }
} catch (IOException e) {
    System.err.println(e);
    System.exit(1);
}
Erwin Bolwidt
  • 30,799
  • 15
  • 56
  • 79
hmharsh3
  • 335
  • 3
  • 11
  • 1
    depends on what `m` is. If you're confused by assignment as function argument, please keep in mind that in Java assignment is an operator, just like `+`. Its result is the value being assigned, plus it has a side effect of changing the value of the variable. –  Aug 01 '16 at 15:21
  • Your full code needs to be here as a [mcve]. But looks like you have a Hashmap. What's not to understand about that? – OneCricketeer Aug 01 '16 at 15:21
  • In Java 8, that would be `m.computeIfAbsent(alpha, l -> new ArrayList()).add(word);` – 4castle Aug 01 '16 at 15:28

1 Answers1

3

The part

m.put(alpha, l=new ArrayList<String>());

could also be written as

l=new ArrayList<String>();
m.put(alpha, l);

An assignment returns the assigned value, which is why your code is working.

f1sh
  • 11,489
  • 3
  • 25
  • 51
  • ok thanks for this but i want to know what l will contain , value of l is empty array? – hmharsh3 Aug 01 '16 at 15:26
  • 1
    @user3386952 Yes, `l` will contain what is currently in the map, and if it's not in the map, an empty `ArrayList` will be added and `l` will reference it. – 4castle Aug 01 '16 at 15:36