Java 使用现有列表中字段的值创建一个新列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20362373/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Create a new list with values from fields from existing list
提问by Nick Vikeras
Let's say there is a class:
假设有一个类:
class Person
{
String name;
int age;
City location;
}
Is there some library that will let me create a list of Strings containing each name from the list of persons in one line instead of creating a new list and looping through the other list?
是否有一些库可以让我创建一个字符串列表,其中包含一行中人员列表中的每个名称,而不是创建一个新列表并循环遍历另一个列表?
Something like:
就像是:
List<Person> people = getAllOfThePeople();
List<String> names = CoolLibrary.createList("name", people);
Rather than:
而不是:
List<Person> people = getAllOfThePeople();
List<String> names = new LinkedList<String>();
for(Person person : people)
{
names.add(person.getName());
}
采纳答案by Alexis C.
You can using Java 8 with lambda expressions:
您可以将 Java 8 与lambda 表达式一起使用:
List<String> listNames = people.stream().map(u -> u.getName()).collect(Collectors.toList());
import java.util.*;
import java.util.function.*;
import java.util.stream.*;
public class Test {
public static void main(String args[]){
List<Person> people = Arrays.asList(new Person("Bob",25,"Geneva"),new Person("Alice",27,"Paris"));
List<String> listNames = people.stream().map(u -> u.getName()).collect(Collectors.toList());
System.out.println(listNames);
}
}
class Person
{
private String name;
private int age;
private String location;
public Person(String name, int age, String location){
this.name = name;
this.age = age;
this.location = location;
}
public String getName(){
return this.name;
}
}
Output :
输出 :
[Bob, Alice]
Demo here.
演示在这里。
或者,您可以定义一个方法,该方法将您的列表作为参数以及您要应用于此列表的每个元素的函数:
public static <X, Y> List<Y> processElements(Iterable<X> source, Function <X, Y> mapper) {
List<Y> l = new ArrayList<>();
for (X p : source)
l.add(mapper.apply(p));
return l;
}
Then just do :
然后就做:
List<String> lNames = processElements(people, p -> p.getName()); //for the names
List<Integer> lAges = processElements(people, p -> p.getAge()); //for the ages
//etc.
If you want to group people by age, the Collectors
class provide nice utilities (example):
如果您想按年龄对人进行分组,Collectors
该类提供了很好的实用程序(示例):
Map<Integer, List<Person>> byAge = people.stream()
.collect(Collectors.groupingBy(Person::getAge));
回答by siledh
You can do a little trick using Guavalibrary (which I consider the ultimate Java librarythat you should be using anyway):
您可以使用Guava库(我认为您应该使用的最终 Java 库)来做一个小技巧:
class Person
{
String name;
int age;
City location;
public static final Function<Person, String> getName = new Function<Person, String>() {
public String apply(Person person) {
return person.name;
}
}
}
List<Person> people = getAllOfThePeople();
List<String> names = FluentIterable.from(people).transform(Person.getName).toList();
The trick is defining the getName
public static Function
in the Person
class.
诀窍是在类中定义getName
公共静态。Function
Person