//新建list

        List<User> userList = new ArrayList<>();
User user = new User();
user.setId("user-01");
user.setName("张三");
user.setAge(18); User user1 = new User();
user1.setId("user-02");
user1.setName("李四");
user1.setAge(19); User user2 = new User();
user2.setId("user-03");
user2.setName("王五");
user2.setAge(28); userList.add(user);
userList.add(user1);
userList.add(user2); //遍历list
userList.forEach(s->{
System.out.println("被遍历的list"+s.getName());
}); //list去重复
userList.stream().distinct().collect(Collector.toList()); //通过stream把list转为map
Map<String, User> collect = userList.stream().collect(Collectors.toMap(User::getId, Function.identity(), (o1, o2) -> o2));
System.out.println(collect.toString()); //利用@Builder 注解一次性给对象赋值
User userBuild =User.builder().age(1).Id("build-01").name("buildName").build(); System.out.println(userBuild.toString()); //遍历map
collect.forEach((k,v)->{
if(k.equals("user-01")){
v.setAge(100);
}
}); System.out.println("遍历之后的"+collect.toString()); //反射
StringBuffer saveIdBuffer = new StringBuffer(); collect.forEach((k,v)->{
try {
//capitalize 首字母大写
Method method = v.getClass().getMethod("get" + StringUtils.capitalize("id"));
try {
saveIdBuffer.append(method.invoke(v));
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} } catch (NoSuchMethodException e) {
e.printStackTrace();
} }); System.out.println("通过反射得到的所有id"+saveIdBuffer);

java8 list、map遍历以及反射的更多相关文章

  1. Java8中Map的遍历方式总结

    在这篇文章中,我将对Map的遍历方式做一个对比和总结,将分别从JAVA8之前和JAVA8做一个遍历方式的对比,亲测可行. public class LambdaMap { private Map< ...

  2. java的Map遍历

    java中的map遍历有多种方法,从最早的Iterator,到java5支持的foreach,再到java8 Lambda,让我们一起来看下具体的用法以及各自的优缺点 先初始化一个mappublic ...

  3. Map遍历的几种方法

    查看Map自带API map遍历方法: public static void main(String[] args) { Map<Integer,String> map = new Has ...

  4. 分页查询和分页缓存查询,List<Map<String, Object>>遍历和Map遍历

    分页查询 String sql = "返回所有符合条件记录的待分页SQL语句"; int start = (page - 1) * limit + 1; int end = pag ...

  5. java8中map的meger方法的使用

    java8中map有一个merge方法使用示例: /** * 打印出包含号码集的label的集合 * * @param args */ public static void main(String[] ...

  6. js中三个对数组操作的函数 indexOf()方法 filter筛选 forEach遍历 map遍历

     indexOf()方法  indexOf()方法返回在该数组中第一个找到的元素位置,如果它不存在则返回-1. 不使用indexOf时 var arr = ['apple','orange','pea ...

  7. map遍历的四种方式

    原文 http://blog.csdn.net/dayanxuqun/article/details/26348277 以下是map遍历的四种方式: // 一.推荐只用value的时候用,都懂的... ...

  8. 原生JS forEach()和map()遍历的区别以及兼容写法

    一.原生JS forEach()和map()遍历 共同点: 1.都是循环遍历数组中的每一项. 2.forEach() 和 map() 里面每一次执行匿名函数都支持3个参数:数组中的当前项item,当前 ...

  9. map遍历性能记录

    map遍历可以通过keySet或者entrySet方式. 性能上:entrySet略胜一筹,原因是keySet获取到key后再根据key去获取value,在查一遍,所以慢一些. keySet: //先 ...

随机推荐

  1. sqlserver 如何瞬间执行上万条数据

    核心的内容是:使用自定义表类型 第一步:创建存储过程P_T1DeclareInfo_Upload_new 参数: T1DeclareInfo_UploadPNSN_Param  类型 T1Declar ...

  2. Java SE(1)

    Java SE基础回顾 1.循环语句中的break是终止全部循环,跳出循环体:而continue是终止本次循环,跳执行下一循环 2.return语句有两个作用:返回值:结束方法的运行 3.对于java ...

  3. delphi xe10 消息操作

    //消息提醒(从手机屏幕顶部向下滑动,出现的提示消息) NotificationC: TNotificationCenter; procedure TNotificationsForm.btnSend ...

  4. 在vue中使用handsontable

    1.使用npm安装 npm install handsontable @handsontable/vue 2.定义结构 <hot-table :settings="hotSetting ...

  5. word 文献标题自动编号

    来自:word中自动编号和多级编号的使用 选中标题或段落,点击鼠标右键,在编号菜单内选择适合的自动编号样式.或者在窗口上方的“开始”选项卡中选择编号样式.如果对已选的编号样式不满意,可以照以上方法直接 ...

  6. PAT_A1020#Tree Traversals

    Source: PAT A1020 Tree Traversals (25 分) Description: Suppose that all the keys in a binary tree are ...

  7. ICPC 2018 焦作区域赛

    // 2019.10.7 练习赛 // 赛题来源:2018 ICPC 焦作区域赛 // CF链接:http://codeforces.com/gym/102028 A Xu Xiake in Hena ...

  8. Mysql 插入数据,随机事件选择

    在拼写sql的 时候,mysql字段如果需要添加当前时间可以用NOW() 函数 // String sql = ("insert into tablename(content, create ...

  9. Spring源码分析(一):从哪里开始看spring源码(系列文章基于Spring5.0)

    概述 对于大多数第一次看spring源码的人来说,都会感觉不知从哪开始看起,因为spring项目源码由多个子项目组成,如spring-beans,spring-context,spring-core, ...

  10. [转]Visual Studio 各版本下载

    原文地址:[置顶] Visual Studio 各版本下载 文件名称 文件大小 百度网盘下载 微软官方下载 Visual Studio 2015 Enterprise - 企业版 - 简体中文 3.8 ...