Remove all elements LinkedHashSet example

1.Basic java example program to remove all elements in linkedhashset
  • clear()   This method used to remove all elements from Linkedhashset.

  1. package com.removeelementLinkedhashset;
  2.  
  3. import java.util.LinkedHashSet;
  4. import java.util.Iterator;
  5.  
  6. public class LinkedHashsetExample{
  7.  
  8. public static void main(String[] args) {
  9.   
  10. LinkedHashSet<String> linkedhashset = new LinkedHashSet<>();
  11.        
  12.         linkedhashset.add("Java Interview Questions");
  13.         linkedhashset.add("Java interview program");
  14.         linkedhashset.add("Concept and example program");
  15.         linkedhashset.add("Concept and interview questions");
  16.         linkedhashset.add("Java Quiz");
  17.     
  18.       
  19. System.out.println("LinkedHashSet before removal : " + linkedhashset);
  20.  
  21.   boolean blnRemoved = linkedhashset.remove("Java Quiz");
  22.   System.out.println("Was Java Quiz removed from LinkedHashSet ? " + blnRemoved);
  23.   

  24.  
  25. System.out.println("LinkedHashSet after removal : ");
  26.  
  27. Iterator it=linkedhashset.iterator();
  28.              
  29. while(it.hasNext()){
  30. System.out.println(it.next());
  31.                      
  32. }   
  33.  linkedhashset.clear();
  34.  System.out.println("LinkedHashSet after removal  all elements"); 
  35.  
  36. }
  37.  
  38. }
     



Output:

  1. LinkedHashSet before removal : [Java Interview Questions, Java interview program, Concept
  2. and example program, Concept and interview questions, Java Quiz]
  3. Was Java Quiz removed from LinkedHashSet ? true
  4. LinkedHashSet after removal :
  5. Java Interview Questions
  6. Java interview program
  7. Concept and example program
  8. Concept and interview questions
  9. LinkedHashSet after removal all elements  :[]

Remove specified element from Java LinkedHashSet example

1.Basic java example program to remove particular element in linkedhashset
  • boolean remove(Object o)    This method used to remove specified element from Linkedhashset.

  1. package com.removeelementLinkedhashset;
  2.  
  3. import java.util.LinkedHashSet;
  4. import java.util.Iterator;
  5.  
  6. public class LinkedHashsetExample{
  7.  
  8. public static void main(String[] args) {
  9.   
  10. LinkedHashSet<String> linkedhashset = new LinkedHashSet<>();
  11.        
  12.         linkedhashset.add("Java Interview Questions");
  13.         linkedhashset.add("Java interview program");
  14.         linkedhashset.add("Concept and example program");
  15.         linkedhashset.add("Concept and interview questions");
  16.         linkedhashset.add("Java Quiz");
  17.     
  18.       
  19. System.out.println("LinkedHashSet before removal : " + linkedhashset);
  20.  
  21.   boolean blnRemoved = linkedhashset.remove("Java Quiz");
  22.   System.out.println("Was Java Quiz removed from LinkedHashSet ? " + blnRemoved);
  23.   

  24.  
  25. System.out.println("LinkedHashSet after removal : ");
  26.  
  27. Iterator it=linkedhashset.iterator();
  28.              
  29. while(it.hasNext()){
  30. System.out.println(it.next());
  31.                      
  32. }   
  33.  
  34. }
  35.  
  36. }
     



Output:

  1. LinkedHashSet before removal : [Java Interview Questions, Java interview program, Concept
  2. and example program, Concept and interview questions, Java Quiz]
  3. Was Java Quiz removed from LinkedHashSet ? true
  4. Java Interview Questions
  5. Java interview program
  6. Concept and example program
  7. Concept and interview questions


Check if a particular element exists in Java LinkedHashSet Example

1.Basic java example program to check particular element is exists in linkedhashset
  • boolean contains(Object o)   This method Returns true if this set contains the specified element

  1. package com.checkelementhashset;
  2.  
  3. import java.util.LinkedHashSet;
  4. import java.util.Iterator;
  5.  
  6. public class LinkedHashsetExample{
  7.  
  8. public static void main(String[] args) {
  9.   
  10. LinkedHashSet<String> linkedhashset = new LinkedHashSet<>();
  11.        
  12.         linkedhashset.add("Java Interview Questions");
  13.         linkedhashset.add("Java interview program");
  14.         linkedhashset.add("Concept and example program");
  15.         linkedhashset.add("Concept and interview questions");
  16.         linkedhashset.add("Java Quiz");
  17.     
  18.  /*
  19.  To check whether a particular value exists in LinkedHashSet we need to use
  20.   boolean contains(Object value) method of HashSet class.
  21.  this method returns true if the LinkedHashSet contains the value, otherwise returns false.
  22.  */
  23.        
  24.         boolean isExists = linkedhashset.contains("Java Quiz");
  25.         System.out.println("Java Quiz exists in LinkedHashSet? : " + isExists);    
  26.  

  27.  
  28. Iterator it=linkedhashset.iterator();
  29.              
  30. while(it.hasNext()){
  31. System.out.println(it.next());
  32.                      
  33. }   
  34.  
  35. }
  36.  
  37. }
     



Output:

  1. Java Quiz exists in LinkedHashSet ? : true
  2. Java Interview Questions
  3. Java interview program
  4. Concept and example program
  5. Concept and interview questions
  6. Java Quiz
Select Menu