Table of Contents
In this post, we will see how to convert List to Set in java.We will convert ArrayList to HashSet.
As HashSet does not allow duplicates, when you convert ArrayList to HashSet, all the duplicates will be discarded.
You can simply use the constructor of HashSet to convert ArrayList to HashSet.
1 2 3 |
HashSet set=new HashSet(list); |
Here is simple example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
package org.arpit.java2blog; import java.util.ArrayList; import java.util.HashSet; import java.util.List; import java.util.Set; public class ListToSetMain { public static void main(String args[]) { List<String> listofCountries=new ArrayList<>(); listofCountries.add("India"); listofCountries.add("China"); listofCountries.add("Bhutan"); listofCountries.add("Nepal"); listofCountries.add("India"); System.out.println("======================="); System.out.println("List of Countries: "); System.out.println("======================="); for(String country:listofCountries) { System.out.println(country); } // Converting list to set Set<String> countriesSet=new HashSet<String>(listofCountries); System.out.println("======================="); System.out.println("Set of Countries: "); System.out.println("======================="); for(String country:countriesSet) { System.out.println(country); } } } |
When you run above program, you will get below output
List of Countries:
=======================
India
China
Bhutan
Nepal
India
=======================
Set of Countries:
=======================
Bhutan
China
Nepal
India
Java 8 list of set example
You can use Java 8 Stream API to convert list to set.
1 2 3 4 |
// Converting list to set in java 8 Set<String> countriesSet=listofCountries.stream().collect(Collectors.toCollection(HashSet::new)); |
Just change line 29 in ListToSetMain.java to above and you will get same output.
List to Set in case of Custom objects
You need to quite careful when you are converting list of custom objects to Set.
Let’s understand with help of simple example:
Create a class named "Country". We will put object of this class to list and then convert it to list.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
package org.arpit.java2blog; public class Country { String name; long population; public Country(String name, long population) { super(); this.name = name; this.population = population; } public String getName() { return name; } public void setName(String name) { this.name = name; } public long getPopulation() { return population; } public void setPopulation(long population) { this.population = population; } @Override public String toString() { return "Country [name=" + name + ", population=" + population + "]"; } } |
Create a class named ListToSetCustomObjectMain as below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
package org.arpit.java2blog; import java.util.ArrayList; import java.util.HashSet; import java.util.List; import java.util.Set; public class ListToSetCustomObjectMain { public static void main(String args[]) { List<Country> listofCountries=new ArrayList<>(); listofCountries.add(new Country("India",20000)); listofCountries.add(new Country("China",30000)); listofCountries.add(new Country("Bhutan",1000)); listofCountries.add(new Country("Nepal",3000)); listofCountries.add(new Country("India",20000)); System.out.println("======================="); System.out.println("List of Countries: "); System.out.println("======================="); for(Country country:listofCountries) { System.out.println(country); } // Converting list to set Set<Country> countriesSet=new HashSet<Country>(listofCountries); System.out.println("======================="); System.out.println("Set of Countries: "); System.out.println("======================="); for(Country country:countriesSet) { System.out.println(country); } } } |
When you run above program, you will get below output:
List of Countries:
=======================
Country [name=India, population=20000] Country [name=China, population=30000] Country [name=Bhutan, population=1000] Country [name=Nepal, population=3000] Country [name=India, population=20000] =======================
Set of Countries:
=======================
Country [name=India, population=20000] Country [name=Nepal, population=3000] Country [name=India, population=20000] Country [name=Bhutan, population=1000] Country [name=China, population=30000]
As you can see, we have Country [name=India, population=20000] twice in the output but these are duplicate entries but HashSet does not consider it as duplicates.
Do you know why?
Because we did not implement hashcode and equals method in Country class.
Let’s add below methods to Country class.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
@Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((name == null) ? 0 : name.hashCode()); result = prime * result + (int) (population ^ (population >>> 32)); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Country other = (Country) obj; if (name == null) { if (other.name != null) return false; } else if (!name.equals(other.name)) return false; if (population != other.population) return false; return true; } |
When you run above program again after adding these two methods. You will get below output:
List of Countries:
=======================
Country [name=India, population=20000] Country [name=China, population=30000] Country [name=Bhutan, population=1000] Country [name=Nepal, population=3000] Country [name=India, population=20000] =======================
Set of Countries:
=======================
Country [name=India, population=20000] Country [name=Nepal, population=3000] Country [name=China, population=30000] Country [name=Bhutan, population=1000]
As you can see, we have only one entry for Country [name=India, population=20000] in Set.
That’s all about converting a Set to list in java.