How to convert a Set to List in Java

0 votes

Hi Team,

I have one set in Java. I want to convert the set to a list. How to do that?

Dec 30, 2020 in Java by MD
• 95,460 points
623 views

1 answer to this question.

0 votes

Hi@MD,

The most straightforward way to convert a set to a list is bypassing 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));
    }
}

Hope this helps!

Check out java certification course to learn more.

Thanks!

answered Dec 30, 2020 by akhtar
• 38,260 points

edited Jun 22, 2023 by Khan Sarfaraz

Related Questions In Java

+1 vote
3 answers

How to convert a List to an Array in Java?

Either: Foo[] array = list.toArray(new Foo[list.size()]); or: Foo[] array = ...READ MORE

answered Aug 7, 2018 in Java by Akrati
• 3,190 points
1,159 views
0 votes
2 answers

How can I convert a String variable to a primitive int in Java

 Here are two ways illustrating this: Integer x ...READ MORE

answered Aug 20, 2019 in Java by Sirajul
• 59,230 points
2,356 views
0 votes
2 answers

How to convert array into list in Java?

Another workaround if you use apache commons-lang: int[] ...READ MORE

answered Aug 10, 2018 in Java by samarth295
• 2,220 points
908 views
0 votes
1 answer

How to set a timer in Java?

To work on timer in java, you ...READ MORE

answered May 16, 2018 in Java by Daisy
• 8,140 points
1,231 views
0 votes
2 answers

How to convert a JSON String into Object in Java?

You could probably check out Google's Gson: ...READ MORE

answered Aug 21, 2019 in Java by Sirajul
• 59,230 points
3,283 views
0 votes
2 answers

How to convert a byte into Hexadecimal in Java?

private static final String ...READ MORE

answered Aug 29, 2019 in Java by Sirajul
• 59,230 points
2,139 views
0 votes
2 answers

Store String inside a File using Java

We can use Apache Commons IO. It ...READ MORE

answered Jul 20, 2018 in Java by Sushmita
• 6,920 points
1,444 views
0 votes
3 answers

Adding text to a file using Java

try { final Path ...READ MORE

answered Sep 6, 2018 in Java by Sushmita
• 6,920 points
1,437 views
+1 vote
1 answer

Are arrays equivalent to objects in Java ?

Yes; the Java Language Specification writes: In the Java ...READ MORE

answered May 10, 2018 in Java by Rishabh
• 3,620 points
1,346 views
0 votes
1 answer

How to split Strings by space in Java ?

You can use split() method. str = "Hello ...READ MORE

answered May 16, 2018 in Java by sharth
• 3,370 points
1,618 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP