Converting LinkedHashSet to ArrayList or just using ArrayList
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
|
show 4 more comments
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
Possible duplicate of Convert Set to List without creating new List
– Nicholas K
Dec 30 '18 at 17:23
ChangeMyData
to use aCollection
. Why was it hardcoded to use anArrayList
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 theArrayList
between using aLinkedHashSet
and using any otherSet
, 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 withO(n)
? I don't get this point.
– AKSW
Dec 30 '18 at 17:27
1
...and I wonder whatpaths
is in your code? Is that aList
orSet
?
– nullpointer
Dec 30 '18 at 17:30
|
show 4 more comments
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
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
java arraylist linkedhashset
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
ChangeMyData
to use aCollection
. Why was it hardcoded to use anArrayList
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 theArrayList
between using aLinkedHashSet
and using any otherSet
, 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 withO(n)
? I don't get this point.
– AKSW
Dec 30 '18 at 17:27
1
...and I wonder whatpaths
is in your code? Is that aList
orSet
?
– nullpointer
Dec 30 '18 at 17:30
|
show 4 more comments
Possible duplicate of Convert Set to List without creating new List
– Nicholas K
Dec 30 '18 at 17:23
ChangeMyData
to use aCollection
. Why was it hardcoded to use anArrayList
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 theArrayList
between using aLinkedHashSet
and using any otherSet
, 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 withO(n)
? I don't get this point.
– AKSW
Dec 30 '18 at 17:27
1
...and I wonder whatpaths
is in your code? Is that aList
orSet
?
– 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
|
show 4 more comments
4 Answers
4
active
oldest
votes
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);
doesnew ArrayList<String>(<???>)
know to handle with a collection (likeLinkedHashSet
)? 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 theaddAll
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
|
show 1 more comment
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
add a comment |
@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
add a comment |
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));
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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);
doesnew ArrayList<String>(<???>)
know to handle with a collection (likeLinkedHashSet
)? 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 theaddAll
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
|
show 1 more comment
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);
doesnew ArrayList<String>(<???>)
know to handle with a collection (likeLinkedHashSet
)? 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 theaddAll
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
|
show 1 more comment
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);
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);
edited Dec 30 '18 at 17:38
answered Dec 30 '18 at 17:31
Daniel B.Daniel B.
1,166112
1,166112
doesnew ArrayList<String>(<???>)
know to handle with a collection (likeLinkedHashSet
)? 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 theaddAll
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
|
show 1 more comment
doesnew ArrayList<String>(<???>)
know to handle with a collection (likeLinkedHashSet
)? 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 theaddAll
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
|
show 1 more comment
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
add a comment |
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
add a comment |
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
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
answered Dec 30 '18 at 17:36
Dorian GrayDorian Gray
1,473516
1,473516
add a comment |
add a comment |
@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
add a comment |
@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
add a comment |
@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
@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
answered Dec 30 '18 at 17:55
Dilanka MDilanka M
9219
9219
add a comment |
add a comment |
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));
add a comment |
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));
add a comment |
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));
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));
answered Dec 30 '18 at 18:51
c0derc0der
8,57051744
8,57051744
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
Possible duplicate of Convert Set to List without creating new List
– Nicholas K
Dec 30 '18 at 17:23
Change
MyData
to use aCollection
. Why was it hardcoded to use anArrayList
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 aLinkedHashSet
and using any otherSet
, 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 aList
orSet
?– nullpointer
Dec 30 '18 at 17:30