| by Arround The Web | No comments

How to Convert a Set to a List in Java

In Java, there can be a requirement for the developer to contain duplicate or null values. For instance, allocating the space for the values but updating them later in accordance with the requirement. In addition to that, the “list” approach is effective to invoke the elements by their indexes, instantly. Therefore, the conversion of “set” to “list” in Java is assistive for the programmer to perform such functionalities conveniently.

This blog will discuss the approaches to converting a “set” to a “list” in Java.

How to Convert a “Set” to a “List” in Java?

To convert a “set” to a “list” in Java, apply the following approaches:

Before proceeding to the approaches, make sure to include the below-provided package to access all the classes and methods:

import java.util.*;

Approach 1: Convert a Set to a List in Java by Passing the Set as List Constructor Argument

The “add()” method is used to add a specific element into a “Set”. In this approach, the set can be transformed into a list by passing the set as a list constructor argument, thereby appending set values to a list via the “add()” method.

Syntax

add(elem)

In this syntax, “elem” corresponds to the elements that need to be added to the corresponding set.

Example
Let’s overview the below-provided example:

public class settolist1 {
 public static void main(String[] args) {
  Set<Integer> settoList = new HashSet<>();
  settoList.add(1);
  settoList.add(2);
  settoList.add(3);
  List<Integer> list = new ArrayList<>(settoList);
  System.out.println("The list becomes: "+list);
}}

In the above code snippet:

  • Firstly, create a “HashSet” of “Integer” type named “settoList”.
  • After that, append the stated integers to the set via the associated “add()” method.
  • Now, create an “ArrayList” object via the “new” keyword and the “ArrayList()” constructor, respectively.
  • Also, pass the initialized set as a constructor, i.e., “ArrayList” argument.
  • This will result in transforming the set into a list by adding the set elements into a list.

Note: The “ArrayList” implements the interface “List”, therefore an instance of the ArrayList can be allocated to a variable of type “List”. As a result, a new ArrayList is created and the associated object contains all the functionalities of an ArrayList.

Output

In this output, it can be observed that the list is appended with the allocated set values.

Approach 2: Convert a Set to a List in Java Using the “List.addAll()” Method

Lists” contain a method referred to as “addAll()” that adds multiple values to the list at once. Moreover, this method also works for adding the elements of a set to a list, which is the requirement.

Syntax

collect.addAll(items)

In this syntax:

  • collect” refers to the collection in which elements need to be added.
  • items” point to the elements list that must be added.

Example
Let’s go through the following example:

public class settolist1 {
 public static void main(String[] args) {
  Set<String> settoList = new HashSet<>();
  settoList.add("London");
  settoList.add("New York");
  settoList.add("Tokyo");
  List<String> list = new ArrayList<>();
  list.addAll(settoList);
  System.out.println("The list becomes: "+list);
}}

In this code block, perform the following steps:

  • Recall the discussed approaches for creating a set of “String” types, and adding values to it via the “add()” method.
  • In the next step, likewise, create an “ArrayList” having the data type synchronized with that of “set”.
  • Now, associate the “addAll()” method with the created list to convert the passed set, as its argument, to the list.
  • Lastly, display the resultant list, on the console.

Output

In this outcome, it can be analyzed that the allocated string values in the set became a part of the list.

Approach 3: Convert a Set to a List in Java Using the “List.copyOf()” Method

The “List.copyOf()” method creates an immutable list with the help of the specified collection. This method can be applied to copy the allocated set values to a list by referring to the corresponding set.

Example
The following example demonstrates the stated concept:

public class settolist2 {
 public static void main(String[] args) {
  Set<Object> settoList = new HashSet<>();
  settoList.add("London");
  settoList.add(2);
  settoList.add("Tokyo");
  List<Object> list;
  list = List.copyOf(settoList);
  System.out.println("The list becomes: "+list);
}}

In these lines of code, apply the following steps:

  • Likewise, repeat the discussed approaches for creating a set of “Object” types and add the provided values to it.
  • Note: The “Object” type supports both the “Integer” and “String” data types which can be evident from the appended values.
  • After that, declare a “List” named “list” of the identical type, i.e., “Object”.
  • In the next step, associate the “copyOf()” method with the list to append the set values into a list.

Output

In this output, it can be implied that the list becomes appended with the set values of the “Object” type.

Approach 4: Convert a Set to a List in Java Using a “User-defined” Function

In this particular approach, the set can be converted into a list by passing the declared set to the “User-defined” function:

public class settolist3 {
 public static <T> List<T> SetToList(Set<T> set) {
  List<T> givenList = new ArrayList<>();
  for (T x : set) {
   givenList.add(x);
}
 return givenList;
}
 public static void main(String args[]) {
  Set<Integer> set = new HashSet<Integer>();
  set.add(10);
  set.add(20);
  set.add(30);
  List<Integer> list = SetToList(set);
  System.out.println("The list becomes: " +list);
}}

According to the above code, apply the below-given steps:

  • Firstly, create a user-defined function named “SetToList()” having the “set” that needs to be converted into “list”, as its parameter.
  • In the function definition, likewise, create an “ArrayList” named “givenList”.
  • Now, apply the “for” loop to iterate through the passed “set” and append the created list with the “set” values, thereby performing the conversion and returning it(list).
  • In the “main()” method, similarly, create a set of “Integer” types and add the stated integer values in it.
  • Finally, invoke the “User-defined” function by passing the allocated set as its argument and returning the converted list.

Output

The above output signifies that the desired requirement is fulfilled.

Conclusion

To convert a set to a list in Java, pass the set as a constructor argument, apply the “List.addAll()” method, the “List.copyOf()” method, or the “User-defined” function. These approaches convert the declared set of “Integer”, “String” or “Object” types into a list. This article elaborated on the approaches to converting a set to a list in Java.

Share Button

Source: linuxhint.com

Leave a Reply