Piano Guidance
Photo by Karolina Grabowska Pexels Logo Photo: Karolina Grabowska

How do I convert a set to a list?

The most straightforward way to convert a set to a list is by passing the set as an argument while creating the list. This calls the constructor and from there onwards the constructor takes care of the rest. Since the set has been converted to a list, the elements are now ordered.

How do you solo over chord changes blues?
How do you solo over chord changes blues?

The easiest way to approach a blues solo is to use the minor pentatonic scale of the key for all the chords. So, in the key of A, we're going to...

Read More »
What famous painting went missing?
What famous painting went missing?

'Poppy Flowers' by Vincent van Gogh The painting was stolen from the Mohamed Mahmoud Khalil Museum, Cairo, in August 2010. Valued at $50 million,...

Read More »

While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial. Lists in Java are ordered collection of data, whereas sets are an unordered collection of data. A list can have duplicate entries, a set can not. Both the data structures are useful in different scenarios. Knowing how to convert a set into a list is useful. It can convert unordered data into ordered data.

Initializing a set

Let’s initialize a set and add some elements to it.

import java . util . * ; public class Main { public static void main ( String [ ] args ) { Set < Integer > a = new HashSet < > ( ) ; a . add ( 1 ) ; a . add ( 2 ) ; a . add ( 3 ) ; a . add ( 1 ) ; System . out . println ( a ) ; } } The add() method of HashSet adds the elements to the set. Note that the elements are distinct. There is no way to get the elements according to their insertion order as the sets are unordered.

Converting Set to List in Java

Let’s convert the set into a list. There are multiple ways of doing so. Each way is different from the other and these differences are subtle.

1. List Constructor with Set argument

The most straightforward way to convert a set to a list is by passing the set as an argument while creating the list. This calls the constructor and from there onwards the constructor takes care of the rest. import java . util . * ; public class Main { public static void main ( String [ ] args ) { Set < Integer > a = new HashSet < > ( ) ; a . add ( 1 ) ; a . add ( 2 ) ; a . add ( 3 ) ; a . add ( 1 ) ; List < Integer > arr = new ArrayList < > ( a ) ; System . out . println ( arr ) ; System . out . println ( arr . get ( 1 ) ) ; } }

Output

Since the set has been converted to a list, the elements are now ordered. That means we can use the get() method to access elements by index.

2. Using conventional for loop

We can use the good old for loop to explicitly copy the elements from the set to the list. import java . util . * ; public class Main { public static void main ( String [ ] args ) { Set < Integer > a = new HashSet < > ( ) ; a . add ( 1 ) ; a . add ( 2 ) ; a . add ( 3 ) ; a . add ( 1 ) ; List < Integer > arr = new ArrayList < > ( a ) ; for ( int i : a ) arr . add ( i ) ; System . out . println ( arr ) ; System . out . println ( arr . get ( 1 ) ) ; } } For loop goes over the set element by element and adds the elements to the list.

Why are 7th chords used in jazz?
Why are 7th chords used in jazz?

Including the 7th in the chord (or playing it instead of the 5th) lets you hear a clear difference in the dominant chord (major 3rd, minor 7th). In...

Read More »
Should you memorize a piano piece?
Should you memorize a piano piece?

It makes playing difficult passages easier as pianist play recalling from his memory. Memorizing makes learning to play new pieces easier and there...

Read More »

3. List addAll() method

Lists have a method called addAll() that adds multiple values to the list at once. You might recall this operation from its use in merging two lists. addAll() also works for adding the elements of a set to a list. import java . util . * ; public class Main { public static void main ( String [ ] args ) { Set < Integer > a = new HashSet < > ( ) ; a . add ( 1 ) ; a . add ( 2 ) ; a . add ( 3 ) ; a . add ( 1 ) ; List < Integer > arr = new ArrayList < > ( ) ; arr . addAll ( a ) ; System . out . println ( arr ) ; System . out . println ( arr . get ( 1 ) ) ; } }

4. Stream API collect() method

Stream.collect() is available from Java 8 onwards. ToList collector collects all Stream elements into a List instance. import java . util . * ; import java . util . stream . Collectors ; public class Main { public static void main ( String [ ] args ) { Set < Integer > a = new HashSet < > ( ) ; a . add ( 1 ) ; a . add ( 2 ) ; a . add ( 3 ) ; a . add ( 1 ) ; List < Integer > arr ; arr = a . stream ( ) . collect ( Collectors . toList ( ) ) ; System . out . println ( arr ) ; System . out . println ( arr . get ( 1 ) ) ; } } The documentation for stream.collect() mentions that there is no guarantee on the type, mutability, serializability, or thread-safety of the List returned. If more control over the returned List is required, use toCollection(Supplier).

To specify the type of list use toCollection(ArrayList::new)

import java . util . * ; import java . util . stream . Collectors ; public class Main { public static void main ( String [ ] args ) { Set < Integer > a = new HashSet < > ( ) ; a . add ( 1 ) ; a . add ( 2 ) ; a . add ( 3 ) ; a . add ( 1 ) ; List < Integer > arr ; arr = a . stream ( ) . collect ( Collectors . toCollection ( ArrayList :: new ) ) ; System . out . println ( arr ) ; System . out . println ( arr . get ( 1 ) ) ; } }

5. List.copyOf() method

Java 10 onwards List has a copyOf() method. The method returns an unmodifiable List containing the elements of the given Collection, in its iteration order. The list can’t contain any null elements. In case the set contains ‘null’, the method returns a null pointer exception. import java . util . * ; import java . util . stream . Collectors ; public class Main { public static void main ( String [ ] args ) { Set < Integer > a = new HashSet < > ( ) ; a . add ( 1 ) ; a . add ( 2 ) ; a . add ( 3 ) ; a . add ( 1 ) ; List < Integer > arr ; arr = List . copyOf ( a ) ; System . out . println ( arr ) ; System . out . println ( arr . get ( 1 ) ) ; } }

Can I learn Japanese in 2 years?
Can I learn Japanese in 2 years?

The average length of time to learn advanced Japanese is 2-3 years. At the intermediate level, you can understand most of what your teacher says,...

Read More »
Why is middle C called C4?
Why is middle C called C4?

Why Is Middle C Also Known As C4? The C that lives on that shared ledger line, one line above the bass clef and one line below the treble clef, is...

Read More »

import java . util . * ; import java . util . stream . Collectors ; public class Main { public static void main ( String [ ] args ) { Set < Integer > a = new HashSet < > ( ) ; a . add ( 1 ) ; a . add ( 2 ) ; a . add ( 3 ) ; a . add ( 1 ) ; a . add ( null ) ; List < Integer > arr ; arr = List . copyOf ( a ) ; System . out . println ( arr ) ; System . out . println ( arr . get ( 1 ) ) ; } }

Null Pointer Exception

If you try to add an element to an immutable list you’ll get an error that looks like the following.

Immutable Error

Using the addAll() method to convert set with null element to a list : import java . util . * ; import java . util . stream . Collectors ; public class Main { public static void main ( String [ ] args ) { Set < Integer > a = new HashSet < > ( ) ; a . add ( 1 ) ; a . add ( 2 ) ; a . add ( 3 ) ; a . add ( 1 ) ; a . add ( null ) ; List < Integer > arr = new ArrayList < > ( ) ; arr . addAll ( a ) ; System . out . println ( arr ) ; System . out . println ( arr . get ( 1 ) ) ; } }

Note that the null appears at the beginning of the list.

Conclusion

We saw some really interesting methods to convert a set into a list. It’s important to pay attention to the type of list that is created from each method. Like copyOf() method produces an immutable list and can’t handle null elements. Whereas, stream.collect() doesn’t guarantee anything. Constructor and addAll() are the most trustworthy among the batch.

Can you program a chip key yourself?
Can you program a chip key yourself?

Insert the key into the ignition, and turn it to the on position. Don't turn on the engine, only the electronics. Leave the key in this position...

Read More »
What is a blowing instrument called?
What is a blowing instrument called?

Wind instruments are typically grouped into two families: Brass instruments (horns, trumpets, trombones, euphoniums, and tubas) Woodwind...

Read More »
Which keyboard is best buy for beginners?
Which keyboard is best buy for beginners?

The Best Keyboard Pianos for Beginners of 2022 Best Overall. Alesis Recital Pro. Best Value. Casio CT-X700. Donner DEP-20. Casio Casiotone LK-S250....

Read More »
Can you learn piano with 25 keys?
Can you learn piano with 25 keys?

Although you're more than capable to make great sounds, beats, and songs on just 25 keys, 88 key models are way better. You'll need 88 keys (or at...

Read More »