java api 特性
stream只能被“消费”一次,一旦遍历过就会失效,就像容器的迭代器那样,想要再次遍历必须重新生成
map():用于映射每个元素到对应的结果。
filter():filter 方法用于通过设置的条件过滤出元素。
Collectors.toList() 用来结束Stream流
例如:
//userList User实体类对象集合
//User 实体类
//getId 实体类属性的get方法
List
//或者 把数据放到map根据user.getId(条件) 循环 在转换成list
List
//过滤list集合中属性type为1的值并赋值给permissions集合 在返回list集合 .collect(Collectors.toList()) 转换成list集合
List
.collect(Collectors.toList());
list转map
Map
List
Map
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
result.forEach(System.out::println);
System.out.println("n2. Export Map Value to List...");
List
result2.forEach(System.out::println);
System.out.println("n3. Export Map Value to List..., say no to banana");
List
result3.forEach(System.out::println);
List
.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
ListroleList = sysRoleService.list(roleWrapper); List roleIdList = roleList.stream().map(SysRole::getId).collect(Collectors.toList()); List roleNames = roleList.stream().map(SysRole::getRoleName).collect(Collectors.toList()); sysUser.setUserRoleNames(CollectionUtil.join(roleNames, ","));
字符串转list
ListshipIdList = Arrays.stream(shipIds.split(",")).map(Long::parseLong).collect(Collectors.toList());
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)