1

I have a simple object User, with fields like below:

class User {
 int id; float size, size2; String username, nickname;
[...]
public float getPercentage() {
 return (size - size2); //
 }
}

I try to use stream from java 8, but without result. I try to sort by value which is calculated like this: (size - size2). But result is wrong, for this objects. First I create a list:

List<User> users = new ArrayList<>();
users.add(new User(1, "User","nickname", 1.5f, 1.5f));
users.add(new User(1, "User","nickname", 1.5f, 1.2f));
users.add(new User(1, "User","nickname", 1.5f, 1f));
users.add(new User(1, "User","nickname", 1.5f, 1.6f));
users.add(new User(1, "User","nickname", 1.5f, 1.3f));

My comparator looks like this:

Comparator<User> bySizeDifferent = Comparator.comparing(user -> user.getPercentage());

And the I use comparator and display result:

users.stream().sorted(bySizeDifferent).forEach(System.out::println);

After sort result looks like this (the last column is result of subtraction):

1 - User-nickname-1.5-1.5-diff- 0.0- = 0.0
1 - User-nickname-1.5-1.2-diff- 0.2999999523162842-ひく = 0.29999995
1 - User-nickname-1.5-1.0-diff- 0.5- = 0.5
1 - User-nickname-1.5-1.6-diff- -0.10000002384185791-ひく = -ひく0.100000024
1 - User-nickname-1.5-1.3-diff- 0.20000004768371582-ひく = 0.20000005

To see this output method toString() is overrided:

@Override
 public String toString() {
 return id + " - " + username + "-" + nickname + "-" + size + "-" + size2 + "-diff- " + diff + "- = " + getPercentage();
 }

Any advice how solve this?

asked Jul 1, 2015 at 15:54
5
  • Please show a short but complete program demonstrating the problem. Commented Jul 1, 2015 at 15:57
  • Agreed - I note the output is the same order as the input. Are you sure you're actually sorting anything at all? Are you even using the Comparator? We have no way to tell. Commented Jul 1, 2015 at 16:00
  • I edited question, hope that now it is more understandable. Commented Jul 1, 2015 at 16:05
  • 1
    You might need forEachOrdered instead of forEach. Commented Jul 1, 2015 at 16:09
  • @Alex no, it doesn't help. Commented Jul 1, 2015 at 16:12

1 Answer 1

4

This is what a short, complete program looks like. It puts together all the bits and pieces you provided above, and it works, and sorts as expected. I don't know what you're doing differently, because you never actually provided a short complete program, but this works:

package com.example.dcsohl;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
public class Comparator8 {
 public static class User {
 int id; float size, size2; String username, nickname;
 public User(int id, String name, String nick, float size, float size2) {
 this.id = id;
 this.username = name;
 this.nickname = nick;
 this.size = size;
 this.size2 = size2;
 }
 public float getPercentage() {
 return (size - size2);
 }
 public String toString() {
 return id + " - " + username + "-" + nickname + "-" + size + "-" + size2 + "-diff- " + getPercentage();
 }
 }
 public static void main(String...args) {
 List<User> users = new ArrayList<>();
 users.add(new User(1, "User","nickname", 1.5f, 1.5f));
 users.add(new User(1, "User","nickname", 1.5f, 1.2f));
 users.add(new User(1, "User","nickname", 1.5f, 1f));
 users.add(new User(1, "User","nickname", 1.5f, 1.6f));
 users.add(new User(1, "User","nickname", 1.5f, 1.3f));
 Comparator<User> bySizeDifferent = Comparator.comparing(user -> user.getPercentage());
 users.stream().sorted(bySizeDifferent).forEach(System.out::println);
 }
}

Here's the output:

1 - User-nickname-1.5-1.6-diff- -0.100000024
1 - User-nickname-1.5-1.5-diff- 0.0
1 - User-nickname-1.5-1.3-diff- 0.20000005
1 - User-nickname-1.5-1.2-diff- 0.29999995
1 - User-nickname-1.5-1.0-diff- 0.5
answered Jul 1, 2015 at 16:15
Sign up to request clarification or add additional context in comments.

2 Comments

Yeah, I got the same result. He must have something else going on in part of the code that isn't shown.
Exactly. I had to provide the User constructor, for example ... for all I know, he's giving size and size2 the same value by accident. And his toString() mentioned a variable, diff, that wasn't mentioned anywhere else. I ignored it in my code, but who knows what it does in the OP's code?

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.