VitalyT:
私が持っていると仮定
private String userId;
private String email;
private AWSRegion region;
private String total;
List<Prop> all = new ArrayList<>
all.add(new Prop("aaa", "dddd", "EU", total1));
all.add(new Prop("aaa1", "dddd", "US", tota2l));
all.add(new Prop("aaa2", "dddd", "AU", tota2l));
all.add(new Prop("aaa3", "dddd", "AU", tota3l));
all.add(new Prop("aaa3", "dddd", "EU", tota4l));....a lot of regions
私は、1行にしたいjava8
プロパティ「AWSRegion」でリストのリストを持っています
いくつかは次のように考えて....しかし、私は地域の多くを持っているので、「フィルタ述語」としてそれを実行しないように...
List<Prop> users = all.stream().filter(u -> u.getRegeion() == AWSRegion.ASIA_SIDNEY).collect(Collectors.toList());
RESULTはリストのリストでなければなりません。
LIST : {sublist1-AU , sublist2-US, sublist3-EU....,etc'}
おかげで、
彼らは次のとおりでした:
使用groupingBy
取得するMap<AWSRegion,List<Prop>>
と、その値を取得しますMap
:
Collection<List<Prop>> groups =
all.stream()
.collect(Collectors.groupingBy(Prop::getRegion))
.values();
出力があるべき場合はList
、余分なステップを追加します。
List<List<Prop>> groups = new ArrayList<>(
all.stream()
.collect(Collectors.groupingBy(Prop::getRegion))
.values());