![]() ![]() NullPointerException or ClassCastException. Attempting toĪdd an ineligible element throws an unchecked exception, typically For example, some implementations prohibit null elements,Īnd some have restrictions on the types of their elements. Some set implementations have restrictions on the elements that That it is not permissible for a set to contain itself as an element. ![]() Is changed in a manner that affects equals comparisons while the The behavior of a set is not specified if the value of an object Note: Great care must be exercised if mutable objects are used as setĮlements. That all constructors must create a set that contains no duplicate elements The additional stipulation on constructors is, not surprisingly, ![]() Not contain any additional stipulations.) (The specifications accompanying theseĭeclarations have been tailored to the Set interface, but they do Declarations for other inherited methods areĪlso included here for convenience. Inherited from the Collection interface, on the contracts of allĬonstructors and on the contracts of the add, equals and The Set interface places additional stipulations, beyond those Its name, this interface models the mathematical set abstraction. More formally, setsĬontain no pair of elements e1 and e2 such thatĮ1.equals(e2), and at most one null element. List list = List.of( "a", "b", "c", "a") ĬollectionUtils.A collection that contains no duplicate elements. To do so, we create an empty HashSet and use the addAll() method of CollectionUtils class to copy all the elements from the List into the Set. We can also use Apache Commons Collections Library to convert a Java List to a Set. Set set = Sets.newHashSet(list) Code language: Java ( java ) List to Set Using Apache Commons Library The Sets class of the Guava Library provides the newHashSet() method that creates a new HashSet containing all the elements from the given Java Collection. Set set = Set.copyOf(list) Code language: Java ( java ) List to Set Using Guava Library To create an immutable Set from a List, we can use the copyOf() method of the Set interface. The HashSet created by the constructor is a mutable one. Set set = new HashSet(list) Code language: Java ( java ) ![]() The constructor creates a new HashSet instance containing all the unique elements of the given List.Įxample of converting a Java List to a mutable HashSet using Java. The most basic solution to convert a Java List to a Set is to use the HashSet constructor and provide the List. Thus, all the duplicate elements are r emoved when we convert a List to a Set. Please remember that a List is not a collection of unique elements. Now, let’s learn to convert a Java List to a Set. We understood how we could convert a Java Set to a List. List list = pyOf(set) Code language: Java ( java ) However, if we want to create an immutable List, we can use the copyOf() method of the List interface.Įxample of converting a Java Set to an immutable List using Java. List list = new ArrayList(set) Code language: Java ( java ) The ArrayList constructor creates a new ArrayList containing all the elements from the given Java collection.Įxample of converting a Java Set to a mutable List using Java. The most straightforward way of converting a Java Set to a List is to use the ArrayList constructor and supply the source Set. List to Set Using Apache Commons LibraryĪs the Sets are a unique collection of elements and lists are not, we can easily convert Sets to Lists.Set to List Using Apache Commons Library. ![]()
0 Comments
Leave a Reply. |