1.list与字符串互转
// 导入阿里的fastJson.jar<!--添加fastjson依赖--><dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>1.2.7</version></dependency>// 其中JSONObject和JSONArray均是继承JSON的方法,所以建议直接使用JSON抽象类即可// list转换为json(以下六种均可)List<User> list = new ArrayList<User>();String str = JSON.toJSON(list).toString();String str1 = JSON.toJSONString(list);String str2 = JSONArray.toJSON(list).toString();String str3 = JSONArray.toJSONString(list);String str4 = JSONObject.toJSON(list).toString();String str5 = JSONObject.toJSONString(list);// json转换为list(以下三种均可)List<User> list = JSONObject.parseArray(str, User.class);List<User> list1 = JSON.parseArray(str, User.class);List<User> list2 = JSONArray.parseArray(str, User.class);
实体类与字符串的转换与这个上面类似
String str = JSON.toJSONString(new User()); // 转字符串User user = JSON.parseObject(str, User.class); // 转实体类
2-1.list转map
lombok注解链接
// 这里的注解作用可点击上方链接@Data@Accessors(chain = true)@AllArgsConstructor@NoArgsConstructorpublic static class User{private String id;private String name;}// list转map// ::用于类与方法之间,如person -> person.getAge();可以替换成Person::getAgeList<User> userList = Lists.newArrayList(new User().setId("A").setName("张三"),new User().setId("B").setName("李四"), new User().setId("C").setName("王五"));Map<String,String> map = userList.stream().collect(Collectors.toMap(User::getId, User::getName));System.out.println(map);{A=张三, B=李四, C=王五}
其中Collectors.toMap 有三个重载方法,四个参数:
keyMapper:Key 的映射函数valueMapper:Value 的映射函数mergeFunction:当 Key 冲突时,调用的合并方法mapSupplier:Map 构造器,在需要返回特定的 Map 时使用
当然,如果希望得到 Map 的 value 为对象本身时,可以这样写:
userList.stream().collect(Collectors.toMap(User::getId, t -> t));或:userList.stream().collect(Collectors.toMap(User::getId, Function.identity()));{A=User(id=A, name=张三), B=User(id=B, name=李四), C=User(id=C, name=王五)}
如果键值重复用前面的会报错,所以加上第三个参数,如果有相同key做出处理
List<User> userList = Lists.newArrayList(new User("A","张三"),new User().setId("A").setName("李四"),new User().setId("A").setName("桃源"),new User().setId("C").setName("王五"));Map<String,String> map = userList.stream().collect(Collectors.toMap(User::getId, User::getName, (n1, n2) -> n1+","+n2));System.out.println(map);{A=张三,李四,桃源, C=王五}
第四个参数是排序,这里根据treeMap排序(根据key排序)
List<User> userList = Lists.newArrayList(new User("2","张三"),new User().setId("23").setName("李四"),new User().setId("17").setName("桃源"),new User().setId("1").setName("王五"));Map<String,String> map = userList.stream().collect(Collectors.toMap(User::getId, User::getName, (n1, n2) -> n1, TreeMap::new));System.out.println(map);{1=王五, 17=桃源, 2=张三, 23=李四}
2-2.map转list
1.通过new来获取列表
Map<Integer, String> map = new HashMap<>();map.put(10, "apple");map.put(20, "orange");map.put(30, "banana");List<Integer> result = new ArrayList(map.keySet());result.forEach(System.out::println);List<String> result2 = new ArrayList(map.values());result2.forEach(System.out::println);
2.根据流来获取列表
Map<Integer, String> map = new HashMap<>();map.put(10, "apple");map.put(20, "orange");map.put(30, "banana");List<Integer> result = map.keySet().stream().collect(Collectors.toList());List<String> result2 = map.values().stream().collect(Collectors.toList());// equalsIgnoreCase不区分大小写,filter过滤器(这里会过滤掉banana)List<String> result3 = map.values().stream().filter(x -> !"banana".equalsIgnoreCase(x)).collect(Collectors.toList());
3.list与array互转
1.list转array
// 方法一(建议):// 用法:String[] y = x.toArray(new String[0]);// 括号里面是数组长度,最好为列表的大小,如果比列表长则报错,如果比列表短则默认列表长度。User[] b = list.toArray(new User[list.size()]);// 方法二(不建议):// 先定义list大小的数组,再循环遍历listString[] strArray2 = new String[strList.size()];for (int i = 0; i < list.size(); i++) {strArray2[i] = list.get(i);}
2.array转list
// 这里有以下两种方式,有人肯定会问第二种不是画蛇添足吗?// 因为Arrays.asList返回list大小是定长的,不支持add、remove操作。而第二种相当于new了一个新的数组列表,因此支持增加和删除String[] strs={"dog","cat","cow"};List<String> listA = Arrays.asList(strs);List<String> listB = new ArrayList<>(Arrays.asList(strs));
4.map与字符串互转
方法与“1.list与字符串互转”相同,这里就只用JSON类来实现
Map<String, String> map = new HashMap<>();map.put("xiao", "zhang");// map转字符串String a = JSON.toJSONString(map);// 字符串转mapmap = JSON.parseObject(a,Map.class);
5.一个list转另一个list
两个不同对象的列表,但是有相同字段。
// 通过stream().map在map方法里面进行转换// 其中o对list1的对象,return出来的对象代表list2中的对象。// BeanUtils类的copyProperties方法是把o中与entity相同字段数据从o转到entityList<StudentEntity> list1 = new ArrayList<>();List<LessonStudentEntity> list2 = list1.stream().map(o -> {LessonStudentEntity entity = new LessonStudentEntity();BeanUtils.copyProperties(o, entity);return entity;}).collect(Collectors.toList());list2.forEach(System.out::println);
6.list其他用法(java8语法)
1.list排序(源码来自第五个链接)
1.使用年龄进行升序排序
List<StudentInfo> studentsSortName = studentList.stream().sorted(paring(StudentInfo::getAge)).collect(Collectors.toList());
2.使用年龄进行降序排序(使用reversed()方法)
List<StudentInfo> studentsSortName = studentList.stream().sorted(paring(StudentInfo::getAge).reversed()).collect(Collectors.toList());
3.使用年龄进行降序排序,年龄相同再使用身高升序排序(根据2个字段排序)
List<StudentInfo> studentsSortName = studentList.stream().sorted(paring(StudentInfo::getAge).reversed().thenComparing(StudentInfo::getHeight)).collect(Collectors.toList());
2.list循环遍历
对列表中每个对象字段进行操作
// 单行操作list.stream().forEach(o -> o.setHead("头像") );// 多行操作,用大括号list.stream().forEach(o -> {o.setHead("头像");o.setName("名称");});
3.获取list中非重复字段列表
distinct()方法是用于获取不同的(如list中有两个id为1的实体类,最终获取的collect1 列表中只有一个id为1的)
List<Long> collect1 = list.stream().map(StudentInfo::getId).distinct().collect(Collectors.toList());
4.根据某个字段去重,并对重复数据操作
例,当name相同时,去重,并且id进行相加,返回list
List<Ac> list = new ArrayList<Ac>(){{add(new Ac("A", 1));add(new Ac("A", 3));add(new Ac("B", 4));add(new Ac("B", 5));}};// 第一步:把list转为第一个map,key为name,// value为Ac类(a->a表示Ac对象)// o1和02表示的是value,这里是Ac对象,// 不懂list转map的可以看上面的list转mapMap<String, Ac> map= list.stream().collect(Collectors.toMap(Ac::getName, a -> a, (o1,o2)-> {o1.setId(o1.getId() + o2.getId());return o1;}));// 第二步:获取map的所有values,生成一个列表到result。List<Ac> result = result.values().stream().collect(Collectors.toList());System.out.println("map:"+map);System.out.println("result:"+result);
输出结果
map:{A=Ac(name=A, id=4), B=Ac(name=B, id=9)}result:[Ac(name=A, id=4), Ac(name=B, id=9)]
5.获取两个list的交集、差集、并集
List<String> list1 = new ArrayList<>();list1.add("1");list1.add("2");List<String> list2 = new ArrayList<>();list2.add("2");list1.add("3");// 交集List<String> intersection = list1.stream().filter(item -> list2.contains(item)).collect(Collectors.toList());// 差集 (list1 - list2)List<String> reduce1 = list1.stream().filter(item -> !list2.contains(item)).collect(Collectors.toList());// 差集 (list2 - list1)List<String> reduce2 = list2.stream().filter(item -> !list1.contains(item)).collect(Collectors.toList());// 并集List<String> listAll = list1.parallelStream().collect(Collectors.toList());List<String> listAll2 = list2.parallelStream().collect(Collectors.toList());listAll.addAll(listAll2);// 去重并集List<String> listAllDistinct = listAll.stream().distinct().collect(Collectors.toList());
6.交换list中两个对象位置
交换list中第1个和第2个对象的位置。
Collections.swap(list, 0, 1);
感谢以下博主的资源,附上链接:
/xialong_927/article/details/81872422
/zzzfeiyu/article/details/100640361
/miracle-luna/p/11113673.html
/lpq374606827/article/details/93203927
/codecat/p/10873757.html
/weixin_38256991/article/details/81672235