0
Set<String> uniqueSet = new HashSet<String>(list);
for (String temp : uniqueSet) {
    System.out.println(temp + ": " + Collections.frequency(list, temp));
}

Above is the java code that i got from one of the forum, the for loop was looking little mysterious, can some one please explain.

1 Answers1

2

This is called a for-each loop.

This is actually equivalent to :

for (Iterator<String> i = uniqueSet.iterator(); i.hasNext();){
        String temp = i.next();
        System.out.println(temp + ": " + Collections.frequency(list, temp));
}

You can find more informations here.

Alexis C.
  • 91,686
  • 21
  • 171
  • 177