Converting LinkedHashSet to ArrayList or just using ArrayList

convert hashset to arraylist java
convert linkedhashset to list java
convert hashset to arraylist c
convert hashmap to arraylist
convert hashset to list c#
convert hashset to int array java

Consider the following code:

final Set<String> allPaths = new HashSet<String>();
for (final String path: paths) {
final MyData d = new MyData(new ArrayList<String>(allPaths));

MyData is some class I should not touch. It should get an ArrayList as an argument. Before this day, we used that way because we didn't care about the order, that is why we used Set (so there will not be duplicated). But now, I would like to keep the order of the elements, so after some research, I found out that I can use the data structure LinkedHashSet in order to do so. So I did:

final LinkedHashSet<String> allPaths = new LinkedHashSet<String>();
for (final String path: paths) {
        allPaths .add(path);
final MyData d = new MyData(new ArrayList<String>(allPaths));

Problem is, I'm not sure how to convert LinkedHashSet to ArrayList. Also, I thought of using ArrayList instead of LinkedHashSet so I won't have to convert it, but I'll have to iterate over the array (O(n)).

What good, clean and effiect way should I use?

Just use the public boolean addAll(Collection<? extends E> c) method, on the arrayList, it accepts any Collection.

You have your LinkedHashSet:

final LinkedHashSet<String> allPaths = new LinkedHashSet<String>();
for (final String path: paths) {
        allPaths .add(path);

and then do (you can use this even if mylist is not empty):

List<String> myList = new ArrayList<>();

or for even a simpler approach:

List<String> myList = new ArrayList<>(allPaths);

Convert LinkedHashSet to ArrayList in Java Example, LinkedHashSet is Hashtable & linked list implementation of the Set interface, with predictable iteration order. LinkedHashSet maintains the Insertion order of  Given a LinkedHashSet collection of String objects in java. Set<String> cmputerGenerations = new LinkedHashSet<>(); Convert LinkedHashSet of String object to ArrayList collection in java (with example) 1. Convert LinkedHashSet of String objects to ArrayList collection in java

Why dont you just convert your paths to an LinkedHashSet like that (assuming that paths is a Collection?

final MyData d = new MyData(new ArrayList<>(new LinkedHashSet<>(paths)));

In case paths is an array, you can use Arrays.asList(paths) inside the conversion above

Convert LinkedHashSet of String objects to ArrayList collection (java , To convert a given hashset to an arraylist, all we need is to use arraylist constructor and pass hashset as constructor argument. It will copy all elements from  But can you confirm that LinkedHashSet scores over arraylist and linkedlist in terms of creation, Insertion . No. For the normal use case of just calling add(), AL and LL are both O(1), while LHS is O(log N). But LHS serves a different purpose than AL and LL. As a Set, it's for when you want to make sure no element is duplicated.

@TTaJTa4 you can use the code below as an example. Both ways are fine.

import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.Set;

public class ConvertLinkedHashSetToArrayList

  public static void main(String[] args)
    Set<String> testStrings = new LinkedHashSet<>();
    testStrings.add("String 1");
    testStrings.add("String 2");
    testStrings.add("String 3");
    testStrings.add("String 4");
    testStrings.add("String 5");

    System.out.println("** Printing LinkedHashSet: " + testStrings);
    ArrayList<String> linkedHashSetToArrayList1 = new ArrayList<>(testStrings);
    System.out.println("** Printing linkedHashSetToArrayList1:" + 

    ArrayList<String> linkedHashSetToArrayList2 = new ArrayList<>();
    System.out.println("** Printing linkedHashSetToArrayList2:" + 

Results are like:

** Printing LinkedHashSet: [String 1, String 2, String 3, String 4, String 5]
** Printing linkedHashSetToArrayList1:[String 1, String 2, String 3, String 4, String 5]
** Printing linkedHashSetToArrayList2:[String 1, String 2, String 3, String 4, String 5]

Follow the GitHub link for the full java project: GitHub example

How to convert HashSet to ArrayList in Java, In this tutorial we will be learning how to convert a HashSet to a List (ArrayList). Program Here we have a HashSet of String elements and we are creating. Java doesn't have a direct API to sort HashSet and LinkedHashSet collections. However, you can sort by converting them to TreeSet and ArrayList collections Let's walk through this tutorial to explore in more details Sort by converting to TreeSet You can convert a HashSet or LinkedHashSet to TreeSet by using

If paths is a collection you can get an array list no duplicates:

ArrayList<String> p =;

If paths is an array:

ArrayList<String> p = Stream.of(paths).distinct().collect(Collectors.toCollection(ArrayList::new));

Convert HashSet to a List/ArrayList, This code example shows how to convert HashSet to ArrayList: import java.util.​ArrayList; import java.util.HashSet; import java.util.List; import java.util.Set; public class How to use Java String.split method to split a string by dot? How to  Convert LinkedList to ArrayList in Java Java 8 Object Oriented Programming Programming A LinkedList can be converted into an ArrayList by creating an ArrayList such that the parameterized constructor of the ArrayList initialises it with the elements of the LinkedList. A program that demonstrates this is given as follows −

Converting LinkedHashSet to ArrayList or just using ArrayList , LinkedHashSet – stores elements in a hash table with a linked list and maintains the insertion-order ( the order in which the elements were  Below are methods to convert ArrayList to LinkedList in Java: Brute Force or Naive Method: In this method, an empty LinkedList is created and all elements present of the ArrayList are added to it one by one. Algorithm: Get the ArrayList to be converted. Create an empty LinkedList; Iterate through the items in the ArrayList.

How to Convert a HashSet to an ArrayList?, To convert array to set , we first convert it to a list using asList() as HashSet accepts list as a constructor. Then, we initialize set with the elements of the converted  This example shows how to get LinkedHashSet elements by index in Java. This example also shows how to get elements by index from LinkedHashSet using an array, ArrayList, and iterator. How to get elements by index from LinkedHashSet in Java? The LinkedHashSet in Java is a hash table and linked list implementation of the Set interface.

convert Array / ArrayList to Set in Java. Use LinkedHashSet to , In this post we will see examples to convert ArrayList to HashSet in java. HashSet Hence we can pass ArrayList to the construtor of the HashSet and get it converted. Please note that For that you may need to use a LinkedHashSet. The following situations may arise for converting List (ArrayList) to Set. 1. just to remove the duplicates in the ArrayList and no need to maintain the original ArrayList order. (convert ArrayList to HashSet) 2. to remove the duplicates in the ArrayList and preserve the ArrayList order (convert ArrayList to LinkedHashSet)

  • Possible duplicate of Convert Set to List without creating new List
  • Change MyData to use a Collection. Why was it hardcoded to use an ArrayList in the first place?
  • What is wrong with the way you are doing it? There should not be a difference in the creation of the ArrayList between using a LinkedHashSet and using any other Set, except that it will use the proper iteration order.
  • what's the problem with what you're doing right now? I mean, creating a list fro ma set is basically what you did and this is fine. what does it have to do with O(n)? I don't get this point.
  • ...and I wonder what paths is in your code? Is that a List or Set?
  • does new ArrayList<String>(<???>) know to handle with a collection (like LinkedHashSet)? If so, will my way still work?
  • @TTaJTa4 yes, that is the whole point of the Collection interface. It is an abstract idea, that has many implementations, but it is basically the same thing, a collection of things. Java uses Collection, instead of hard coded implementations (like ArrayList for example), to be able to swap from one implementation to another - just like in your case, when you want to use LinkedHashSet instead of HashSet, without the need to re-write the method/class you are passing the argument into.
  • addAll is merely an extra over what the OP already did. There is no need to use it, it can be used the way it was originally.
  • @RealSkeptic you call use the addAll method even if the list is not empty, in case he needs it, as I mentioned in my answer.
  • I'm wondering what's different in your answer from what he already does in his question?!