java api 特性
stream只能被“消费”一次,一旦遍历过就会失效,就像容器的迭代器那样,想要再次遍历必须重新生成
map():用于映射每个元素到对应的结果。
filter():filter 方法用于通过设置的条件过滤出元素。
Collectors.toList() 用来结束Stream流
例如:
//userList User实体类对象集合
//User 实体类
//getId 实体类属性的get方法 
List<int> ids= userList.stream().map(User::getId).collect(Collectors.toList())
//或者  把数据放到map根据user.getId(条件) 循环 在转换成list
List<int> ids= userList.stream().map(user->user.getId()).collect(Collectors.toList());

//过滤list集合中属性type为1的值并赋值给permissions集合 在返回list集合 .collect(Collectors.toList()) 转换成list集合
List<Permission> permissions = list.stream().filter(l -> l.getType().equals(1))
                .collect(Collectors.toList());

list转map

Map<String,Entity> statMap = statList.stream().collect(Collectors.toMap(Entity::getId, Entity -> Entity));
        
List<String> collect = roleResultList.stream().map(AcAppRole::getName).collect(Collectors.toList());
Map<Integer, String> map = new HashMap<>();
map.put(10, "apple");
map.put(20, "orange");
map.put(30, "banana");
map.put(40, "watermelon");
map.put(50, "dragonfruit");
System.out.println("\n1. Export Map Key to List...");
List<Integer> result = map.keySet().stream().collect(Collectors.toList());
result.forEach(System.out::println);
System.out.println("\n2. Export Map Value to List...");
List<String> result2 = map.values().stream().collect(Collectors.toList());
result2.forEach(System.out::println);
System.out.println("\n3. Export Map Value to List..., say no to banana");
List<String> result3 = map.keySet().stream().filter(x -> !"banana".equalsIgnoreCase(x)).collect(Collectors.toList());
result3.forEach(System.out::println);
 
List<String> resultValues = map.entrySet().stream().sorted(Map.Entry.<Integer, String>comparingByKey().reversed())
                .peek(e -> resultSortedKey.add(e.getKey()))
                .map(x -> x.getValue())
                .filter(x -> !"banana".equalsIgnoreCase(x))
                .collect(Collectors.toList());
 
    public static void main(String args[]) {
        SqlServerReader tester = new SqlServerReader();
        tester.testCaseFormat();
    }
 
    private void testCaseFormat() {
        System.out.println(CaseFormat.LOWER_HYPHEN.to(CaseFormat.LOWER_CAMEL, "test-data"));
        System.out.println(CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, "test_data"));
        System.out.println(CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL, "test_data"));
 
        System.out.println(CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, "testdata"));
        System.out.println();
        System.out.println(CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_HYPHEN, "testData"));
    }

list转逗号分隔的字符串CollectionUtil.join

List<SysRole> roleList = sysRoleService.list(roleWrapper);
List<String> roleIdList = roleList.stream().map(SysRole::getId).collect(Collectors.toList());
List<String> roleNames = roleList.stream().map(SysRole::getRoleName).collect(Collectors.toList());
sysUser.setUserRoleNames(CollectionUtil.join(roleNames, ","));

字符串转list

List<Long> shipIdList = Arrays.stream(shipIds.split(",")).map(Long::parseLong).collect(Collectors.toList());

list根据某个对象属性去重

// 根据name去重
List<Person> unique = persons.stream().collect(
            Collectors.collectingAndThen(
                    Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(Person::getName))), ArrayList::new)
);

// 根据name,sex两个属性去重
List<Person> unique = persons.stream().collect(
           Collectors. collectingAndThen(
                    Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(o -> o.getName() + ";" + o.getSex()))), ArrayList::new)
);

List<Person> filterList = persons.stream().filter(p -> p.getSex().equals(1)).collect(Collectors.toList());

list.stream().map().collect(Collectors.toList())相关推荐

  1. java8 .stream().map().collect() 的用法

    API: https://www.runoob.com/java/java8-streams.html mylist.stream().map(myfunction->{return item; ...

  2. java8 stream().map().collect()的Collectors.toList()、Collectors.toMap()、Collectors.groupingBy()的

    一.Collectors.toList() 现在有个集合: List<User> users = getUserList(); 现在需要将这些user的id提取出来.这个很简单,for循环 ...

  3. 通俗易懂,java8 .stream().map().collect()用法

    API: https://www.runoob.com/java/java8-streams.html 模板: mylist.stream().map(myfunction->{return i ...

  4. java8 stream().map().collect()用法

    java8 stream().map().collect()用法 有一个集合: List<User> users = getList(); //从数据库查询的用户集合 现在想获取User的 ...

  5. stream().map().collect()用法

    有一个集合: List users = getList(); //从数据库查询的用户集合 现在想获取User的身份证号码:在后续的逻辑处理中要用: 常用的方法我们大家都知道,用for循环, List ...

  6. strm().filter().collect()和stream().map().collect()的作用

    在看代码的时候看到了相关方法,自己在写了个例子练习一下 public class People {private Integer id;private String name;private Bool ...

  7. Stream的特性、用法、stream().map().collect()用法

    Stream的特性.用法.stream().map().collect()用法 1.举例说明 有一个集合: List<User> users = getList(); 现在想获取User的 ...

  8. stream().map()

    Stream作为Java 8的一大特点,是对集合对象功能的增强,***.stream().map(...).collect(Collectors.toList())中,***需要是一个List类型的对 ...

  9. java8 Lambda Stream collect Collectors 常用实例

    将一个对象的集合转化成另一个对象的集合 List<OrderDetail> orderDetailList = orderDetailService.listOrderDetails(); ...

  10. java8 stream map flatMap

    集合操作stream出了以后简化了代码和增强了可读性. 今天使用map和flatMap简单记录一下. 例: public class Test {public static void main(Str ...

最新文章

  1. laravel中TokenMismatchException异常处理
  2. Target runtime Apache Tomcat v6.0 is not defined
  3. proc文件的简单读写
  4. 给定一个函数做其最佳平方逼近c语言,求函数f(x)在指定区间上对于Φ=span{1,x}最佳平方逼近多项式: (1),[1,3]; (2...
  5. POJ 1091(数论)
  6. 华硕首款平板电脑周五开售
  7. [Linux]变量加减赋值以及将String转int
  8. JS计算本周一和本周五的日期
  9. 2019.04.13 - 19:34
  10. Google 5.5亿美金投资了京东?
  11. u8系统怎么进服务器取数,u8服务器如何连接数据库
  12. sql更新语句中update set from用法
  13. 通过H5(浏览器/WebView/其他)唤起本地app
  14. 基于特征子空间的波束形成算法原理介绍及MATLAB实现
  15. IDM短信发送接口设计说明
  16. 区块链最好的编程语言是什么?
  17. 工资被倒挂,想离职很正常,但这3种情况要警惕
  18. Linux---冯诺依曼体系结构和操作系统
  19. 微软AI秀肌肉:Windows之后,用Azure收割市场
  20. kubeadm部署高可用k8s

热门文章

  1. java 随机生成姓名_java生成随机姓氏中文人名
  2. 缓存数据一致性-解决方案
  3. 企业公众号如何申请开通模板消息功能?
  4. sap代加工流程图_委外加工_SAP的两种典型委外处理方法
  5. String字符串拼接原理
  6. xh2.54母头转换为杜邦线母头
  7. sql问题导致CPU使用率100%
  8. u盘写保护,无法格式化
  9. 从辅助运动到让人开口说话,脑机接口:“你的福气还在后头!”
  10. python jinja2_Python jinja2