Converting LinkedHashSet to ArrayList or just using ArrayList












1















Consider the following code:



final Set<String> allPaths = new HashSet<String>();
for (final String path: paths) {
allPaths.add(path);
}
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?










share|improve this question























  • Possible duplicate of Convert Set to List without creating new List

    – Nicholas K
    Dec 30 '18 at 17:23











  • Change MyData to use a Collection. Why was it hardcoded to use an ArrayList in the first place?

    – Elliott Frisch
    Dec 30 '18 at 17:24






  • 1





    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.

    – RealSkeptic
    Dec 30 '18 at 17:25











  • 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.

    – AKSW
    Dec 30 '18 at 17:27






  • 1





    ...and I wonder what paths is in your code? Is that a List or Set?

    – nullpointer
    Dec 30 '18 at 17:30


















1















Consider the following code:



final Set<String> allPaths = new HashSet<String>();
for (final String path: paths) {
allPaths.add(path);
}
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?










share|improve this question























  • Possible duplicate of Convert Set to List without creating new List

    – Nicholas K
    Dec 30 '18 at 17:23











  • Change MyData to use a Collection. Why was it hardcoded to use an ArrayList in the first place?

    – Elliott Frisch
    Dec 30 '18 at 17:24






  • 1





    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.

    – RealSkeptic
    Dec 30 '18 at 17:25











  • 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.

    – AKSW
    Dec 30 '18 at 17:27






  • 1





    ...and I wonder what paths is in your code? Is that a List or Set?

    – nullpointer
    Dec 30 '18 at 17:30
















1












1








1








Consider the following code:



final Set<String> allPaths = new HashSet<String>();
for (final String path: paths) {
allPaths.add(path);
}
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?










share|improve this question














Consider the following code:



final Set<String> allPaths = new HashSet<String>();
for (final String path: paths) {
allPaths.add(path);
}
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?







java arraylist linkedhashset






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Dec 30 '18 at 17:21









TTaJTa4TTaJTa4

35019




35019













  • Possible duplicate of Convert Set to List without creating new List

    – Nicholas K
    Dec 30 '18 at 17:23











  • Change MyData to use a Collection. Why was it hardcoded to use an ArrayList in the first place?

    – Elliott Frisch
    Dec 30 '18 at 17:24






  • 1





    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.

    – RealSkeptic
    Dec 30 '18 at 17:25











  • 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.

    – AKSW
    Dec 30 '18 at 17:27






  • 1





    ...and I wonder what paths is in your code? Is that a List or Set?

    – nullpointer
    Dec 30 '18 at 17:30





















  • Possible duplicate of Convert Set to List without creating new List

    – Nicholas K
    Dec 30 '18 at 17:23











  • Change MyData to use a Collection. Why was it hardcoded to use an ArrayList in the first place?

    – Elliott Frisch
    Dec 30 '18 at 17:24






  • 1





    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.

    – RealSkeptic
    Dec 30 '18 at 17:25











  • 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.

    – AKSW
    Dec 30 '18 at 17:27






  • 1





    ...and I wonder what paths is in your code? Is that a List or Set?

    – nullpointer
    Dec 30 '18 at 17:30



















Possible duplicate of Convert Set to List without creating new List

– Nicholas K
Dec 30 '18 at 17:23





Possible duplicate of Convert Set to List without creating new List

– Nicholas K
Dec 30 '18 at 17:23













Change MyData to use a Collection. Why was it hardcoded to use an ArrayList in the first place?

– Elliott Frisch
Dec 30 '18 at 17:24





Change MyData to use a Collection. Why was it hardcoded to use an ArrayList in the first place?

– Elliott Frisch
Dec 30 '18 at 17:24




1




1





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.

– RealSkeptic
Dec 30 '18 at 17:25





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.

– RealSkeptic
Dec 30 '18 at 17:25













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.

– AKSW
Dec 30 '18 at 17:27





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.

– AKSW
Dec 30 '18 at 17:27




1




1





...and I wonder what paths is in your code? Is that a List or Set?

– nullpointer
Dec 30 '18 at 17:30







...and I wonder what paths is in your code? Is that a List or Set?

– nullpointer
Dec 30 '18 at 17:30














4 Answers
4






active

oldest

votes


















3














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<>();
mylist.addAll(allPaths);


or for even a simpler approach:



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





share|improve this answer


























  • does new ArrayList<String>(<???>) know to handle with a collection (like LinkedHashSet)? If so, will my way still work?

    – TTaJTa4
    Dec 30 '18 at 17:37













  • @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.

    – Daniel B.
    Dec 30 '18 at 17:41













  • 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
    Dec 30 '18 at 17:45











  • @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.

    – Daniel B.
    Dec 30 '18 at 17:46











  • I'm wondering what's different in your answer from what he already does in his question?!

    – AKSW
    Dec 30 '18 at 20:18



















2














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






share|improve this answer































    0














    @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:" +
    linkedHashSetToArrayList1);

    ArrayList<String> linkedHashSetToArrayList2 = new ArrayList<>();
    linkedHashSetToArrayList2.addAll(testStrings);
    System.out.println("** Printing linkedHashSetToArrayList2:" +
    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






    share|improve this answer































      0














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



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



      If paths is an array:



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






      share|improve this answer























        Your Answer






        StackExchange.ifUsing("editor", function () {
        StackExchange.using("externalEditor", function () {
        StackExchange.using("snippets", function () {
        StackExchange.snippets.init();
        });
        });
        }, "code-snippets");

        StackExchange.ready(function() {
        var channelOptions = {
        tags: "".split(" "),
        id: "1"
        };
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function() {
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled) {
        StackExchange.using("snippets", function() {
        createEditor();
        });
        }
        else {
        createEditor();
        }
        });

        function createEditor() {
        StackExchange.prepareEditor({
        heartbeatType: 'answer',
        autoActivateHeartbeat: false,
        convertImagesToLinks: true,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: 10,
        bindNavPrevention: true,
        postfix: "",
        imageUploader: {
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        },
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        });


        }
        });














        draft saved

        draft discarded


















        StackExchange.ready(
        function () {
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53979792%2fconverting-linkedhashset-to-arraylist-or-just-using-arraylist%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        4 Answers
        4






        active

        oldest

        votes








        4 Answers
        4






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        3














        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<>();
        mylist.addAll(allPaths);


        or for even a simpler approach:



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





        share|improve this answer


























        • does new ArrayList<String>(<???>) know to handle with a collection (like LinkedHashSet)? If so, will my way still work?

          – TTaJTa4
          Dec 30 '18 at 17:37













        • @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.

          – Daniel B.
          Dec 30 '18 at 17:41













        • 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
          Dec 30 '18 at 17:45











        • @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.

          – Daniel B.
          Dec 30 '18 at 17:46











        • I'm wondering what's different in your answer from what he already does in his question?!

          – AKSW
          Dec 30 '18 at 20:18
















        3














        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<>();
        mylist.addAll(allPaths);


        or for even a simpler approach:



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





        share|improve this answer


























        • does new ArrayList<String>(<???>) know to handle with a collection (like LinkedHashSet)? If so, will my way still work?

          – TTaJTa4
          Dec 30 '18 at 17:37













        • @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.

          – Daniel B.
          Dec 30 '18 at 17:41













        • 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
          Dec 30 '18 at 17:45











        • @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.

          – Daniel B.
          Dec 30 '18 at 17:46











        • I'm wondering what's different in your answer from what he already does in his question?!

          – AKSW
          Dec 30 '18 at 20:18














        3












        3








        3







        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<>();
        mylist.addAll(allPaths);


        or for even a simpler approach:



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





        share|improve this answer















        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<>();
        mylist.addAll(allPaths);


        or for even a simpler approach:



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






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Dec 30 '18 at 17:38

























        answered Dec 30 '18 at 17:31









        Daniel B.Daniel B.

        1,166112




        1,166112













        • does new ArrayList<String>(<???>) know to handle with a collection (like LinkedHashSet)? If so, will my way still work?

          – TTaJTa4
          Dec 30 '18 at 17:37













        • @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.

          – Daniel B.
          Dec 30 '18 at 17:41













        • 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
          Dec 30 '18 at 17:45











        • @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.

          – Daniel B.
          Dec 30 '18 at 17:46











        • I'm wondering what's different in your answer from what he already does in his question?!

          – AKSW
          Dec 30 '18 at 20:18



















        • does new ArrayList<String>(<???>) know to handle with a collection (like LinkedHashSet)? If so, will my way still work?

          – TTaJTa4
          Dec 30 '18 at 17:37













        • @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.

          – Daniel B.
          Dec 30 '18 at 17:41













        • 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
          Dec 30 '18 at 17:45











        • @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.

          – Daniel B.
          Dec 30 '18 at 17:46











        • I'm wondering what's different in your answer from what he already does in his question?!

          – AKSW
          Dec 30 '18 at 20:18

















        does new ArrayList<String>(<???>) know to handle with a collection (like LinkedHashSet)? If so, will my way still work?

        – TTaJTa4
        Dec 30 '18 at 17:37







        does new ArrayList<String>(<???>) know to handle with a collection (like LinkedHashSet)? If so, will my way still work?

        – TTaJTa4
        Dec 30 '18 at 17:37















        @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.

        – Daniel B.
        Dec 30 '18 at 17:41







        @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.

        – Daniel B.
        Dec 30 '18 at 17:41















        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
        Dec 30 '18 at 17:45





        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
        Dec 30 '18 at 17:45













        @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.

        – Daniel B.
        Dec 30 '18 at 17:46





        @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.

        – Daniel B.
        Dec 30 '18 at 17:46













        I'm wondering what's different in your answer from what he already does in his question?!

        – AKSW
        Dec 30 '18 at 20:18





        I'm wondering what's different in your answer from what he already does in his question?!

        – AKSW
        Dec 30 '18 at 20:18













        2














        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






        share|improve this answer




























          2














          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






          share|improve this answer


























            2












            2








            2







            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






            share|improve this answer













            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







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Dec 30 '18 at 17:36









            Dorian GrayDorian Gray

            1,473516




            1,473516























                0














                @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:" +
                linkedHashSetToArrayList1);

                ArrayList<String> linkedHashSetToArrayList2 = new ArrayList<>();
                linkedHashSetToArrayList2.addAll(testStrings);
                System.out.println("** Printing linkedHashSetToArrayList2:" +
                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






                share|improve this answer




























                  0














                  @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:" +
                  linkedHashSetToArrayList1);

                  ArrayList<String> linkedHashSetToArrayList2 = new ArrayList<>();
                  linkedHashSetToArrayList2.addAll(testStrings);
                  System.out.println("** Printing linkedHashSetToArrayList2:" +
                  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






                  share|improve this answer


























                    0












                    0








                    0







                    @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:" +
                    linkedHashSetToArrayList1);

                    ArrayList<String> linkedHashSetToArrayList2 = new ArrayList<>();
                    linkedHashSetToArrayList2.addAll(testStrings);
                    System.out.println("** Printing linkedHashSetToArrayList2:" +
                    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






                    share|improve this answer













                    @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:" +
                    linkedHashSetToArrayList1);

                    ArrayList<String> linkedHashSetToArrayList2 = new ArrayList<>();
                    linkedHashSetToArrayList2.addAll(testStrings);
                    System.out.println("** Printing linkedHashSetToArrayList2:" +
                    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







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Dec 30 '18 at 17:55









                    Dilanka MDilanka M

                    9219




                    9219























                        0














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



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



                        If paths is an array:



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






                        share|improve this answer




























                          0














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



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



                          If paths is an array:



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






                          share|improve this answer


























                            0












                            0








                            0







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



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



                            If paths is an array:



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






                            share|improve this answer













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



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



                            If paths is an array:



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







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Dec 30 '18 at 18:51









                            c0derc0der

                            8,57051744




                            8,57051744






























                                draft saved

                                draft discarded




















































                                Thanks for contributing an answer to Stack Overflow!


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid



                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.


                                To learn more, see our tips on writing great answers.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function () {
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53979792%2fconverting-linkedhashset-to-arraylist-or-just-using-arraylist%23new-answer', 'question_page');
                                }
                                );

                                Post as a guest















                                Required, but never shown





















































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown

































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown







                                Popular posts from this blog

                                Monofisismo

                                Angular Downloading a file using contenturl with Basic Authentication

                                Olmecas