Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
219 views
in Technique[技术] by (71.8m points)

java - how can i get value through complex logic from list

I have filter personCountFilter=3, and have list as below:

Rate{ PersonCount:1, LOS:1}
Rate{ PersonCount:1, LOS:2}
Rate{ PersonCount:1, LOS:3}
Rate{ PersonCount:2, LOS:1}
Rate{ PersonCount:2, LOS:2}
Rate{ PersonCount:2, LOS:3}
Rate{ PersonCount:3, LOS:2}
Rate{ PersonCount:3, LOS:4}
Rate{ PersonCount:3, LOS:5}
Rate{ PersonCount:3, LOS:6}
Rate{ PersonCount:4, LOS:3}
Rate{ PersonCount:5, LOS:7}
Rate{ PersonCount:6, LOS:7}

After filter my expected:

Rate{ PersonCount:2, LOS:1}
Rate{ PersonCount:3, LOS:2}
Rate{ PersonCount:4, LOS:3}
Rate{ PersonCount:3, LOS:4}
Rate{ PersonCount:3, LOS:5}
Rate{ PersonCount:3, LOS:6}
Rate{ PersonCount:5, LOS:7}

how can I get value after grouping by LOS, and if personCount matched filter get this one, if not matched, get closest to personCountFilter, bigger personCountFilter first

I tried to use

HashSet<Rate> testSet = rates.stream()
                .collect(Collectors.collectingAndThen(
                        Collectors.toMap(Rate::getLengthOfStayCount,
                                Function.identity(),
                                (previous, current) ->
                                {
                                    return previous.getPersonCount() > 
                                           current.getPersonCount() ? previous : current;
                                }),
                        map ->
                        {
                            HashSet<Rate> set = new HashSet<>();
                            set.addAll(map.values());
                            return set;
                        }));

but it returns

Rate{ PersonCount:2, LOS:1}
Rate{ PersonCount:3, LOS:2}
Rate{ PersonCount:4, LOS:3}
Rate{ PersonCount:3, LOS:4}
Rate{ PersonCount:3, LOS:5}
Rate{ PersonCount:3, LOS:6}
Rate{ PersonCount:6, LOS:7}

Current it gets max personCount when after grouping by LOS

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I was unable to test the following code with the information you have provided in the question (learn how to make a Minimal, Complete, and Verifiable example for your next question), but I think something like this should work for you:

HashSet<Rate> testSet = rates.stream()
        .collect(Collectors.collectingAndThen(
                Collectors.toMap(Rate::getLengthOfStayCount,
                        Function.identity(),
                        (previous, current) ->
                        {
                            int diff1 = Math.abs(personCountFilter - previous.getPersonCount());
                            int diff2 = Math.abs(personCountFilter - current.getPersonCount());
                            if (diff1 > diff2) {
                                return current;
                            } else if (diff1 < diff2) {
                                return previous;
                            } else if (diff1 == diff2) {
                                return previous.getPersonCount() <= current.getPersonCount() ? current : previous;
                            }
                        }),
                map -> new HashSet<>(map.values())
                    ));

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...