I have some Account model object with a list of fields, in the simple case with two fields String name and double margin
And I need to get the exact page of these accounts that exceeds the allowable limit, sorted by margin and name. Each entry must have a margin as a key, and an account as a value.
I made this minimal code example, but it seems that sorting doesn't work for me. So I port it to TreeMap, but it will require extra memory, and I hate it. You probably need to wait to fix this inside the threads and add the lost sorting by name in case of equal field
@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)); } }
source share