Java8从对象列表中取出某个属性的列表
List<属性值类型> 属性List = 对象List.stream().map(对象::get方法()).collect(Collectors.toList());
例如:
List<Integer> idList = list.stream().map(User::getId).collect(Collectors.toList()); //或者
List<Integer> idList = list.stream().map(u -> u.getId()).collect(Collectors.toList());
举例
public class HelloWorld {
public static void main(String[] args) {
List<User> list = new ArrayList<>();
for(int i=1;i<=10;i++) {
User u = new User(i, "用户-" + i);
list.add(u);
}
//取出id列表
//List<Integer> idList = list.stream().map(User::getId).collect(Collectors.toList());
List<Integer> idList = list.stream().map(u -> u.getId()).collect(Collectors.toList());
System.out.println("id列表:" + idList);
}
private static class User{
int id;
String name;
public User(int id, String name) {
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
}
}
执行后:

Java8从对象列表中取出某个属性的列表的更多相关文章
- java8 从对象集合中取出某个字段的集合
public class FeildTest { public static void main(String[] args) { //定义list集合 List<P> list = Ar ...
- java8之list集合中取出某一属性的方法
上代码 List<User> list = new ArrayList<User>(); User user1 = new User("第一位"," ...
- js对象数组中的某属性值 拼接成字符串
js对象数组中的某属性值 拼接成字符串 var objs=[ {id:1,name:'张三'}, {id:2,name:'李四'}, {id:3,name:'王五'}, {id:4,name:'赵六' ...
- 参数对象Struts2中Action的属性接收参数
题记:写这篇博客要主是加深自己对参数对象的认识和总结实现算法时的一些验经和训教,如果有错误请指出,万分感谢. Action中三种传递并接受参数: 1. 在Action添加成员属性接受参数 例如请求的 ...
- JavaScript中将对象数组中的某个属性值,批量替换成另一个数值
原文链接 https://segmentfault.com/q/1010000010352622 希望将下列数组中的sh替换成沪,sz替换成深 var stooges = [ {label:1,val ...
- javascript 从对象数组中 按字段/属性取最大值或最小值
var array=[ { "index_id": 119, "area_id": "18335623", "name" ...
- 微信小程序:利用map方法方便获得对象数组中的特定属性值们
- Python3基础 把一个列表中内容给另外一个列表,形成两个独立的列表
镇场诗:---大梦谁觉,水月中建博客.百千磨难,才知世事无常.---今持佛语,技术无量愿学.愿尽所学,铸一良心博客.------------------------------------------ ...
- Python中如何获取类属性的列表
这篇文章主要给大家介绍了在Python中如何获取类属性的列表,文中通过示例代码介绍的很详细,相信对大家的学习或者工作具有一定的参考借鉴价值,有需要的朋友可以参考借鉴,下面来一起看看吧. 前言 最近工作 ...
随机推荐
- 监控zookeeper
[4ajr@db1 scripts]$ cat zookeeper_mode.sh #!/bin/bash mode=`echo srvr|nc 127.0.0.1 2181|awk '/Mode/{ ...
- (四)Exploring Your Cluster
The REST API Now that we have our node (and cluster) up and running, the next step is to understand ...
- C# FileSystemWatcher 并发
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threa ...
- vi/vim 使用
1. vim一共有4个模式:(linux下最好用的编辑器) 正常模式 (Normal-mode) 插入模式 (Insert-mode) 命令模式 (Command-mode) 可视模式 (Visua ...
- python 必学模块collections
包含的主要功能如下 查看collections 的源码我们可以看到其为我们封装了以下的数据结果供我们调用 __all__ = ['deque', 'defaultdict', 'namedtuple' ...
- 一、Redis-NoSQL数据库
转载:[https://blog.csdn.net/aaronthon/article/details/81714528 ] [https://www.cnblogs.com/StanleyBlogs ...
- SSH 协议的 ssh StrictHostKeyChecking
项目的SFTP用到了这个参数: @Override public PooledObject<ChannelSftp> makeObject() throws Exception { JSc ...
- Java多线程9:中断机制
一.概述 之前讲解Thread类中方法的时候,interrupt().interrupted().isInterrupted()三个方法没有讲得很清楚,只是提了一下.现在把这三个方法同一放到这里来讲, ...
- codeforces479E
Riding in a Lift CodeForces - 479E Imagine that you are in a building that has exactly n floors. You ...
- 【XSY2849】陈姚班 平面图网络流 最短路 DP
题目描述 有一个\(n\)行\(m\)列的网格图. \(S\)到第一行的每一个点都有一条单向边,容量为\(\infty\). 最后一行的每个点到\(T\)都有一条单向边,容量为\(\infty\). ...