collection set

How many collection set in Java Program?

Three types of collection set in java programs.

  • LinkedHashSet collection set
  • HashSet
  • TreeSet

Generally these three work same in java collection programming. But in this blog, we will see basic different between collection set.

LinkedHashSet works same as added data in Set.

HashSet works as randomly with data.

TreeSet works ascending order with data.

 

Let’s see simple example on collection set.

 

import java.util.*;

class collection8

{

                public static void main(String args[])

                {

                                LinkedHashSet<Double> set1 = new LinkedHashSet<Double>();

                                set1.add(new Double(56.78));

                                set1.add(new Double(23.67));

                                set1.add(new Double(89.56));

                                set1.add(new Double(76.23));

                                set1.add(new Double(34.56));

                                System.out.println(set1);

 

                                HashSet<Double> set2 = new HashSet<Double>();

                                set2.add(new Double(56.78));

                                set2.add(new Double(23.67));

                                set2.add(new Double(89.56));

                                set2.add(new Double(76.23));

                                set2.add(new Double(34.56));

                                System.out.println(set2);

 

                                TreeSet<Double> set3 = new TreeSet<Double>();

                                set3.add(new Double(56.78));

                                set3.add(new Double(23.67));

                                set3.add(new Double(89.56));

                                set3.add(new Double(76.23));

                                set3.add(new Double(34.56));

                                System.out.println(set3);

                }

}

Output

[56.78, 23.67, 89.56, 76.23, 34.56]

[56.78, 23.67, 89.56, 34.56, 76.23]

[23.67, 34.56, 56.78, 76.23, 89.56]

 

Visit this program in YouTube Channel

 

For more videos on Java visit our YouTube Channel

 

Earn Money Online

485total visits,1visits today


mayank

Hello, I am Maddy a Java blogger, trainer and developer

Related articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Your comments (*)

Name (*)

Your full name please.

Email address (*)

Used for gravatar.

Website

Link back if you want.

Enter Captcha Here :*

Reload Image