List to HashSet java

Converting ArrayList to Set in Java means creating a Set implementation like HashSet from an ArrayList full of objects. Before Converting your ArrayList into HashSet do remember that List keeps insertion order and guarantees the same but Set doesn't have such obligation. Also, List allows duplicates but Set doesn't allow any duplicates, which means if you have duplicates in your ArrayList they will be lost when you convert ArrayList to HashSet and that's the reason why the sometimes size of ArrayList doesn't match with the size of HashSet after conversion.


Converting ArrayList to Set is entirely different than Converting Map to List but has one thing in common, A constructor that takes a collection object. HashSet also has a constructor that takes another collection object e.g. ArrayList and creates Set out of those elements.

We have also seen a bit of this on 10 Examples of ArrayList in Java and we will see here in detail.

And, If you are new to the Java world then I also recommend you go through these Java Collection Coursesto learn Java in a better and more structured way. This is one of the best and up-to-date courses to learn Java online.

How to Convert List to Set in Java

Now, let's see a couple of ways to convert a List to Set like HashSet in Java :

1. ArrayList to Set Conversion Example

Converting ArrayList or any other List implementation into HashSet or any other Set Implementation is not very difficult to write. In this Java tutorial,we will see a complete code example of converting ArrayList to HashSet in Java:





package test;

import java.util.ArrayList;
import java.util.HashSet;

public class ArrayListToSetConverter {

public static void main[String args[]]{
//Creating ArrayList for converting into HashSet
ArrayListcompanies = new ArrayList[];
companies.add["Sony"];
companies.add["Samsung"];
companies.add["Microsoft"];
companies.add["Intel"];
companies.add["Sony"];
System.out.println["Size of ArrayList before Converstion: " + companies.size[]];
System.out.println[companies];
//Converting ArrayList into HashSet in Java
HashSetcompanySet = new HashSet[companies];
System.out.println["Size of HashSet after Converstion: " + companies.size[]];
System.out.println[companySet];
}
}

Output:
Size of ArrayList before Conversation: 5
[Sony, Samsung, Microsoft, Intel, Sony]
Size of HashSet after Conversation: 5
[Sony, Microsoft, Intel, Samsung]


You might have noticed that theSize of the Converted ArrayList cum HashSet is not the same and one less than the original ArrayList because duplicate entry "Sony" is just one time in Set. This is another great way of removing duplicates from ArrayList. just copy entries of ArrayList into Set and then copy it back into ArrayList you don't have duplicates anymore.


Thats all on the quick tip to Convert an ArrayList into HashSet in Java. You may check thedifference between ArrayList and Vector to know more about ArrayList and other Collection classes.


Related Java Tutorial
How to parse XML File using DOM Parser in Java
10 Points on Generics in Java Tutorial with Example
Secrets of Final keyword in Java
How SubString method works in Java
How to replace String using Regular Expression in Java
How to run Java Program from Command Prompt

Video liên quan

Chủ Đề