Copy set content to another hashset in java

We can use addAll() method to copy set content to another hashset in java. Syntax: hashSet.addAll(subSet); 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

Iterate a hashset in java

Example: package com.w3schools;   import java.util.HashSet; import java.util.Iterator;   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"); Iterator<String> iterator = hashSet.iterator(); while(iterator.hasNext()){ System.out.println(iterator.next()); } } }package com.w3schools; import java.util.HashSet; import java.util.Iterator; public class Test { … Read more

Remove duplicate elements from a linked list in java

Example: package com.w3schools;   public class LinkedList { static Node head; static class Node { int data; Node next; Node(int data) { this.data = data; this.next = null; } }   void removeDuplicates() { Node ptr1 = null, ptr2 = null, duplicate = null; ptr1 = head; while (ptr1 != null && ptr1.next != null) … Read more

Remove duplicate elements from arraylist in java

Example: package com.w3schools;   import java.util.ArrayList; import java.util.HashSet; import java.util.List; import java.util.stream.Collectors;   public class Test { public static void main(String args[]){ ArrayList<String> arrayList = new ArrayList<String>(); arrayList.add("Jai"); arrayList.add("Mahesh"); arrayList.add("Vivek"); arrayList.add("Naren"); arrayList.add("Hemant"); arrayList.add("Vishal"); arrayList.add("Vishal"); arrayList.add("Naren"); System.out.println("Actual ArrayList:"+arrayList); //Prior to Java 8 List<String> newList1 = new ArrayList<>(new HashSet<>(arrayList)); System.out.println("Modified ArrayList:"+newList1); //Using Java 8 List<String> newList2 = … Read more

Eliminate duplicate keys user defined objects with Hashtable

Example: package com.w3schools;   import java.util.Hashtable; import java.util.Set;   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; } … Read more

hash table implementation with equals and hashcode example

Example: package com.w3schools;   import java.util.Hashtable;   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

Remove all elements from hashtable in java

We can use clear() method to remove all elements from hashtable in java. Syntax: hashtable.clear(); Example: package com.w3schools;   import java.util.Hashtable;   public class Test { public static void main(String args[]){ //Create Hashtable object. Hashtable<String, String> hashtable = new Hashtable<String, String>(); //Add objects to the Hashtable. hashtable.put("1", "Jai"); hashtable.put("2", "Mahesh"); hashtable.put("3","Vivek"); System.out.println(hashtable); hashtable.clear(); System.out.println("Content After … Read more

Get entrySet from hashtable in java

We can use entrySet() method get all key-value pairs from hashtable in java. It returns a Set object with all key-value pairs. Syntax: hashtable.entrySet(); Example: package com.w3schools;   import java.util.Hashtable; import java.util.Map.Entry; import java.util.Set;   public class Test { public static void main(String args[]){ //Create Hashtable object. Hashtable<String, String> hashtable = new Hashtable<String, String>(); //Add … Read more

Get all keys from hashtable in java

We can use keySet() method get all keys from hashtable in java. It returns a Set object with all keys. Syntax: hashtable.keySet(); Example: package com.w3schools;   import java.util.Hashtable; import java.util.Set;   public class Test { public static void main(String args[]){ //Create Hashtable object. Hashtable<String, String> hashtable = new Hashtable<String, String>(); //Add objects to the Hashtable. … Read more

Search a value in hashtable

We can use containsValue() method to search a value in hashtable. Syntax: hashtable.containsValue(specifiedValue); Example: package com.w3schools;   import java.util.Hashtable;   public class Test { public static void main(String args[]){ //Create Hashtable object. Hashtable<String, String> hashtable = new Hashtable<String, String>(); //Add objects to the Hashtable. hashtable.put("1", "Jai"); hashtable.put("2", "Mahesh"); hashtable.put("3","Vivek"); System.out.println(hashtable); if(hashtable.containsValue("Vivek")){ System.out.println("The Hashtable contains value … Read more