Janardhan yadav
2 Answer(s)      5 years and 11 months ago
Posted in : Java Beginners
 import java.util.*;
    public class Mapit {
        public static void main(String[] args) {
            Set<Integer> set = new HashSet<Integer>();
            Integer i1 = 45;
            Integer i2 = 46;

            System.out.print(set.size() + " ");
            System.out.print(set.size() + " ");

             i2 = 47;


            System.out.print(set.size() + " ");

Output : 2 1 1

Plz explain the flow of code ( i2 = 47; set.remove(i2); System.out.print(set.size() + " ");)


View Answers

April 13, 2011 at 3:23 PM

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.

June 2, 2011 at 3:57 PM

Integer is wrapper class. By default wrapper classes are immutable. thats why we cant give another value here.

Related Tutorials/Questions & Answers:
Collections  Hi, Please send a study material on collections.../java/jdk6/introduction-collections-api.shtml http://www.roseindia.net/javacodeexamples/index.shtml http://www.devmanuals.com/tutorials/java/collections/index.html
collections  why we need collections.when we will use collections in our applications
collections  in collections can we perform binarysearch on arraylist elements without sorting them
what is collections?
what is collections?  what is collections
Collections API
Collections API  hello, What is the Collections API?   hi The Collections API is a set of classes and interfaces that support operations on collections of objects
Collections in Java
Collections in Java  What is Concurrent Hash map
Collections in Java
Collections in Java  What is Concurrent Hash map
java collections
java collections  what are all the methods and things added for collections in java5 and java6 please kindly reply me as soon as possible
Collections Framework
Collections Framework  Sir, We know that both HashMap & Hashtable is using for same purposes i.e Used for storing keys-values pair. But there is some difference between this two class that are 1)Hashtable is synchronized
collections in java
collections in java  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
java collections
achieve this plz without using stringtokenizer with any collections!!!  
Hibernate Criteria With Collections Example
Hibernate Criteria With Collections Example  Good code of Hibernate Criteria With Collections Example. Thanks   Example program of using Hibernate Criteria With Collections in your Java project. Check the tutorial
collections - Java Interview Questions
collections   what is the way to insert map values into a set in collections