Online Training Spring Hibernate Training in Delhi R Programming Training Course Big Data and Hadoop Training Java Training  




Share on Google+Share on Google+

Janardhan yadav
2 Answer(s)      6 years and 4 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
collections  in collections can we perform binarysearch on arraylist elements without sorting them
collections  why we need collections.when we will use collections in our applications
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
Version of commons-collections>commons-collections dependency
List of Version of commons-collections>commons-collections dependency
Version of excalibur-collections>excalibur-collections dependency
List of Version of excalibur-collections>excalibur-collections dependency
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 it that if we change the i2 value,it doesnt remove. import java.util.*; public class Mapit { public static void main
Version of commons-collections>commons-collections-testframework dependency
List of Version of commons-collections>commons-collections-testframework dependency