Jdk1.8新特性常用操作-列举一部分

it2023-01-08  64

JDK8集合操作

因为太多了,这里列举几种比较常用的: 1)遍历list 2)遍历list得到map 3)遍历list,根据某个key进行分组 4)遍历多个对象,合并多个对象中的明细行 5)foreach:遍历操作,无返回值 6)distrinct:去重操作 7)filter:过滤,返回filter方法体中为true的对象

首先引入guaua包和fastjson包,方便打印:

<dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>19.0</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.60</version> </dependency>

1)代码示例:

package com.test; import com.alibaba.fastjson.JSON; import com.google.common.base.Joiner; import com.sun.scenario.effect.Identity; import org.junit.Test; import java.lang.reflect.Array; import java.util.*; import java.util.function.Function; import java.util.stream.Collectors; public class JDK8Test { class A{ private String id; private String name; private List<A> lines; public List<A> getLines() { return lines; } public void setLines(List<A> lines) { this.lines = lines; } public void setId(String id) { this.id = id; } public void setName(String name) { this.name = name; } public String getId() { return id; } public String getName() { return name; } } /*** * 遍历list */ @Test public void testList(){ List<A> list = new ArrayList<>(); A a = new A(); a.setId("1"); a.setName("张三"); A a2 = new A(); a2.setId("2"); a2.setName("李四"); list.add(a); list.add(a2); List<String> idList = list.stream().map(A::getId).collect(Collectors.toList()); System.out.println(Joiner.on(",").join(idList)); //运行结果:1,2 } /*** * 遍历list分组得到map */ @Test public void testGetMap(){ List<A> list = new ArrayList<>(); A a = new A(); a.setId("1"); a.setName("张三"); A a2 = new A(); a2.setId("2"); a2.setName("李四"); list.add(a); list.add(a2); Map<String,String> idList = list.stream().collect(Collectors.toMap(A::getId,A::getName)); Map<String,A> aList = list.stream().collect(Collectors.toMap(A::getId, Function.identity())); System.out.println(idList.toString()); System.out.println(JSON.toJSONString(aList)); //运行结果: //{1=张三, 2=李四} //{"1":{"id":"1","name":"张三"},"2":{"id":"2","name":"李四"}} } /*** * 遍历list,根据id进行分组 */ @Test public void testGroupMap(){ List<A> list = new ArrayList<>(); A a = new A(); a.setId("1"); a.setName("张三"); a.setId("1"); a.setName("王五"); A a2 = new A(); a2.setId("2"); a2.setName("李四"); list.add(a); list.add(a2); Map<String,List<A>> idList = list.stream().collect(Collectors.groupingBy(A::getId)); System.out.println(JSON.toJSONString(idList)); //运行结果: //{"1":[{"id":"1","name":"王五"}],"2":[{"id":"2","name":"李四"}]} } /*** * flatmap合并用法 */ @Test public void testFlatMap(){ A a = new A(); a.setName("name1"); a.setId("1"); A a2 = new A(); a2.setName("name2"); a2.setId("2"); List<A> list1 = new ArrayList<>(); list1.add(a); list1.add(a2); A a3 = new A(); a3.setName("name3"); a3.setId("3"); A a4 = new A(); a4.setName("name4"); a4.setId("4"); List<A> list2 = new ArrayList<>(); list2.add(a3); list2.add(a4); //对象A1 A test1 = new A(); A test2 = new A(); test1.setLines(list1); test2.setLines(list2); List<A> list = new ArrayList<>(); list.add(test1); list.add(test2); List<A> allList = list.stream().map(A::getLines).flatMap(Collection::stream).collect(Collectors.toList()); System.out.println(JSON.toJSONString(allList)); //运行结果: //[{"id":"1","name":"name1"},{"id":"2","name":"name2"},{"id":"3","name":"name3"},{"id":"4","name":"name4"}] } }

当前还有很多其他的用法: 1)foreach:遍历操作,无返回值 2)distrinct:去重操作 3)filter:过滤,返回filter方法体中为true的对象

我们写一段代码来一次性演示3种用法:

@Test public void testForeach(){ A a = new A(); a.setId("1"); a.setName("1"); A a2 = new A(); a2.setId("1"); a2.setName("2"); A a3 = new A(); a3.setId("3"); a3.setName("3"); List<A> list = new ArrayList<>(); list.add(a); list.add(a2); list.add(a3); //foreach操作: list.forEach(item->{ System.out.println(item.getName()); }); //distinct操作: List<String> idList = list.stream().map(A::getId).distinct().collect(Collectors.toList()); System.out.println(Joiner.on(",").join(idList)); //filter操作: List<A> aList = list.stream().filter(item->!"1".equals(item.getId())).collect(Collectors.toList()); System.out.println(JSON.toJSONString(aList)); }

运行结果:

1 2 3 1,3 [{"id":"3","name":"3"}]

只记录一点用法吧,很多其他用法都是想通的;

最新回复(0)