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

Search a key in hashtable

We can use containsKey() method to search a key in hashtable. Syntax: hashtable.containsKey(specifiedKey); 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.containsKey("2")){ System.out.println("The Hashtable contains key … Read more

Copy map content to another hashtable

We can use putAll() method to copy map content to another hashtable in java. Syntax: hashtable.putAll(hashMap); Example: package com.w3schools;   import java.util.HashMap; 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); … Read more

Iterate through hashtable in java

To iterate through hashtable in java, first we have to get all keys as set object and then fetch each element using key. 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 … Read more