Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

How to convert List to Set in java

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.

HashSet set=new HashSet(list);

Here is simple example:

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 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 countriesSet=new HashSet(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.

// Converting list to set in java 8
		Set 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.

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:

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 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 countriesSet=new HashSet(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.

@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.

The post How to convert List to Set in java appeared first on Java2Blog.



This post first appeared on How To Learn Java Programming, please read the originial post: here

Share the post

How to convert List to Set in java

×

Subscribe to How To Learn Java Programming

Get updates delivered right to your inbox!

Thank you for your subscription

×