如何在Java 8中使用stream()进行排序,两次浮动减法?

时间:2015-07-01 15:54:14

标签: java sorting java-8

我有一个简单的对象User,其字段如下:

class User {
    int id; float size, size2; String username, nickname;

[...]
public float getPercentage() {
             return (size - size2); //
         }
}

我尝试使用java 8中的stream,但没有结果。我尝试按如下计算的值排序:(size - size2)。但对于这个对象,结果是错误的。首先,我创建一个列表:

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);

排序后的结果如下所示(最后一列是减法的结果):

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

要查看此输出方法toString()被覆盖:

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

有任何建议如何解决这个问题?

1 个答案:

答案 0 :(得分:4)

这是一个简短,完整的程序。它将您在上面提供的所有部分组合在一起,并且它可以工作,并按预期排序。我不知道你做了什么不同的事情,因为你从来没有真正提供过一个简短的完整程序,但这有效:

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);
    }

}

这是输出:

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