Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Collections HashSet

820459Dec 22 2010 — edited Dec 22 2010
public class Test {
public static void main(String[] args) {
Set<Integer> set = new HashSet<Integer>();
Integer i1 = 45;
Integer i2 = 46;
set.add(i1);
set.add(i1);
set.add(i2); System.out.print(set.size() + " ");
set.remove(i1); System.out.print(set.size() + " ");
i2 = 47;
set.remove(i2); System.out.print(set.size() + " ");

}
}

The output for this program is 2 1 1. When I remove i2=47; from the code, the output is 2 1 0. I understand that this line does the trick but excuse me for I am not good at collections.

Can anyone explain.

Edited by: 817456 on Dec 22, 2010 2:23 AM

Edited by: 817456 on Dec 22, 2010 2:24 AM
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jan 19 2011
Added on Dec 22 2010
7 comments
187 views