gpt4 book ai didi

java - 如何创建特定对象变量的子列表?

转载 作者:行者123 更新时间:2023-12-01 07:32:57 27 4
gpt4 key购买 nike

如何轻松创建子列表 List<String>全部name使用 Guava 属性?

class Person {
String name;
}


List<Person> persons;

//how can I replace the following using guava `com.google.common`?
List<String> results = new ArrayList<String>();
for (Person p : persons) {
results.add(p.getName());
}

最佳答案

可以使用 Guava 的 functional idioms但是你不应该这样做。正如链接文档中所解释的:

Excessive use of Guava's functional programming idioms can lead to verbose, confusing, unreadable, and inefficient code. These are by far the most easily (and most commonly) abused parts of Guava, and when you go to preposterous lengths to make your code "a one-liner," the Guava team weeps.

那里的示例与您的用例非常匹配,并且很好地表明“功能”代码的可读性要差得多。由于它也很可能效率较低,因此在这种情况下它确实没有任何作用。

关于java - 如何创建特定对象变量的子列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15875397/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com