Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/333.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用现有列表中字段的值创建新列表_Java_List_Collections_Field - Fatal编程技术网

Java 使用现有列表中字段的值创建新列表

Java 使用现有列表中字段的值创建新列表,java,list,collections,field,Java,List,Collections,Field,假设有一个类: class Person { String name; int age; City location; } 是否有一些库可以让我创建一个字符串列表,其中包含列表中的每个名称 一行中的人员,而不是创建一个新的列表并循环浏览另一个列表 比如: List<Person> people = getAllOfThePeople(); List<String> names = CoolLibrary.createList("name", peopl

假设有一个类:

class Person
{
   String name;
   int age;
   City location;
}
是否有一些库可以让我创建一个字符串列表,其中包含列表中的每个名称 一行中的人员,而不是创建一个新的列表并循环浏览另一个列表

比如:

List<Person> people = getAllOfThePeople();
List<String> names = CoolLibrary.createList("name", people);
List people=getAllOfThePeople();
List name=CoolLibrary.createList(“name”,people);
而不是:

List<Person> people = getAllOfThePeople();
List<String> names = new LinkedList<String>();
for(Person person : people)
{
    names.add(person.getName());
}
List people=getAllOfThePeople();
列表名称=新建LinkedList();
用于(人:人)
{
name.add(person.getName());
}
您可以将Java 8用于:

演示


或者,您可以定义一个将列表作为参数的方法,以及要为此列表的每个元素应用的函数:

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;
}

<>你可以使用库(我认为你应该使用的最终java库)做一个小技巧:< /P>
班级人员
{
字符串名;
智力年龄;
城市位置;
公共静态最终函数getName=新函数(){
公共字符串应用(个人){
返回person.name;
}
}
}
List people=getAllOfThePeople();
List name=FluentIterable.from(people.transform(Person.getName.toList();

诀窍是在
Person
类中定义
getName
公共静态
函数

如果Java本机拥有LINQ的话。。不,这是不可能的。也许Java8支持它,但在此之前肯定不支持。当然,反射是可行的,但没有内置的查询选项。看起来太具体了,但您可以使用反射和泛型编写一个,使输出强类型化。。
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;
  }

}
[Bob, Alice]
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;
}
List<String> lNames = processElements(people, p -> p.getName()); //for the names
List<Integer> lAges = processElements(people, p -> p.getAge()); //for the ages
//etc.
Map<Integer, List<Person>> byAge = people.stream()
                                         .collect(Collectors.groupingBy(Person::getAge));
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();