英文:
How get the first matching element from stream filter() in JAVA 11?
问题
如何从JAVA 11中的stream filter()中获取第一个匹配的元素?
List<Address> addresses = List.of(
new Address(1, "办公室", "AAAAA"), new Address(2, "通讯", "BBBBB"),
new Address(3, "住所", "CCCC"), new Address(4, "隔壁", "DDDD"));
如何根据优先级检索第一个匹配的地址?
addresses.stream().filter(a -> a.getType().equals("住所")).findAny()
.orElseGet(() -> addresses.stream().filter(a -> a.getType()
.equals("通讯")).findAny()
.orElseGet(() -> addresses.stream().filter(a -> a.getType()
.equals("办公室")).findAny()
.orElseGet(() -> addresses.stream().filter(a -> a.getType()
.equals("隔壁")).findAny().orElse(null))));
如何简化上述代码以根据其Type
获取Address
?
如果列表中有住所
类型,则返回Address
,否则按照以下优先级进行:通讯
,办公室
和隔壁
。
在STREAM
中做这个最好的方法是什么?
英文:
How get the first matching element from stream filter() in JAVA 11?
List<Address> addresses = List.of(
new Address(1, "Office", "AAAAA"), new Address(2, "Correspondence", "BBBBB"),
new Address(3, "Residence", "CCCC"), new Address(4, "Next Door", "DDDD"));
How to retrieve the first matching address based on the priorities?
addresses.stream().filter(a->a.getType().equals("Residence")).findAny()
.orElseGet(() -> addresses.stream().filter(a->a.getType()
.equals("Correspondence")).findAny()
.orElseGet(()->addresses.stream().filter(a->a.getType()
.equals("Correspondence")).findAny().orElseGet(null)));
How to simplify the above code to get the Address
based on it's Type
?
If the Residence
type is available in the List then return Address
, else next priorities are followed Correspondence
, Office
, and Next Door
.
What is the best way to do it in STREAM
?
答案1
得分: 4
以下是您要翻译的内容:
使用类型列表,按所需优先级的顺序,既用于筛选流,也用作您在流上使用 min()
的比较器的基础:
List<String> types = List.of("Residence", "Correspondance", "Office", "Next Door");
addresses.stream()
.filter(a -> types.contains(a.getType()))
.min(comparingInt(a -> types.indexOf(a.getType())));
通常,您会使用一个常量来表示该列表,以便不需要每次创建:
private static final List<String> ADDRESS_TYPES = List.of("Residence", "Correspondance", "Office", "Next Door");
附注: "Correspondence" 的拼写是 ...ence,而不是 ...ance。
英文:
Use a list of the types, in order of desired precedence, to both filter the stream and as the basis of a comparator you use with min()
on the stream:
List<String> types = List.of("Residence", "Correspondance", "Office", "Next Door");
addresses.stream()
.filter(a -> types.contains(a.getType()))
.min(comparingInt(a -> types.indexOf(a.getType())));
——
You would typically use a constant for the list so it didn’t need to be created every time.
private static final List<String> ADDRESS_TYPES = List.of("Residence", "Correspondance", "Office", "Next Door");
——-
p.s "Correspondence" is spelled ...ence, not ...ance
答案2
得分: 2
这是你要翻译的代码部分:
private int priority(Address addr) {
switch (addr.getType())
case "Residence":
return 1;
case "Correspondance":
return 2;
case "Office":
return 3;
case "Next Door":
return 4;
default:
return 5;
}
}
private Optional<Address> bestAddress(List<Address> addresses) {
return
addresses.stream().min(Comparator.comparingInt(this::priority));
}
如果需要更多翻译,请告诉我。
英文:
private int priority(Address addr) {
switch (addr.getType())
case "Residence":
return 1;
case "Correspondance":
return 2;
case "Office":
return 3;
case "Next Door":
return 4;
default:
return 5;
}
}
private Optional<Address> bestAddress(List<Address> addresses) {
return
addresses.stream().min(Comparator.comparingInt(this::priority));
}
Edited as per comment. Thanks.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论