Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/google-chrome/4.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 - Fatal编程技术网

在Java中创建新列表,只提取一个字段

在Java中创建新列表,只提取一个字段,java,Java,我有以下课程: public class Person { private Integer id; private String name; private String address; ... } 在代码的某一点上,我构建了一个人员列表: List<Person> people = getPeople(); 但我想知道是否有一种方法可以做到这一点而不需要迭代 我正在使用JDK6 非常感谢您这取决于您需要如何处理列表。如果您希望它是原始列表ID的

我有以下课程:

public class Person {
    private Integer id;
    private String name;
    private String address;
    ...
}
在代码的某一点上,我构建了一个人员列表:

List<Person> people = getPeople();
但我想知道是否有一种方法可以做到这一点而不需要迭代

我正在使用JDK6


非常感谢您

这取决于您需要如何处理
列表
。如果您希望它是原始列表ID的视图,并且不能直接修改,则可以执行以下操作:

public static List<Integer> idList(final List<Person> people) {
    return new AbstractList<Integer>() {
        @Override
        public Integer get(int index) {
            return people.get(index).getId();
        }
        @Override
        public int size() {
            return people.size();
        }
    };
}
公共静态列表空闲列表(最终列表人员){
返回新的AbstractList(){
@凌驾
公共整数get(整数索引){
返回people.get(index.getId();
}
@凌驾
公共整数大小(){
返回人。大小();
}
};
}

如果需要ID的
ArrayList
,则必须进行迭代。

这取决于需要对
列表执行什么操作。如果您希望它是原始列表ID的视图,并且不能直接修改,则可以执行以下操作:

public static List<Integer> idList(final List<Person> people) {
    return new AbstractList<Integer>() {
        @Override
        public Integer get(int index) {
            return people.get(index).getId();
        }
        @Override
        public int size() {
            return people.size();
        }
    };
}
公共静态列表空闲列表(最终列表人员){
返回新的AbstractList(){
@凌驾
公共整数get(整数索引){
返回people.get(index.getId();
}
@凌驾
公共整数大小(){
返回人。大小();
}
};
}
如果需要ID的
ArrayList
,则必须进行迭代。

在Java8中:

List<Person> people = getPeople();
List<Integer> ids = people.stream()
    .map(Person::getId)
    .collect(Collectors.toList());
List people=getPeople();
List id=people.stream()
.map(Person::getId)
.collect(Collectors.toList());
编辑 对不起,我没有注意到JDK版本

对于J6+番石榴,它将是:

List<Person> people = getPeople();
List<Integer> ids = FluentIterable.from(people)
.transform(new Function<Person, Integer>() {
    @Override
    public Integer apply(Person person) {
        return person.getId();
    }
}).toList();
List people=getPeople();
列表ID=fluenterable.from(人)
.transform(新函数(){
@凌驾
公共整数应用(个人){
return person.getId();
}
}).toList();
显然,您可以将函数提取到某个静态变量,以使其更清晰

private static final Function<Person, Integer> TO_ID = new Function<Person, Integer>() {
    @Override public Integer apply(Person person) {
        return person.getId();
    }
};

public static void main(String [] args) {
    List<Person> people = getPeople();
    List<Integer> ids = FluentIterable.from(people)
            .transform(TO_ID)
            .toList();
}
private static final Function TO_ID=new Function(){
@覆盖公共整数应用(个人){
return person.getId();
}
};
公共静态void main(字符串[]args){
List people=getPeople();
列表ID=fluenterable.from(人)
.转换(到_ID)
.toList();
}
在Java8中:

List<Person> people = getPeople();
List<Integer> ids = people.stream()
    .map(Person::getId)
    .collect(Collectors.toList());
List people=getPeople();
List id=people.stream()
.map(Person::getId)
.collect(Collectors.toList());
编辑 对不起,我没有注意到JDK版本

对于J6+番石榴,它将是:

List<Person> people = getPeople();
List<Integer> ids = FluentIterable.from(people)
.transform(new Function<Person, Integer>() {
    @Override
    public Integer apply(Person person) {
        return person.getId();
    }
}).toList();
List people=getPeople();
列表ID=fluenterable.from(人)
.transform(新函数(){
@凌驾
公共整数应用(个人){
return person.getId();
}
}).toList();
显然,您可以将函数提取到某个静态变量,以使其更清晰

private static final Function<Person, Integer> TO_ID = new Function<Person, Integer>() {
    @Override public Integer apply(Person person) {
        return person.getId();
    }
};

public static void main(String [] args) {
    List<Person> people = getPeople();
    List<Integer> ids = FluentIterable.from(people)
            .transform(TO_ID)
            .toList();
}
private static final Function TO_ID=new Function(){
@覆盖公共整数应用(个人){
return person.getId();
}
};
公共静态void main(字符串[]args){
List people=getPeople();
列表ID=fluenterable.from(人)
.转换(到_ID)
.toList();
}

只是把想法扔到这里

当代码将person对象添加到列表中时,为什么不直接将id添加到列表中?这是为了让你的id列表在运行中得到维护,而你不必在人员列表中循环来创建最后的id列表


如果我没有完全理解你的问题,请告诉我,只是在这里抛砖引玉

当代码将person对象添加到列表中时,为什么不直接将id添加到列表中?这是为了让你的id列表在运行中得到维护,而你不必在人员列表中循环来创建最后的id列表


如果我没有完全理解您的问题,请告诉我,仅在JDK8中。您不能将
列表
a
映射
。您可以使用两个
List
作为键和值集,同时还可以在
n(1)
时间内通过id获取
Person
。@KevinEsche,恐怕我不能,因为我的代码中的许多其他类都使用了该列表。您也可以使用Java 6上库中的惰性集合。但是,当然,最好使用Java 8。您可以在获取“getPeople()”时创建ID列表。--仅在JDK8中。您不能将
列表
a
映射
。您可以使用两个
List
作为键和值集,同时还可以在
n(1)
时间内通过id获取
Person
。@KevinEsche,恐怕我不能,因为我的代码中的许多其他类都使用了该列表。您也可以使用Java 6上库中的惰性集合。但是,当然,最好使用Java 8。您可以在获取“getPeople()”时创建ID列表。