Java8常用Lamda表达式(持续更新)

编程

package lamda;

import lombok.Builder;

import lombok.Data;

import java.util.*;

import java.util.function.Function;

import java.util.stream.Collectors;

/**

* @describe: Lamda 表达式

* @author:(willdas)

**/

public class Demo {

public static void main(String[] args) {

listToMap();

mapToList();

listSort();

}

/**

* 1、List转Map

*/

public static void listToMap() {

//(1)

List<String> stringList = new ArrayList<>();

stringList.add("1");

stringList.add("2");

stringList.add("3");

Map<String, String> stringMap = stringList.stream().collect(Collectors.toMap(k -> k, k -> k));

//{1=1, 2=2, 3=3}

System.out.println(stringMap);

//(2)

List<People> entityList = new ArrayList<>();

entityList.add(People.builder().userId(1).userAge(21).userName("张三").build());

entityList.add(People.builder().userId(2).userAge(22).userName("李四").build());

entityList.add(People.builder().userId(3).userAge(23).userName("王五").build());

Map<Integer, People> peopleMap1 = entityList.stream().collect(Collectors.toMap(People::getUserId, p -> p));

Map<Integer, People> peopleMap2 = entityList.stream().collect(Collectors.toMap(p -> p.getUserId(), p -> p));

//避免键重复

Map<Integer, People> peopleMap3 = entityList.stream().collect(Collectors.toMap(p -> p.getUserId(), Function.identity(), (k, v) -> k));

//{1=People(userId=1, userAge=21, userName=张三), 2=People(userId=2, userAge=22, userName=李四), 3=People(userId=3, userAge=23, userName=王五)}

System.out.println(peopleMap1);

System.out.println(peopleMap2);

System.out.println(peopleMap3);

Map<Integer, String> userNameMap = entityList.stream().collect(Collectors.toMap(People::getUserId, People::getUserName));

//{1=张三, 2=李四, 3=王五}

System.out.println(userNameMap);

}

/**

* 2、Map转List

*/

public static void mapToList() {

Map<String, Integer> stringMap = new HashMap<>(4, 0.75f);

stringMap.put("1", 21);

stringMap.put("2", 22);

stringMap.put("3", 23);

List<Map.Entry<String, Integer>> stringMapList = stringMap.entrySet().stream().collect(Collectors.toList());

//[1=21, 2=22, 3=23]

System.out.println(stringMapList);

List<String> stringKeyList = stringMap.keySet().stream().collect(Collectors.toList());

//[1, 2, 3]

System.out.println(stringKeyList);

List<Integer> stringValueList = stringMap.values().stream().collect(Collectors.toList());

//[21, 22, 23]

System.out.println(stringValueList);

Map<Integer, People> peopleMap = new HashMap<>(4, 0.75f);

peopleMap.put(1, People.builder().userId(1).userAge(21).userName("张三").build());

peopleMap.put(2, People.builder().userId(2).userAge(22).userName("李四").build());

peopleMap.put(3, People.builder().userId(3).userAge(23).userName("王五").build());

List<Map.Entry<Integer, People>> peopleMapList = peopleMap.entrySet().stream().collect(Collectors.toList());

//[1=People(userId=1, userAge=21, userName=张三), 2=People(userId=2, userAge=22, userName=李四), 3=People(userId=3, userAge=23, userName=王五)]

System.out.println(peopleMapList);

List<Integer> peopleKeyList = peopleMap.keySet().stream().collect(Collectors.toList());

//[1, 2, 3]

System.out.println(peopleKeyList);

List<People> peopleValueList = peopleMap.values().stream().collect(Collectors.toList());

//[People(userId=1, userAge=21, userName=张三), People(userId=2, userAge=22, userName=李四), People(userId=3, userAge=23, userName=王五)]

System.out.println(peopleValueList);

}

/**

* 3、排序

*/

public static void listSort() {

List<Integer> integerList = new ArrayList<>();

integerList.add(1);

integerList.add(3);

integerList.add(2);

//升序 [1, 2, 3]

List<Integer> integerAscSortList = integerList.stream().sorted(Comparator.comparing(k -> k)).collect(Collectors.toList());

System.out.println(integerAscSortList);

//降序 [3, 2, 1]

integerList.sort(Comparator.comparing(t -> t.toString()).reversed());

System.out.println(integerList);

//升序 [People(userId=1, userAge=21, userName=张三), People(userId=2, userAge=22, userName=李四), People(userId=3, userAge=23, userName=王五)]

List<People> peopleList = new ArrayList<>();

peopleList.add(People.builder().userId(1).userAge(21).userName("张三").build());

peopleList.add(People.builder().userId(2).userAge(22).userName("李四").build());

peopleList.add(People.builder().userId(3).userAge(23).userName("王五").build());

List<People> peopleAscSortList = peopleList.stream().sorted(Comparator.comparing(People::getUserId)).collect(Collectors.toList());

System.out.println(peopleAscSortList);

//降序 [People(userId=3, userAge=23, userName=王五), People(userId=2, userAge=22, userName=李四), People(userId=1, userAge=21, userName=张三)]

List<People> peopleDescSortList = peopleList.stream().sorted(Comparator.comparing(People::getUserId).reversed()).collect(Collectors.toList());

System.out.println(peopleDescSortList);

}

}

@Data

@Builder

class People {

private Integer userId;

private Integer userAge;

private String userName;

}

 

以上是 Java8常用Lamda表达式(持续更新) 的全部内容, 来源链接: utcz.com/z/517353.html

回到顶部