Eliminate duplicate user defined objects from linkedhashset

Example: package com.w3schools;   import java.util.LinkedHashSet;   class Employee{ private String name; private int salary; private int id;   public Employee(int id, String name, int salary){ this.id = id; this.name = name; this.salary = salary; }   public String getName() { return name; } public void setName(String name) { this.name = name; } public int … Read more

Check if a particular element exists in LinkedHashSet

We can use contains() method to check if a particular element exists in LinkedHashSet. Syntax: linkedHashSet.contains(specifiedElement); Example: package com.w3schools;   import java.util.LinkedHashSet;   public class Test { public static void main(String args[]){ //Create LinkedHashSet LinkedHashSet<String> linkedHashSet = new LinkedHashSet<String>(); //Add elements to LinkedHashSet linkedHashSet.add("Jai"); linkedHashSet.add("Mahesh"); linkedHashSet.add("Vikas"); linkedHashSet.add("Sahil"); linkedHashSet.add("Sachin"); linkedHashSet.add("Vijay"); System.out.println("LinkedHashSet: " + linkedHashSet); System.out.println("LinkedHashSet contains … Read more

Delete specific element from linkedhashset

We can use remove() method to remove the specific element from linkedhashset in java. Syntax: linkedHashSet.remove(specifiedElement); Example: package com.w3schools;   import java.util.LinkedHashSet;   public class Test { public static void main(String args[]){ //Create LinkedHashSet LinkedHashSet<String> linkedHashSet = new LinkedHashSet<String>(); //Add elements to LinkedHashSet linkedHashSet.add("Jai"); linkedHashSet.add("Mahesh"); linkedHashSet.add("Vikas"); linkedHashSet.add("Sahil"); linkedHashSet.add("Sachin"); linkedHashSet.add("Vijay"); System.out.println("LinkedHashSet: " + linkedHashSet); linkedHashSet.remove("Vikas"); System.out.println("LinkedHashSet … Read more

Remove all elements from linkedhashset in java

We can use clear() method to remove all elements from linkedhashset in java. Syntax: linkedHashSet.clear(); Example: package com.w3schools;   import java.util.LinkedHashSet;   public class Test { public static void main(String args[]){ //Create LinkedHashSet LinkedHashSet<String> linkedHashSet = new LinkedHashSet<String>(); //Add elements to LinkedHashSet linkedHashSet.add("Jai"); linkedHashSet.add("Mahesh"); linkedHashSet.add("Vikas"); linkedHashSet.add("Sahil"); linkedHashSet.add("Sachin"); linkedHashSet.add("Vijay"); System.out.println("LinkedHashSet: " + linkedHashSet); linkedHashSet.clear(); System.out.println("LinkedHashSet after … Read more

Add all elements of a set to linkedhashset in java

We can use addAll() method to add all elements of a set to linkedhashset in java. Syntax: linkedHashSet.addAll(subSet); Example: package com.w3schools;   import java.util.HashSet; import java.util.LinkedHashSet;   public class Test { public static void main(String args[]){ //Create LinkedHashSet LinkedHashSet<String> linkedHashSet = new LinkedHashSet<String>(); //Add elements to LinkedHashSet linkedHashSet.add("Jai"); linkedHashSet.add("Mahesh"); linkedHashSet.add("Vikas"); linkedHashSet.add("Sahil"); linkedHashSet.add("Sachin"); linkedHashSet.add("Vijay"); System.out.println("LinkedHashSet: " … Read more

Linkedhashset to array in java

We can use toArray() method to convert linkedhashset to array in java. Syntax: linkedHashSet.toArray(strArray); Example: package com.w3schools;   import java.util.LinkedHashSet;   public class Test { public static void main(String args[]){ //Create LinkedHashSet LinkedHashSet<String> linkedHashSet = new LinkedHashSet<String>(); //Add elements to LinkedHashSet linkedHashSet.add("Jai"); linkedHashSet.add("Mahesh"); linkedHashSet.add("Vikas"); linkedHashSet.add("Sahil"); linkedHashSet.add("Sachin"); linkedHashSet.add("Vijay"); System.out.println("LinkedHashSet: " + linkedHashSet); String[] strArray = new … Read more

Iterate a linkedhashset in java

Example: package com.w3schools;   import java.util.Iterator; import java.util.LinkedHashSet;   public class Test { public static void main(String args[]){ //Create LinkedHashSet LinkedHashSet<String> linkedHashSet = new LinkedHashSet<String>(); //Add elements to LinkedHashSet linkedHashSet.add("Jai"); linkedHashSet.add("Mahesh"); linkedHashSet.add("Vikas"); linkedHashSet.add("Sahil"); linkedHashSet.add("Sachin"); linkedHashSet.add("Vijay"); Iterator<String> iterator = linkedHashSet.iterator(); while(iterator.hasNext()){ System.out.println(iterator.next()); } } }package com.w3schools; import java.util.Iterator; import java.util.LinkedHashSet; public class Test { public static … Read more

Eliminate duplicate user defined objects from hashset in java

Example: package com.w3schools;   import java.util.HashSet;   class Employee{ private String name; private int salary; private int id;   public Employee(int id, String name, int salary){ this.id = id; this.name = name; this.salary = salary; }   public String getName() { return name; } public void setName(String name) { this.name = name; } public int … Read more

Hashset to array in java

We can use toArray() method to convert a hashset to an array in java. Syntax: hashSet.toArray(strArray); Example: package com.w3schools;   import java.util.HashSet;   public class Test { public static void main(String[] args) { //Create HashSet object HashSet<String> hashSet = new HashSet<String>(); //Add elements to HashSet hashSet.add("Jai"); hashSet.add("Mahesh"); hashSet.add("Vivek"); hashSet.add("Naren"); hashSet.add("Vishal"); hashSet.add("Hemant"); System.out.println("HashSet: " + hashSet); … Read more

Remove all elements from hashset in java

We can use clear() method to remove all elements from hashset in java. Syntax: hashSet.clear(); Example: package com.w3schools;   import java.util.HashSet;   public class Test { public static void main(String[] args) { //Create HashSet object HashSet<String> hashSet = new HashSet<String>(); //Add elements to HashSet hashSet.add("Jai"); hashSet.add("Mahesh"); hashSet.add("Vivek"); hashSet.add("Naren"); hashSet.add("Vishal"); hashSet.add("Hemant"); System.out.println("HashSet: " + hashSet); hashSet.clear(); … Read more