Sorting this Map in Java
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I have a Map where FullName is an object and contains another object of class Name that contains FirstName and LastName. Firstname and last names are String. (Yes i know its a bad design, but i am trying to learn sorting)
The key String is just id's such as 1,2,3,...
I want to sort based on the full name(both firstname and last name), and just return a list of id's.
This is the code i have so far, but i am getting a syntax error on the sorted portion where i am passing the comparator. And also I am pretty sure i am doing something incorrect semantically too.
List<String> listofIds = map.entrySet().stream()
.sorted(new ValueComparator(map))
.map(Map.Entry::getKey)
.collect(toList());
class ValueComparator implements Comparator<String> {
Map<String, FullName> base;
public ValueComparator(Map<String, FullName> base) {
this.base = base;
}
public int compare(String a, String b) {
FullName fullName1 = base.get(a);
FullName fullName2 = base.get(b);
return(fullName1.getName().getFirstName()
.compareTo(fullName2.getName().getFirstName()));
}
}
java sorting hashmap
add a comment |
I have a Map where FullName is an object and contains another object of class Name that contains FirstName and LastName. Firstname and last names are String. (Yes i know its a bad design, but i am trying to learn sorting)
The key String is just id's such as 1,2,3,...
I want to sort based on the full name(both firstname and last name), and just return a list of id's.
This is the code i have so far, but i am getting a syntax error on the sorted portion where i am passing the comparator. And also I am pretty sure i am doing something incorrect semantically too.
List<String> listofIds = map.entrySet().stream()
.sorted(new ValueComparator(map))
.map(Map.Entry::getKey)
.collect(toList());
class ValueComparator implements Comparator<String> {
Map<String, FullName> base;
public ValueComparator(Map<String, FullName> base) {
this.base = base;
}
public int compare(String a, String b) {
FullName fullName1 = base.get(a);
FullName fullName2 = base.get(b);
return(fullName1.getName().getFirstName()
.compareTo(fullName2.getName().getFirstName()));
}
}
java sorting hashmap
You want to compare Map entries together. So what you need is a Comparator<Map.Entry<String, FullName>>, not a Comparator<String>.
– JB Nizet
Nov 16 '18 at 19:45
It would be helpful to have the code for FullName since it's used in the code you do share, and actual error. For more advice on how to write a good question, see: stackoverflow.com/help/how-to-ask
– Geoffrey Wiseman
Nov 16 '18 at 19:48
add a comment |
I have a Map where FullName is an object and contains another object of class Name that contains FirstName and LastName. Firstname and last names are String. (Yes i know its a bad design, but i am trying to learn sorting)
The key String is just id's such as 1,2,3,...
I want to sort based on the full name(both firstname and last name), and just return a list of id's.
This is the code i have so far, but i am getting a syntax error on the sorted portion where i am passing the comparator. And also I am pretty sure i am doing something incorrect semantically too.
List<String> listofIds = map.entrySet().stream()
.sorted(new ValueComparator(map))
.map(Map.Entry::getKey)
.collect(toList());
class ValueComparator implements Comparator<String> {
Map<String, FullName> base;
public ValueComparator(Map<String, FullName> base) {
this.base = base;
}
public int compare(String a, String b) {
FullName fullName1 = base.get(a);
FullName fullName2 = base.get(b);
return(fullName1.getName().getFirstName()
.compareTo(fullName2.getName().getFirstName()));
}
}
java sorting hashmap
I have a Map where FullName is an object and contains another object of class Name that contains FirstName and LastName. Firstname and last names are String. (Yes i know its a bad design, but i am trying to learn sorting)
The key String is just id's such as 1,2,3,...
I want to sort based on the full name(both firstname and last name), and just return a list of id's.
This is the code i have so far, but i am getting a syntax error on the sorted portion where i am passing the comparator. And also I am pretty sure i am doing something incorrect semantically too.
List<String> listofIds = map.entrySet().stream()
.sorted(new ValueComparator(map))
.map(Map.Entry::getKey)
.collect(toList());
class ValueComparator implements Comparator<String> {
Map<String, FullName> base;
public ValueComparator(Map<String, FullName> base) {
this.base = base;
}
public int compare(String a, String b) {
FullName fullName1 = base.get(a);
FullName fullName2 = base.get(b);
return(fullName1.getName().getFirstName()
.compareTo(fullName2.getName().getFirstName()));
}
}
java sorting hashmap
java sorting hashmap
asked Nov 16 '18 at 19:40
SaadSaad
447412
447412
You want to compare Map entries together. So what you need is a Comparator<Map.Entry<String, FullName>>, not a Comparator<String>.
– JB Nizet
Nov 16 '18 at 19:45
It would be helpful to have the code for FullName since it's used in the code you do share, and actual error. For more advice on how to write a good question, see: stackoverflow.com/help/how-to-ask
– Geoffrey Wiseman
Nov 16 '18 at 19:48
add a comment |
You want to compare Map entries together. So what you need is a Comparator<Map.Entry<String, FullName>>, not a Comparator<String>.
– JB Nizet
Nov 16 '18 at 19:45
It would be helpful to have the code for FullName since it's used in the code you do share, and actual error. For more advice on how to write a good question, see: stackoverflow.com/help/how-to-ask
– Geoffrey Wiseman
Nov 16 '18 at 19:48
You want to compare Map entries together. So what you need is a Comparator<Map.Entry<String, FullName>>, not a Comparator<String>.
– JB Nizet
Nov 16 '18 at 19:45
You want to compare Map entries together. So what you need is a Comparator<Map.Entry<String, FullName>>, not a Comparator<String>.
– JB Nizet
Nov 16 '18 at 19:45
It would be helpful to have the code for FullName since it's used in the code you do share, and actual error. For more advice on how to write a good question, see: stackoverflow.com/help/how-to-ask
– Geoffrey Wiseman
Nov 16 '18 at 19:48
It would be helpful to have the code for FullName since it's used in the code you do share, and actual error. For more advice on how to write a good question, see: stackoverflow.com/help/how-to-ask
– Geoffrey Wiseman
Nov 16 '18 at 19:48
add a comment |
1 Answer
1
active
oldest
votes
Your comparator is for String
objects, but map.entrySet().stream()
returns a stream of Entry<String, FullName>
objects.
As suggested by JB in the comments, you can use the static Comparator
method #comparing
to quickly build a Comparator
:
List<String> listofIds = map.entrySet().stream().sorted(
Comparator.comparing(e -> e.getValue().getName().getFirstName())
).map(Map.Entry::getKey).collect(toList());
You can take advantage of Comparator
being a functional interface like this:
List<String> listofIds = map.entrySet().stream().sorted(
(e1, e2) -> e1.getValue().getName().getFirstName().compareTo(
e2.getValue().getName().getFirstName())
).map(Map.Entry::getKey).collect(toList());
If you still want to use a Comparator
object, you could use:
private static class ValueComparator implements Comparator<Entry<String, FullName>> {
@Override
public int compare(Entry<String, FullName> e1, Entry<String, FullName>> e2) {
FullName fullName1 = e1.getValue();
FullName fullName2 = e2.getValue();
return fullName1.getName().getFirstName()
.compareTo(fullName2.getName().getFirstName());
}
}
1
An even better solution would be to useComparator.comparing(e -> e.getValue().getName().getFirstName())
– JB Nizet
Nov 16 '18 at 19:53
@JBNizet I wasn't aware of that function, nice!
– lucasvw
Nov 16 '18 at 19:55
I'm sure you can make the adjustment to compare both :) Maybe give it a try and if you run into trouble you can ask a new question. Glad I can help!
– lucasvw
Nov 16 '18 at 20:37
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%2f53344337%2fsorting-this-mapstring-fullname-in-java%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Your comparator is for String
objects, but map.entrySet().stream()
returns a stream of Entry<String, FullName>
objects.
As suggested by JB in the comments, you can use the static Comparator
method #comparing
to quickly build a Comparator
:
List<String> listofIds = map.entrySet().stream().sorted(
Comparator.comparing(e -> e.getValue().getName().getFirstName())
).map(Map.Entry::getKey).collect(toList());
You can take advantage of Comparator
being a functional interface like this:
List<String> listofIds = map.entrySet().stream().sorted(
(e1, e2) -> e1.getValue().getName().getFirstName().compareTo(
e2.getValue().getName().getFirstName())
).map(Map.Entry::getKey).collect(toList());
If you still want to use a Comparator
object, you could use:
private static class ValueComparator implements Comparator<Entry<String, FullName>> {
@Override
public int compare(Entry<String, FullName> e1, Entry<String, FullName>> e2) {
FullName fullName1 = e1.getValue();
FullName fullName2 = e2.getValue();
return fullName1.getName().getFirstName()
.compareTo(fullName2.getName().getFirstName());
}
}
1
An even better solution would be to useComparator.comparing(e -> e.getValue().getName().getFirstName())
– JB Nizet
Nov 16 '18 at 19:53
@JBNizet I wasn't aware of that function, nice!
– lucasvw
Nov 16 '18 at 19:55
I'm sure you can make the adjustment to compare both :) Maybe give it a try and if you run into trouble you can ask a new question. Glad I can help!
– lucasvw
Nov 16 '18 at 20:37
add a comment |
Your comparator is for String
objects, but map.entrySet().stream()
returns a stream of Entry<String, FullName>
objects.
As suggested by JB in the comments, you can use the static Comparator
method #comparing
to quickly build a Comparator
:
List<String> listofIds = map.entrySet().stream().sorted(
Comparator.comparing(e -> e.getValue().getName().getFirstName())
).map(Map.Entry::getKey).collect(toList());
You can take advantage of Comparator
being a functional interface like this:
List<String> listofIds = map.entrySet().stream().sorted(
(e1, e2) -> e1.getValue().getName().getFirstName().compareTo(
e2.getValue().getName().getFirstName())
).map(Map.Entry::getKey).collect(toList());
If you still want to use a Comparator
object, you could use:
private static class ValueComparator implements Comparator<Entry<String, FullName>> {
@Override
public int compare(Entry<String, FullName> e1, Entry<String, FullName>> e2) {
FullName fullName1 = e1.getValue();
FullName fullName2 = e2.getValue();
return fullName1.getName().getFirstName()
.compareTo(fullName2.getName().getFirstName());
}
}
1
An even better solution would be to useComparator.comparing(e -> e.getValue().getName().getFirstName())
– JB Nizet
Nov 16 '18 at 19:53
@JBNizet I wasn't aware of that function, nice!
– lucasvw
Nov 16 '18 at 19:55
I'm sure you can make the adjustment to compare both :) Maybe give it a try and if you run into trouble you can ask a new question. Glad I can help!
– lucasvw
Nov 16 '18 at 20:37
add a comment |
Your comparator is for String
objects, but map.entrySet().stream()
returns a stream of Entry<String, FullName>
objects.
As suggested by JB in the comments, you can use the static Comparator
method #comparing
to quickly build a Comparator
:
List<String> listofIds = map.entrySet().stream().sorted(
Comparator.comparing(e -> e.getValue().getName().getFirstName())
).map(Map.Entry::getKey).collect(toList());
You can take advantage of Comparator
being a functional interface like this:
List<String> listofIds = map.entrySet().stream().sorted(
(e1, e2) -> e1.getValue().getName().getFirstName().compareTo(
e2.getValue().getName().getFirstName())
).map(Map.Entry::getKey).collect(toList());
If you still want to use a Comparator
object, you could use:
private static class ValueComparator implements Comparator<Entry<String, FullName>> {
@Override
public int compare(Entry<String, FullName> e1, Entry<String, FullName>> e2) {
FullName fullName1 = e1.getValue();
FullName fullName2 = e2.getValue();
return fullName1.getName().getFirstName()
.compareTo(fullName2.getName().getFirstName());
}
}
Your comparator is for String
objects, but map.entrySet().stream()
returns a stream of Entry<String, FullName>
objects.
As suggested by JB in the comments, you can use the static Comparator
method #comparing
to quickly build a Comparator
:
List<String> listofIds = map.entrySet().stream().sorted(
Comparator.comparing(e -> e.getValue().getName().getFirstName())
).map(Map.Entry::getKey).collect(toList());
You can take advantage of Comparator
being a functional interface like this:
List<String> listofIds = map.entrySet().stream().sorted(
(e1, e2) -> e1.getValue().getName().getFirstName().compareTo(
e2.getValue().getName().getFirstName())
).map(Map.Entry::getKey).collect(toList());
If you still want to use a Comparator
object, you could use:
private static class ValueComparator implements Comparator<Entry<String, FullName>> {
@Override
public int compare(Entry<String, FullName> e1, Entry<String, FullName>> e2) {
FullName fullName1 = e1.getValue();
FullName fullName2 = e2.getValue();
return fullName1.getName().getFirstName()
.compareTo(fullName2.getName().getFirstName());
}
}
edited Nov 16 '18 at 19:57
answered Nov 16 '18 at 19:47
lucasvwlucasvw
532822
532822
1
An even better solution would be to useComparator.comparing(e -> e.getValue().getName().getFirstName())
– JB Nizet
Nov 16 '18 at 19:53
@JBNizet I wasn't aware of that function, nice!
– lucasvw
Nov 16 '18 at 19:55
I'm sure you can make the adjustment to compare both :) Maybe give it a try and if you run into trouble you can ask a new question. Glad I can help!
– lucasvw
Nov 16 '18 at 20:37
add a comment |
1
An even better solution would be to useComparator.comparing(e -> e.getValue().getName().getFirstName())
– JB Nizet
Nov 16 '18 at 19:53
@JBNizet I wasn't aware of that function, nice!
– lucasvw
Nov 16 '18 at 19:55
I'm sure you can make the adjustment to compare both :) Maybe give it a try and if you run into trouble you can ask a new question. Glad I can help!
– lucasvw
Nov 16 '18 at 20:37
1
1
An even better solution would be to use
Comparator.comparing(e -> e.getValue().getName().getFirstName())
– JB Nizet
Nov 16 '18 at 19:53
An even better solution would be to use
Comparator.comparing(e -> e.getValue().getName().getFirstName())
– JB Nizet
Nov 16 '18 at 19:53
@JBNizet I wasn't aware of that function, nice!
– lucasvw
Nov 16 '18 at 19:55
@JBNizet I wasn't aware of that function, nice!
– lucasvw
Nov 16 '18 at 19:55
I'm sure you can make the adjustment to compare both :) Maybe give it a try and if you run into trouble you can ask a new question. Glad I can help!
– lucasvw
Nov 16 '18 at 20:37
I'm sure you can make the adjustment to compare both :) Maybe give it a try and if you run into trouble you can ask a new question. Glad I can help!
– lucasvw
Nov 16 '18 at 20:37
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%2f53344337%2fsorting-this-mapstring-fullname-in-java%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
You want to compare Map entries together. So what you need is a Comparator<Map.Entry<String, FullName>>, not a Comparator<String>.
– JB Nizet
Nov 16 '18 at 19:45
It would be helpful to have the code for FullName since it's used in the code you do share, and actual error. For more advice on how to write a good question, see: stackoverflow.com/help/how-to-ask
– Geoffrey Wiseman
Nov 16 '18 at 19:48