Unique set from ArrayList of ArrayList
Hi I have an arraylist of arraylist in this format:
[[val1, val2],[val3,val4],[val1,val2],[val1,val5]]
and would like to get the unique set of arraylists:
[[val1, val2],[val3,val4],[val1,val5]]
I have tried the following:
Set<String> uniques = new HashSet<>();
for (ArrayList<String> sublist : mappedEntities) {
uniques.addAll(sublist);
}
but this merges all the values of the internal arraylist together
java arraylist set
add a comment |
Hi I have an arraylist of arraylist in this format:
[[val1, val2],[val3,val4],[val1,val2],[val1,val5]]
and would like to get the unique set of arraylists:
[[val1, val2],[val3,val4],[val1,val5]]
I have tried the following:
Set<String> uniques = new HashSet<>();
for (ArrayList<String> sublist : mappedEntities) {
uniques.addAll(sublist);
}
but this merges all the values of the internal arraylist together
java arraylist set
1
Share your entire code. Also I think you needSet<ArrayList<String>>
instead ofSet<String>
– Nicholas K
Dec 31 '18 at 9:55
1
As @NicholasK says, the type ofuniques
needs to beSet<List<String>>
. TheaddAll
function takes a Collection and adds each member of the collection to the Set individually -- you just want to add the entire collection as a single item.
– tgdavies
Dec 31 '18 at 10:11
Doesn't aHashSet
only contain unique values likeHashmap
?
– SamzSakerz
Dec 31 '18 at 10:12
Yes @SamzSakerz -- that's correct.
– tgdavies
Dec 31 '18 at 10:15
Oh, I forgotArrayList<String>
are unique Objects
– SamzSakerz
Dec 31 '18 at 10:17
add a comment |
Hi I have an arraylist of arraylist in this format:
[[val1, val2],[val3,val4],[val1,val2],[val1,val5]]
and would like to get the unique set of arraylists:
[[val1, val2],[val3,val4],[val1,val5]]
I have tried the following:
Set<String> uniques = new HashSet<>();
for (ArrayList<String> sublist : mappedEntities) {
uniques.addAll(sublist);
}
but this merges all the values of the internal arraylist together
java arraylist set
Hi I have an arraylist of arraylist in this format:
[[val1, val2],[val3,val4],[val1,val2],[val1,val5]]
and would like to get the unique set of arraylists:
[[val1, val2],[val3,val4],[val1,val5]]
I have tried the following:
Set<String> uniques = new HashSet<>();
for (ArrayList<String> sublist : mappedEntities) {
uniques.addAll(sublist);
}
but this merges all the values of the internal arraylist together
java arraylist set
java arraylist set
asked Dec 31 '18 at 9:50
MarthaMartha
395
395
1
Share your entire code. Also I think you needSet<ArrayList<String>>
instead ofSet<String>
– Nicholas K
Dec 31 '18 at 9:55
1
As @NicholasK says, the type ofuniques
needs to beSet<List<String>>
. TheaddAll
function takes a Collection and adds each member of the collection to the Set individually -- you just want to add the entire collection as a single item.
– tgdavies
Dec 31 '18 at 10:11
Doesn't aHashSet
only contain unique values likeHashmap
?
– SamzSakerz
Dec 31 '18 at 10:12
Yes @SamzSakerz -- that's correct.
– tgdavies
Dec 31 '18 at 10:15
Oh, I forgotArrayList<String>
are unique Objects
– SamzSakerz
Dec 31 '18 at 10:17
add a comment |
1
Share your entire code. Also I think you needSet<ArrayList<String>>
instead ofSet<String>
– Nicholas K
Dec 31 '18 at 9:55
1
As @NicholasK says, the type ofuniques
needs to beSet<List<String>>
. TheaddAll
function takes a Collection and adds each member of the collection to the Set individually -- you just want to add the entire collection as a single item.
– tgdavies
Dec 31 '18 at 10:11
Doesn't aHashSet
only contain unique values likeHashmap
?
– SamzSakerz
Dec 31 '18 at 10:12
Yes @SamzSakerz -- that's correct.
– tgdavies
Dec 31 '18 at 10:15
Oh, I forgotArrayList<String>
are unique Objects
– SamzSakerz
Dec 31 '18 at 10:17
1
1
Share your entire code. Also I think you need
Set<ArrayList<String>>
instead of Set<String>
– Nicholas K
Dec 31 '18 at 9:55
Share your entire code. Also I think you need
Set<ArrayList<String>>
instead of Set<String>
– Nicholas K
Dec 31 '18 at 9:55
1
1
As @NicholasK says, the type of
uniques
needs to be Set<List<String>>
. The addAll
function takes a Collection and adds each member of the collection to the Set individually -- you just want to add the entire collection as a single item.– tgdavies
Dec 31 '18 at 10:11
As @NicholasK says, the type of
uniques
needs to be Set<List<String>>
. The addAll
function takes a Collection and adds each member of the collection to the Set individually -- you just want to add the entire collection as a single item.– tgdavies
Dec 31 '18 at 10:11
Doesn't a
HashSet
only contain unique values like Hashmap
?– SamzSakerz
Dec 31 '18 at 10:12
Doesn't a
HashSet
only contain unique values like Hashmap
?– SamzSakerz
Dec 31 '18 at 10:12
Yes @SamzSakerz -- that's correct.
– tgdavies
Dec 31 '18 at 10:15
Yes @SamzSakerz -- that's correct.
– tgdavies
Dec 31 '18 at 10:15
Oh, I forgot
ArrayList<String>
are unique Objects– SamzSakerz
Dec 31 '18 at 10:17
Oh, I forgot
ArrayList<String>
are unique Objects– SamzSakerz
Dec 31 '18 at 10:17
add a comment |
5 Answers
5
active
oldest
votes
can use Java 8 Collection Stream Distinct,
return in Set
datatype :
Set<List<String>> uniques = mappedEntities.stream().distinct().collect(Collectors.toSet());
if you want return in List
:
List<List<String>> uniques = mappedEntities.stream().distinct().collect(Collectors.toList());
2
While this does filter duplicates, it is an expensive way to do it, asdistinct()
creates aHashSet
and we then create aList
. Also we should be producing a data structure which will retain that uniqueness if we add further items to it -- we should create a Set, not a List. Finally I think an answer which uses a completely different concept is less helpful to the OP than one which explains the misconception in their current code.
– tgdavies
Dec 31 '18 at 10:17
add a comment |
Why not simply put them in a Set
like this?
Set<List<String>> uniques = new HashSet<>(mappedEntities);
Your mistake is that you are flattening the inner lists and putting their items in the set separately.
add a comment |
The issue here is that you need a Set
of ArrayList Set<ArrayList<String>>
, but you are using a Set
of Strings Set<String>
instead.
Given the list :
List<List<String>> mappedEntities = Arrays.asList(Arrays.asList("val1", "val2"),
Arrays.asList("val3", "val4"),
Arrays.asList("val1", "val2"),
Arrays.asList("val1", "val5"));
All you need to do is just declare the set and use the addAll()
.
Set<List<String>> mySet = new HashSet<>();
mySet.addAll(mappedEntities);
Since a set can hold only unique values, all duplicates will not be added to the set (No need to explicitly check this). You can now print it out :
mySet.forEach(System.out::println);
Or more simply, initialize the HashSet using the list mappedEntities
:
Set<List<String>> mySet = new HashSet<>(mappedEntities);
add a comment |
I am beginner on STACKOVERFLOW but i to try solve your problem
I think you want like this..
import java.util.*;
public class GFG {
public static void main(String args)
{
int n = 3;
// Here aList is an ArrayList of ArrayLists
ArrayList<ArrayList<String> > aList =
new ArrayList<ArrayList<String> >(n);
// Create n lists one by one and append to the
// master list (ArrayList of ArrayList)
ArrayList<String> a1 = new ArrayList<String>();
a1.add("1");
a1.add("2");
aList.add(a1);
ArrayList<String> a2 = new ArrayList<String>();
a2.add("11");
a2.add("22");
aList.add(a2);
ArrayList<String> a3 = new ArrayList<String>();
a3.add("1");
a3.add("2");
aList.add(a3);
Set<ArrayList<String>> uniques = new HashSet<ArrayList<String>>();
for (ArrayList<String> sublist : aList) {
uniques.add(sublist);
}
System.out.println("Your Answer");
for (ArrayList<String> x : uniques)
System.out.println(x);
}
}
Output: Your Answer [11, 22] [1, 2]
– Ajay Kumar
Dec 31 '18 at 10:51
add a comment |
try this code:
public class B {
public static void main(String args) throws Exception {
List<List<String>> list= Arrays.asList(
Arrays.asList("a","b","c"),
Arrays.asList("a","b","c"),
Arrays.asList("a","b","c","d"));
Set<List<String>> uniques = new HashSet<>();
for (List<String> sublist : list) {
if(!uniques.contains(sublist))
uniques.add(sublist);
}
System.out.println(uniques);
}
}
output:
[[a, b, c], [a, b, c, d]]
Since it is a set you wouldn't explicitly need to check for duplicates. This line is not neededif (!uniques.contains(sublist))
– Nicholas K
Dec 31 '18 at 10:43
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%2f53986002%2funique-set-from-arraylist-of-arraylist%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
can use Java 8 Collection Stream Distinct,
return in Set
datatype :
Set<List<String>> uniques = mappedEntities.stream().distinct().collect(Collectors.toSet());
if you want return in List
:
List<List<String>> uniques = mappedEntities.stream().distinct().collect(Collectors.toList());
2
While this does filter duplicates, it is an expensive way to do it, asdistinct()
creates aHashSet
and we then create aList
. Also we should be producing a data structure which will retain that uniqueness if we add further items to it -- we should create a Set, not a List. Finally I think an answer which uses a completely different concept is less helpful to the OP than one which explains the misconception in their current code.
– tgdavies
Dec 31 '18 at 10:17
add a comment |
can use Java 8 Collection Stream Distinct,
return in Set
datatype :
Set<List<String>> uniques = mappedEntities.stream().distinct().collect(Collectors.toSet());
if you want return in List
:
List<List<String>> uniques = mappedEntities.stream().distinct().collect(Collectors.toList());
2
While this does filter duplicates, it is an expensive way to do it, asdistinct()
creates aHashSet
and we then create aList
. Also we should be producing a data structure which will retain that uniqueness if we add further items to it -- we should create a Set, not a List. Finally I think an answer which uses a completely different concept is less helpful to the OP than one which explains the misconception in their current code.
– tgdavies
Dec 31 '18 at 10:17
add a comment |
can use Java 8 Collection Stream Distinct,
return in Set
datatype :
Set<List<String>> uniques = mappedEntities.stream().distinct().collect(Collectors.toSet());
if you want return in List
:
List<List<String>> uniques = mappedEntities.stream().distinct().collect(Collectors.toList());
can use Java 8 Collection Stream Distinct,
return in Set
datatype :
Set<List<String>> uniques = mappedEntities.stream().distinct().collect(Collectors.toSet());
if you want return in List
:
List<List<String>> uniques = mappedEntities.stream().distinct().collect(Collectors.toList());
edited Dec 31 '18 at 10:43
Nicholas K
6,89761133
6,89761133
answered Dec 31 '18 at 10:05
m fauzan abdim fauzan abdi
34129
34129
2
While this does filter duplicates, it is an expensive way to do it, asdistinct()
creates aHashSet
and we then create aList
. Also we should be producing a data structure which will retain that uniqueness if we add further items to it -- we should create a Set, not a List. Finally I think an answer which uses a completely different concept is less helpful to the OP than one which explains the misconception in their current code.
– tgdavies
Dec 31 '18 at 10:17
add a comment |
2
While this does filter duplicates, it is an expensive way to do it, asdistinct()
creates aHashSet
and we then create aList
. Also we should be producing a data structure which will retain that uniqueness if we add further items to it -- we should create a Set, not a List. Finally I think an answer which uses a completely different concept is less helpful to the OP than one which explains the misconception in their current code.
– tgdavies
Dec 31 '18 at 10:17
2
2
While this does filter duplicates, it is an expensive way to do it, as
distinct()
creates a HashSet
and we then create a List
. Also we should be producing a data structure which will retain that uniqueness if we add further items to it -- we should create a Set, not a List. Finally I think an answer which uses a completely different concept is less helpful to the OP than one which explains the misconception in their current code.– tgdavies
Dec 31 '18 at 10:17
While this does filter duplicates, it is an expensive way to do it, as
distinct()
creates a HashSet
and we then create a List
. Also we should be producing a data structure which will retain that uniqueness if we add further items to it -- we should create a Set, not a List. Finally I think an answer which uses a completely different concept is less helpful to the OP than one which explains the misconception in their current code.– tgdavies
Dec 31 '18 at 10:17
add a comment |
Why not simply put them in a Set
like this?
Set<List<String>> uniques = new HashSet<>(mappedEntities);
Your mistake is that you are flattening the inner lists and putting their items in the set separately.
add a comment |
Why not simply put them in a Set
like this?
Set<List<String>> uniques = new HashSet<>(mappedEntities);
Your mistake is that you are flattening the inner lists and putting their items in the set separately.
add a comment |
Why not simply put them in a Set
like this?
Set<List<String>> uniques = new HashSet<>(mappedEntities);
Your mistake is that you are flattening the inner lists and putting their items in the set separately.
Why not simply put them in a Set
like this?
Set<List<String>> uniques = new HashSet<>(mappedEntities);
Your mistake is that you are flattening the inner lists and putting their items in the set separately.
edited Dec 31 '18 at 10:52
answered Dec 31 '18 at 10:44
jbxjbx
11.3k1058111
11.3k1058111
add a comment |
add a comment |
The issue here is that you need a Set
of ArrayList Set<ArrayList<String>>
, but you are using a Set
of Strings Set<String>
instead.
Given the list :
List<List<String>> mappedEntities = Arrays.asList(Arrays.asList("val1", "val2"),
Arrays.asList("val3", "val4"),
Arrays.asList("val1", "val2"),
Arrays.asList("val1", "val5"));
All you need to do is just declare the set and use the addAll()
.
Set<List<String>> mySet = new HashSet<>();
mySet.addAll(mappedEntities);
Since a set can hold only unique values, all duplicates will not be added to the set (No need to explicitly check this). You can now print it out :
mySet.forEach(System.out::println);
Or more simply, initialize the HashSet using the list mappedEntities
:
Set<List<String>> mySet = new HashSet<>(mappedEntities);
add a comment |
The issue here is that you need a Set
of ArrayList Set<ArrayList<String>>
, but you are using a Set
of Strings Set<String>
instead.
Given the list :
List<List<String>> mappedEntities = Arrays.asList(Arrays.asList("val1", "val2"),
Arrays.asList("val3", "val4"),
Arrays.asList("val1", "val2"),
Arrays.asList("val1", "val5"));
All you need to do is just declare the set and use the addAll()
.
Set<List<String>> mySet = new HashSet<>();
mySet.addAll(mappedEntities);
Since a set can hold only unique values, all duplicates will not be added to the set (No need to explicitly check this). You can now print it out :
mySet.forEach(System.out::println);
Or more simply, initialize the HashSet using the list mappedEntities
:
Set<List<String>> mySet = new HashSet<>(mappedEntities);
add a comment |
The issue here is that you need a Set
of ArrayList Set<ArrayList<String>>
, but you are using a Set
of Strings Set<String>
instead.
Given the list :
List<List<String>> mappedEntities = Arrays.asList(Arrays.asList("val1", "val2"),
Arrays.asList("val3", "val4"),
Arrays.asList("val1", "val2"),
Arrays.asList("val1", "val5"));
All you need to do is just declare the set and use the addAll()
.
Set<List<String>> mySet = new HashSet<>();
mySet.addAll(mappedEntities);
Since a set can hold only unique values, all duplicates will not be added to the set (No need to explicitly check this). You can now print it out :
mySet.forEach(System.out::println);
Or more simply, initialize the HashSet using the list mappedEntities
:
Set<List<String>> mySet = new HashSet<>(mappedEntities);
The issue here is that you need a Set
of ArrayList Set<ArrayList<String>>
, but you are using a Set
of Strings Set<String>
instead.
Given the list :
List<List<String>> mappedEntities = Arrays.asList(Arrays.asList("val1", "val2"),
Arrays.asList("val3", "val4"),
Arrays.asList("val1", "val2"),
Arrays.asList("val1", "val5"));
All you need to do is just declare the set and use the addAll()
.
Set<List<String>> mySet = new HashSet<>();
mySet.addAll(mappedEntities);
Since a set can hold only unique values, all duplicates will not be added to the set (No need to explicitly check this). You can now print it out :
mySet.forEach(System.out::println);
Or more simply, initialize the HashSet using the list mappedEntities
:
Set<List<String>> mySet = new HashSet<>(mappedEntities);
edited Dec 31 '18 at 14:07
answered Dec 31 '18 at 10:38
Nicholas KNicholas K
6,89761133
6,89761133
add a comment |
add a comment |
I am beginner on STACKOVERFLOW but i to try solve your problem
I think you want like this..
import java.util.*;
public class GFG {
public static void main(String args)
{
int n = 3;
// Here aList is an ArrayList of ArrayLists
ArrayList<ArrayList<String> > aList =
new ArrayList<ArrayList<String> >(n);
// Create n lists one by one and append to the
// master list (ArrayList of ArrayList)
ArrayList<String> a1 = new ArrayList<String>();
a1.add("1");
a1.add("2");
aList.add(a1);
ArrayList<String> a2 = new ArrayList<String>();
a2.add("11");
a2.add("22");
aList.add(a2);
ArrayList<String> a3 = new ArrayList<String>();
a3.add("1");
a3.add("2");
aList.add(a3);
Set<ArrayList<String>> uniques = new HashSet<ArrayList<String>>();
for (ArrayList<String> sublist : aList) {
uniques.add(sublist);
}
System.out.println("Your Answer");
for (ArrayList<String> x : uniques)
System.out.println(x);
}
}
Output: Your Answer [11, 22] [1, 2]
– Ajay Kumar
Dec 31 '18 at 10:51
add a comment |
I am beginner on STACKOVERFLOW but i to try solve your problem
I think you want like this..
import java.util.*;
public class GFG {
public static void main(String args)
{
int n = 3;
// Here aList is an ArrayList of ArrayLists
ArrayList<ArrayList<String> > aList =
new ArrayList<ArrayList<String> >(n);
// Create n lists one by one and append to the
// master list (ArrayList of ArrayList)
ArrayList<String> a1 = new ArrayList<String>();
a1.add("1");
a1.add("2");
aList.add(a1);
ArrayList<String> a2 = new ArrayList<String>();
a2.add("11");
a2.add("22");
aList.add(a2);
ArrayList<String> a3 = new ArrayList<String>();
a3.add("1");
a3.add("2");
aList.add(a3);
Set<ArrayList<String>> uniques = new HashSet<ArrayList<String>>();
for (ArrayList<String> sublist : aList) {
uniques.add(sublist);
}
System.out.println("Your Answer");
for (ArrayList<String> x : uniques)
System.out.println(x);
}
}
Output: Your Answer [11, 22] [1, 2]
– Ajay Kumar
Dec 31 '18 at 10:51
add a comment |
I am beginner on STACKOVERFLOW but i to try solve your problem
I think you want like this..
import java.util.*;
public class GFG {
public static void main(String args)
{
int n = 3;
// Here aList is an ArrayList of ArrayLists
ArrayList<ArrayList<String> > aList =
new ArrayList<ArrayList<String> >(n);
// Create n lists one by one and append to the
// master list (ArrayList of ArrayList)
ArrayList<String> a1 = new ArrayList<String>();
a1.add("1");
a1.add("2");
aList.add(a1);
ArrayList<String> a2 = new ArrayList<String>();
a2.add("11");
a2.add("22");
aList.add(a2);
ArrayList<String> a3 = new ArrayList<String>();
a3.add("1");
a3.add("2");
aList.add(a3);
Set<ArrayList<String>> uniques = new HashSet<ArrayList<String>>();
for (ArrayList<String> sublist : aList) {
uniques.add(sublist);
}
System.out.println("Your Answer");
for (ArrayList<String> x : uniques)
System.out.println(x);
}
}
I am beginner on STACKOVERFLOW but i to try solve your problem
I think you want like this..
import java.util.*;
public class GFG {
public static void main(String args)
{
int n = 3;
// Here aList is an ArrayList of ArrayLists
ArrayList<ArrayList<String> > aList =
new ArrayList<ArrayList<String> >(n);
// Create n lists one by one and append to the
// master list (ArrayList of ArrayList)
ArrayList<String> a1 = new ArrayList<String>();
a1.add("1");
a1.add("2");
aList.add(a1);
ArrayList<String> a2 = new ArrayList<String>();
a2.add("11");
a2.add("22");
aList.add(a2);
ArrayList<String> a3 = new ArrayList<String>();
a3.add("1");
a3.add("2");
aList.add(a3);
Set<ArrayList<String>> uniques = new HashSet<ArrayList<String>>();
for (ArrayList<String> sublist : aList) {
uniques.add(sublist);
}
System.out.println("Your Answer");
for (ArrayList<String> x : uniques)
System.out.println(x);
}
}
answered Dec 31 '18 at 10:49
Ajay KumarAjay Kumar
12
12
Output: Your Answer [11, 22] [1, 2]
– Ajay Kumar
Dec 31 '18 at 10:51
add a comment |
Output: Your Answer [11, 22] [1, 2]
– Ajay Kumar
Dec 31 '18 at 10:51
Output: Your Answer [11, 22] [1, 2]
– Ajay Kumar
Dec 31 '18 at 10:51
Output: Your Answer [11, 22] [1, 2]
– Ajay Kumar
Dec 31 '18 at 10:51
add a comment |
try this code:
public class B {
public static void main(String args) throws Exception {
List<List<String>> list= Arrays.asList(
Arrays.asList("a","b","c"),
Arrays.asList("a","b","c"),
Arrays.asList("a","b","c","d"));
Set<List<String>> uniques = new HashSet<>();
for (List<String> sublist : list) {
if(!uniques.contains(sublist))
uniques.add(sublist);
}
System.out.println(uniques);
}
}
output:
[[a, b, c], [a, b, c, d]]
Since it is a set you wouldn't explicitly need to check for duplicates. This line is not neededif (!uniques.contains(sublist))
– Nicholas K
Dec 31 '18 at 10:43
add a comment |
try this code:
public class B {
public static void main(String args) throws Exception {
List<List<String>> list= Arrays.asList(
Arrays.asList("a","b","c"),
Arrays.asList("a","b","c"),
Arrays.asList("a","b","c","d"));
Set<List<String>> uniques = new HashSet<>();
for (List<String> sublist : list) {
if(!uniques.contains(sublist))
uniques.add(sublist);
}
System.out.println(uniques);
}
}
output:
[[a, b, c], [a, b, c, d]]
Since it is a set you wouldn't explicitly need to check for duplicates. This line is not neededif (!uniques.contains(sublist))
– Nicholas K
Dec 31 '18 at 10:43
add a comment |
try this code:
public class B {
public static void main(String args) throws Exception {
List<List<String>> list= Arrays.asList(
Arrays.asList("a","b","c"),
Arrays.asList("a","b","c"),
Arrays.asList("a","b","c","d"));
Set<List<String>> uniques = new HashSet<>();
for (List<String> sublist : list) {
if(!uniques.contains(sublist))
uniques.add(sublist);
}
System.out.println(uniques);
}
}
output:
[[a, b, c], [a, b, c, d]]
try this code:
public class B {
public static void main(String args) throws Exception {
List<List<String>> list= Arrays.asList(
Arrays.asList("a","b","c"),
Arrays.asList("a","b","c"),
Arrays.asList("a","b","c","d"));
Set<List<String>> uniques = new HashSet<>();
for (List<String> sublist : list) {
if(!uniques.contains(sublist))
uniques.add(sublist);
}
System.out.println(uniques);
}
}
output:
[[a, b, c], [a, b, c, d]]
edited Dec 31 '18 at 11:39
answered Dec 31 '18 at 10:15
ZhaoGangZhaoGang
1,9641116
1,9641116
Since it is a set you wouldn't explicitly need to check for duplicates. This line is not neededif (!uniques.contains(sublist))
– Nicholas K
Dec 31 '18 at 10:43
add a comment |
Since it is a set you wouldn't explicitly need to check for duplicates. This line is not neededif (!uniques.contains(sublist))
– Nicholas K
Dec 31 '18 at 10:43
Since it is a set you wouldn't explicitly need to check for duplicates. This line is not needed
if (!uniques.contains(sublist))
– Nicholas K
Dec 31 '18 at 10:43
Since it is a set you wouldn't explicitly need to check for duplicates. This line is not needed
if (!uniques.contains(sublist))
– Nicholas K
Dec 31 '18 at 10:43
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%2f53986002%2funique-set-from-arraylist-of-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
1
Share your entire code. Also I think you need
Set<ArrayList<String>>
instead ofSet<String>
– Nicholas K
Dec 31 '18 at 9:55
1
As @NicholasK says, the type of
uniques
needs to beSet<List<String>>
. TheaddAll
function takes a Collection and adds each member of the collection to the Set individually -- you just want to add the entire collection as a single item.– tgdavies
Dec 31 '18 at 10:11
Doesn't a
HashSet
only contain unique values likeHashmap
?– SamzSakerz
Dec 31 '18 at 10:12
Yes @SamzSakerz -- that's correct.
– tgdavies
Dec 31 '18 at 10:15
Oh, I forgot
ArrayList<String>
are unique Objects– SamzSakerz
Dec 31 '18 at 10:17