有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

如何使用Java8流获得按耦合条件排序的分页地图

我有一些带有字段列表的模型对象Account,在简单的情况下有两个字段String namedouble margin

我需要得到这个账户的确切页面,它超过了一定的保证金限制,按保证金和保证金排序;名称每个记录都应该以保证金为关键,以账户为价值

我制作了这个最小的代码示例,但排序似乎对我来说不太合适。所以我用树形图把它包起来,但它会消耗额外的内存,我讨厌它。 可能应该等待在流内部修复此问题,并按名称添加丢失的排序,以防边缘相等

@Data
class Account {
    private final String name;
    private final double margin;

    private final static double MARGIN_LIMIT = 100;
    private final static int PAGE_SIZE = 3;

    private static Set<Account> accounts = new HashSet<>();

    public static void main(String[] args) {
        accounts.add(new Account("user1", 200));
        accounts.add(new Account("user2", 100));
        accounts.add(new Account("user3", 150));
        accounts.add(new Account("user4", 175));
        accounts.add(new Account("user5", 75));
        accounts.add(new Account("user6", 110));

        Map<Double,Account> val = new TreeMap<Double,Account>(Comparator.reverseOrder());
        val.putAll(getClientsForClosing(2));
        System.out.println(val);
    }

    private static Map<Double, Account> getClientsForClosing(int page) {
        return accounts.stream()
                .filter(account -> account.getMargin() >= MARGIN_LIMIT)
                .sorted(Comparator.comparingDouble(Account::getMargin).reversed())
                .skip(PAGE_SIZE * (page - 1))
                .limit(PAGE_SIZE)
                .collect(Collectors.toMap(Account::getMargin, o -> o));
    }
}

共 (2) 个答案

  1. # 1 楼答案

    其他答案是正确的,但我认为TreeSet(而不是TreeMap)可能对您有很大帮助:

    TreeSet<Account> accounts = new TreeSet<>(
        Comparator.comparingDouble(Account::getMargin).reversed()
            .thenComparing(Account::getName));
    
    // Then add all the accounts
    
    Map<Double, List<Account>> result = accounts
        .headSet(new Account("zzzz", MARGIN_LIMIT)) // zzzz is the greatest string 
        .stream()                                   // I could think of at this time
        .skip(PAGE_SIZE * (page - 1))               // (it's quite late here)
        .limit(PAGE_SIZE)
        .collect(Collectors.groupingBy(Account::getMargin));
    

    这个解决方案使用^{}方法,它就像手套一样适合你要做的事情。然后,我们转向流来跳过和限制元素,并最终收集到所需的数据结构

  2. # 2 楼答案

    您的问题和解决方案在某种程度上是矛盾的,一方面,您显示了按margin对条目排序的代码,这是一个double;在这种情况下,有一种更简单的方法:

    accounts.stream()
            .filter(account -> account.getMargin() >= MARGIN_LIMIT)
            .skip(PAGE_SIZE * (page - 1))
            .limit(PAGE_SIZE)
            .collect(Collectors.groupingBy(
                   Account::getMargin,
                   () -> new TreeMap<Double, List<Account>>(Comparator.reverseOrder()),
                   Collectors.toList()));
    

    如果您想同时按marginname进行排序,并且仍然将定义保留为Map<Double, List<Account>>,则必须使用LinkedHashMap

    accounts.stream()
            .filter(account -> account.getMargin() >= MARGIN_LIMIT)
            .sorted(Comparator.comparingDouble(Account::getMargin)
                              .reversed()
                              .thenComparing(Comparator.comparing(Account::getName)))
            .skip(PAGE_SIZE * (page - 1))
            .limit(PAGE_SIZE)
            .collect(Collectors.toMap(
                  Account::getMargin,
                  x -> {
                     List<Account> list = new ArrayList<>();
                     list.add(x);
                     return list;
                  },
                  (left, right) -> {
                      left.addAll(right);
                      return left;
                  },
                  LinkedHashMap::new));