Set in Java

Set in Java

Set in Java

Set in Java. In this Article I will Explain what is Set Java by stepping through the sample code below.

What is Set

Set can be explains as an Interface which extends a group of element collection.Basically there are 3 Set Type

  • HashSet -> This Command does not maintain the order of the element being stored.
  • LinkedHashSet -> This Command maintain the order of the element being stored.
  • TreeSet -> This Command sort the Element according to Alphabetical order or numbering order.

Class hashSet

  1. First i created a class call hashSet.
  2. The next thing i do is to import  all the Library listed below
    import java.util.ArrayList;
    import java.util.HashSet;
    import java.util.LinkedHashSet;
    import java.util.List;
    import java.util.Set;
    import java.util.TreeSet;
  3. Then i create a new  HashSet instance which i named it  “hash_Set “
  4. “link_HashSet.add(“”);” Add element into the the new “hash_set”
  5.  Finally i use the For Each Loop to print out all the element contain in the “hash_set” list.
  6. Repeat  the step 3 to 4 for creating LinkedHashSet  & TreeSet 
  7. Compare the Difference of all 3 commands LinkedHashSet  & TreeSet  & HashSet.

 



package hash;


import java.util.ArrayList;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;


public class hashSet {

	public static void main(String[] args) {
		
//********Create a new HashSet List**************************************
		
		Set<String>hash_Set= new HashSet<String>();
		
//********HashSet don't maintain sequence or Alphabetical Order in the Element****		
		
		//Add Element into the new instant "hash_Set"you have just created
		hash_Set.add("dog");
		hash_Set.add("cat");
		hash_Set.add("mouse");
		hash_Set.add("bird");
		
		System.out.println("Print out hash_Set");
		
		// Use the For Each loop to print out all element inside the hash_Set
		
		for (String item: hash_Set) {
			System.out.println(item);
		}
//********Create a new LinkedHashSet list**************************************
		
		Set<String> link_HashSet = new LinkedHashSet<String>();
//********LinkedHashSet  maintain sequence of the Element****			
		
 //Add Element into the new instant "link_HashSet "you have just created	
		link_HashSet.add("dog");
		link_HashSet.add("cat");	
		link_HashSet.add("mouse");
		link_HashSet.add("bird");
		
		
		System.out.println("Print out link_HashSet");
		
		
		
		for (String item: link_HashSet) {
			System.out.println(item);
		}
		
		//********Create a new TreeSet list**************************************
	
				Set<String> tree_Set = new TreeSet<String>();
		//******** tree_Set  will sort the Element in numbering sequence or alphabetical order****			
				
		 //Add Element into the new instant "link_HashSet "you have just created	
				tree_Set.add("dog");
				tree_Set.add("cat");	
				tree_Set.add("mouse");
				tree_Set.add("bird");
				
				
				System.out.println("Print out tree_Set");
				
				
				
				for (String item: tree_Set) {
					System.out.println(item);
				}
		

	}

}






 
Check out Java LinkedList here
Check out Oracle Java Site here

Leave a Reply

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

7 − 7 =