how to get data from array list to set java

www.la‮ttu‬uri.com
how to get data from array list to set java

To get the data from an ArrayList and store it in a Set in Java, you can use the addAll() method of the Set interface.

Here's an example of how to get data from an ArrayList and store it in a Set in Java:

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

ArrayList<String> list = new ArrayList<>();
list.add("Apple");
list.add("Banana");
list.add("Orange");

Set<String> set = new HashSet<>();
set.addAll(list);

System.out.println("Set: " + set);

In the above example, the list is an ArrayList of strings, and the set is a Set of strings. The addAll() method is used to add all the elements in the list to the set.

The Set interface is a member of the java.util package and extends the Collection interface. It represents a collection of objects in which each object can only occur once.

The HashSet class is an implementation of the Set interface that uses a hash table to store the elements. It does not allow duplicate elements and does not maintain the insertion order of the elements.

For more information on working with ArrayList and Set in Java, you can refer to the documentation for the ArrayList and Set classes in the Java API.

Created Time:2017-11-01 20:42:54  Author:lautturi