The ArrayList, Vector, Stack, and LinkedList stores the data in First Come First Server Order (also known as Insertion order) also they do supports duplicate objects
The LinkedHashSet also stores objects in Insertion order, but does not allow duplicate objects
The HashSet stores objects in any order, means No specific order, and it does not allow duplicate objects
The TreeSet stores objects in Sorted order (means in their natural order), and does not allow duplicate objects
The PriorityQueue stores objects on the basis of their priorities (using a MIN HEAP DATA STRUCTURES)
Note: Save this code in SomeCollections.java file
// import collection API
// the Collection API is inside java.util package
import java.util.*;
public class SomeCollections
{
public static void main(String[] args)
{
// create an fruits of string
String[] fruits = {"dates","cherry","banana","mango","apple","fig"};
// create few collections to store string objects
LinkedList<String> c1 = new LinkedList<String>();
HashSet<String> c2 = new HashSet<String>();
LinkedHashSet<String> c3 = new LinkedHashSet<String>();
TreeSet<String> c4 = new TreeSet<String>();
PriorityQueue<String> c5 = new PriorityQueue<String>();
// fetch data from string fruits using for loop
for (int i = 0; i < fruits.length; i++)
{
// store the data of i'th element of fruits
// inside the collection
c1.add(fruits[i]);
c2.add(fruits[i]);
c3.add(fruits[i]);
c4.add(fruits[i]);
c5.add(fruits[i]);
}
// show data of LinkedList
System.out.println("-- LinkedList --");
System.out.println(c1);
System.out.println();
// show data of HashSet
System.out.println("-- HashSet --");
System.out.println(c2);
System.out.println();
// show data of LinkedHashSet
System.out.println("-- LinkedHashSet --");
System.out.println(c3);
System.out.println();
// show data of TreeSet
System.out.println("-- TreeSet --");
System.out.println(c4);
System.out.println();
// show data of PriorityQueue
System.out.println("-- PriorityQueue --");
System.out.println(c5);
}
}
Output of the code ==>
-- LinkedList --
[dates, cherry, banana, mango, apple, fig]
-- HashSet --
[banana, cherry, apple, fig, dates, mango]
-- LinkedHashSet --
[dates, cherry, banana, mango, apple, fig]
-- TreeSet --
[apple, banana, cherry, dates, fig, mango]
-- PriorityQueue --
[apple, banana, cherry, mango, dates, fig]
The LinkedHashSet also stores objects in Insertion order, but does not allow duplicate objects
The HashSet stores objects in any order, means No specific order, and it does not allow duplicate objects
The TreeSet stores objects in Sorted order (means in their natural order), and does not allow duplicate objects
The PriorityQueue stores objects on the basis of their priorities (using a MIN HEAP DATA STRUCTURES)
Note: Save this code in SomeCollections.java file
// import collection API
// the Collection API is inside java.util package
import java.util.*;
public class SomeCollections
{
public static void main(String[] args)
{
// create an fruits of string
String[] fruits = {"dates","cherry","banana","mango","apple","fig"};
// create few collections to store string objects
LinkedList<String> c1 = new LinkedList<String>();
HashSet<String> c2 = new HashSet<String>();
LinkedHashSet<String> c3 = new LinkedHashSet<String>();
TreeSet<String> c4 = new TreeSet<String>();
PriorityQueue<String> c5 = new PriorityQueue<String>();
// fetch data from string fruits using for loop
for (int i = 0; i < fruits.length; i++)
{
// store the data of i'th element of fruits
// inside the collection
c1.add(fruits[i]);
c2.add(fruits[i]);
c3.add(fruits[i]);
c4.add(fruits[i]);
c5.add(fruits[i]);
}
// show data of LinkedList
System.out.println("-- LinkedList --");
System.out.println(c1);
System.out.println();
// show data of HashSet
System.out.println("-- HashSet --");
System.out.println(c2);
System.out.println();
// show data of LinkedHashSet
System.out.println("-- LinkedHashSet --");
System.out.println(c3);
System.out.println();
// show data of TreeSet
System.out.println("-- TreeSet --");
System.out.println(c4);
System.out.println();
// show data of PriorityQueue
System.out.println("-- PriorityQueue --");
System.out.println(c5);
}
}
Output of the code ==>
-- LinkedList --
[dates, cherry, banana, mango, apple, fig]
-- HashSet --
[banana, cherry, apple, fig, dates, mango]
-- LinkedHashSet --
[dates, cherry, banana, mango, apple, fig]
-- TreeSet --
[apple, banana, cherry, dates, fig, mango]
-- PriorityQueue --
[apple, banana, cherry, mango, dates, fig]
Wonderfull fact Any website or Tutorial point doesnt share that information about collection .Its a secret of collection.Thank you Cute Hasan Sir
ReplyDeleteeasy to understand ...
ReplyDeletethank u sir
Nicely explained within one example in a very easy way.. thanks a lot..
ReplyDelete