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.

Here is simple example:

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.

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.

Create a class named ListToSetCustomObjectMain as below:

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.

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.

Was this post helpful?


import_contacts

You may also like:

Related Posts

  • 09 January

    Convert set to String in Python

    Table of ContentsWays to convert set to string in PythonUsing the str() functionUsing the repr() functionUsing the join() functionUsing the map() functionConclusionWas this post helpful? Sets are an unordered collection of elements in Python. It does not contain duplicate elements and can be defined using the set() function in Python. Ways to convert set to […]

  • Convert String to Array in java
    11 December

    Convert String to Array in Java

    Table of ContentsHow to convert String to Array in JavaUsing toArray() method of SetUsing the split() method of String classUsing StringTokenizor classUsing the split() method of StringUtils classUsing split() method of Pattern classConclusionWas this post helpful? When developing applications in Java there are many cases where we will find ourselves converting data from one type […]

  • Convert float to int in Python
    19 June

    Convert float to int in Python

    Table of ContentsUsing the int() function to convert float to int in PythonUsing the math module functions to convert float to int in PythonUsing ceil()Using trunc()Using floorWas this post helpful? Python provides several in-built functions and modules for datatype conversions. Floating-point numbers can be easily converted to integers using some of these in-built functions. This […]

  • Java BigDecimal to Double
    17 May

    Java BigDecimal to Double

    Learn about how to convert BigDecimal to Double in java

  • Java Set to Array
    12 May

    Java Set to Array

    Table of Contents1. Using Java 8’s Stream2. Using toArray()3. Using toArray(IntFunction) [Java 11]4. Using System.arraycopy()5. Using Arrays.copyOf6. Using simple iteration7. Using Guava library7.1 Using FluentIterable7.2 Using IterablesWas this post helpful? In this post, we will learn java set to array conversion. There are many ways to convert set to an array. 1. Using Java 8’s […]

  • Convert Date to LocalDate in java
    12 January

    Java Date to LocalDate

    Table of ContentsUsing toInstant() method of Date classUsing toInstant() method of Date classUsing java.sql.DateWas this post helpful? In this post, we will see how to convert Date to LocalDate in java. Sometimes, we may need to convert Date to new Java 8 APIs and vice versa. There are multiple ways to convert Date to LocalDate […]

Leave a Reply

Your email address will not be published.

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.