3

Can anyone please explain this code to me, I don't have much coding experience with Collections so I am having difficulties in understanding these LOC.

String[] stringList ={"1","2","1","1","2","3","2","3","2","1"}; List<String> al =Arrays.asList(stringList); Set<String> uniqueList = new HashSet<String>(al); for (String strCount :uniqueList) { System.out.println(strCount + ": " + Collections.frequency(al, strCount)); } 

Why does this loop only run 3 times while uniquelist has all the members of stringList. Shouldn't the loop run 10 times (length of uniqueList)?

6
  • You can check the size of set uniqueList by invoking the size() method on it. Commented Oct 12, 2012 at 8:31
  • 2
    May I suggest naming your variables a little better? For example, you are naming a set as uniqueList. A better variable name would convey that it is a set. Commented Oct 12, 2012 at 8:34
  • Thank you Atul but now this post already got too many anwers , so if I change the name now , users will get confused if anyone seeks help from this page in future. Commented Oct 12, 2012 at 8:40
  • 1
    @Sobia I think Atul meant that you should change the variable names in your code, not here in the question. Commented Oct 12, 2012 at 8:41
  • yes, I meant it as a general practice... not in this question as such. :) Commented Oct 12, 2012 at 9:01

5 Answers 5

12

The for loop only executes three times, because there are only 3 distinct values in your stringList.

A Set does not allow duplicate entries. The Set checks entries with .equals(), which will be true for two duplicate entries. Consequently, there are only three elements in uniqueList, namely: "1", "2" and "3".

Note, that "2".equals("2") is true.

Here is the most important part of the javadoc:

A collection that contains no duplicate elements. More formally, sets contain no pair of elements e1 and e2 such that e1.equals(e2), and at most one null element.

Sign up to request clarification or add additional context in comments.

Comments

7

A Set is a Collection that contains no duplicate element. Since your array contains only 1, 2 and 3 as unique elements, your Set will contain only 3 elements.

Check this documentation link.

Comments

4
Set<String> uniqueList = new HashSet<String>(al); 

As Set does not allow duplicates, hence after executing this statement the uniqueList contains the elements "1", "2" and "3".

Comments

4

why this loop runs only 3 times while "uniquelist" has all the members of "StringList"

No it doesn't.

If you go with general Mathematical definition of a Set, a Set is collection of unique values. They can't have duplicate.

Same follows in Java also (In fact anywhere), so when you convert your List to a Set, all the Duplicate values are filtered out. Only unique values are retained.

Comments

4

according to java spec:

add method in HashSet implementation:

public boolean add(E e)

Adds the specified element to this set if it is not already present. More formally, adds the specified element e to this set if this set contains no element e2 such that (e==null ? e2==null : e.equals(e2)). If this set already contains the element, the call leaves the set unchanged and returns false.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.