
please explain me the flow of this program..i m getting o/p as 2 1 1..y not 2 1 0..is it that if we change the i2 value,it doesnt remove.
import java.util.*; public class Mapit { 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() + " "); } }

You have created an instance of HashSet of Integer type and declared two values of Integer. You have then stored the values in the hashset. HashSet does not store duplicate values so it stores the value 45 and 46 once with size 2. Therefore it displays the size 2 firstly.
Then you have used remove method to remove the value of i1. It is removed and displays the size 1 secondly.
After that you have defined another value for i2 and want to remove i2. But this time, it will not remove the value of i2 as you cannot assign two values for the same object.
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.