1. 程式人生 > 其它 >Java8 stream流的操作 map和list轉換

Java8 stream流的操作 map和list轉換

技術標籤:jdk1.8java

                             Java8 stream流的操作

1. 將map的所有value轉換成list

  //建立map並塞兩個值
    Map<String, String> userObjHashMap = new HashMap<>();
    userObjHashMap.put("user1","hello");
    userObjHashMap.put("user2","尛");
    //將map的所有value放入到list集合中
List<String> resultDataList = userObjHashMap.values().stream().collect(Collectors.toList()); System.out.println(resultDataList);

執行結果:
在這裡插入圖片描述

2. 將物件list根據某一欄位轉換為map型別 格式:map<Integer,object>

//將list轉換為map (key:tid,value:TestA物件) 注意:如果key一樣的話會報錯,所以儘量使用唯一的欄位當作key

//第一種方式
Map<Integer,
TestA>
testMap = testAList.stream().collect(Collectors.toMap(TestA::getTid, x -> x)); //第二種方式 Map<Integer, TestA> testMap2 = testAList.stream().collect(Collectors.toMap(TestA::getTid, Function.identity()));

測試程式碼:根據tid作為key,TestA物件為value(tid:TestA),map<Integer,TestA>

public static
void main(String[] args) { //建立testA物件並賦值 TestA test1 = new TestA(); test1.setTid(1); test1.setTname("小明"); //建立testA物件並賦值 TestA test2 = new TestA(); test2.setTid(2); test2.setTname("小紅"); //testA的集合 List<TestA> testAList = new ArrayList<>(); testAList.add(test1); testAList.add(test2); //將list轉換為map (key:tid,value:TestA物件) Map<Integer, TestA> testMap = testAList.stream().collect(Collectors.toMap(TestA::getTid, x -> x)); System.out.println("第一種方法: " + testMap); Map<Integer, TestA> testMap2 = testAList.stream().collect(Collectors.toMap(TestA::getTid, Function.identity())); System.out.println("第二種方法: " +testMap2); }

測試結果:
在這裡插入圖片描述

3. 將物件list根據某兩個欄位轉換為map型別 格式:map<Integer,String>

//將list轉換為map (key:tid,value:tname) 注意:如果key一樣的話會報錯,所以儘量使用唯一的欄位當作key

//第一種方法
Map<Integer, String> testMap = testAList.stream().collect(Collectors.toMap(TestA::getTid, x -> x.getTname()));

//第二種方法
Map<Integer, String> testMap2 = testAList.stream().collect(Collectors.toMap(TestA::getTid,TestA::getTname));

測試程式碼:根據tid作為key,tname為value,map<Integer,String>

public static void main(String[] args) {
    //建立testA物件並賦值
    TestA test1 = new TestA();
    test1.setTid(1);
    test1.setTname("小明");
    //建立testA物件並賦值
    TestA test2 = new TestA();
    test2.setTid(2);
    test2.setTname("小紅");

    //testA的集合
    List<TestA> testAList = new ArrayList<>();
    testAList.add(test1);
    testAList.add(test2);

    //將list轉換為map (key:tid,value:tname)
    Map<Integer, String> testMap = testAList.stream().collect(Collectors.toMap(TestA::getTid, x -> x.getTname()));
    System.out.println("第一種方法: " + testMap);

    Map<Integer, String> testMap2 = testAList.stream().collect(Collectors.toMap(TestA::getTid,TestA::getTname));
    System.out.println("第二種方法: " +testMap2);
}

測試結果:
在這裡插入圖片描述

4. 將物件list根據某一欄位進行分組,轉換為map型別 格式:map<Integer,List>

//將list分組轉換為map (key:tid,value:TestA物件List)
Map<Integer, List<TestA>> testMap = testAList.stream().collect(Collectors.groupingBy(TestA::getTid));

測試程式碼: 建立TestA物件 根據tid進行分組

public static void main(String[] args) {
    //建立testA物件並賦值
    TestA test1 = new TestA();
    test1.setTid(1);
    test1.setTname("小明");
    //建立testA物件並賦值
    TestA test2 = new TestA();
    test2.setTid(1);
    test2.setTname("小紅");

    //testA的集合
    List<TestA> testAList = new ArrayList<>();
    testAList.add(test1);
    testAList.add(test2);

    //將list轉換為map (key:tid,value:TestA物件)
    Map<Integer, List<TestA>> testMap = testAList.stream().collect(Collectors.groupingBy(TestA::getTid));
    System.out.println(testMap);
}

測試結果:
在這裡插入圖片描述

5. 將物件list根據某一欄位進行分組,計算每組年齡的和 格式:map<Integer,Double>

 Map<Integer, Double> tradeAmountMap = testAList.stream().collect(Collectors.groupingBy(p -> p.getTid(), Collectors.summingDouble(p -> p.age)));

測試程式碼:根據tid作為key,tname為value,map<Integer,String>


public static void main(String[] args) {
    //建立testA物件並賦值
    TestA test1 = new TestA();
    test1.setTid(1);
    test1.setAge(10.55);

    //建立testA物件並賦值
    TestA test2 = new TestA();
    test2.setTid(1);
    test2.setAge(20.5);

    //建立testA物件並賦值
    TestA test3 = new TestA();
    test3.setTid(2);
    test3.setAge(10.0);
    //testA的集合
    List<TestA> testAList = new ArrayList<>();
    testAList.add(test1);
    testAList.add(test2);
    testAList.add(test3);

    Map<Integer, Double> tradeAmountMap = testAList.stream().collect(Collectors.groupingBy(p -> p.getTid(), Collectors.summingDouble(p -> p.age)));
    System.out.println(tradeAmountMap);
}

測試結果:
在這裡插入圖片描述

6.物件list求某一個欄位的最小值

 int sumAge = userList.stream().mapToInt(UserObj::getAge).min().getAsInt();

測試程式碼:

 //建立list物件
    UserObj userObj = new UserObj(1,"20",55);
    UserObj userObj2 = new UserObj(1,"20",66);
    UserObj userObj3 = new UserObj(1,"20",30);
    List<UserObj> userList = new ArrayList<>();
    userList.add(userObj);
    userList.add(userObj2);
    userList.add(userObj3);
    //物件list求某一個欄位的最小值
    int sumAge = userList.stream().mapToInt(UserObj::getAge).min().getAsInt();
    System.out.println(sumAge);

測試結果:
在這裡插入圖片描述

7.物件map 獲取根據key排序後的map

stringMap.entrySet().stream().sorted(Map.Entry.<Integer, String>comparingByKey()).forEachOrdered(e -> testMap.put(e.getKey(), e.getValue()));

測試程式碼:

    Map<Integer, String> stringMap = new HashMap<>();
    stringMap.put(5,"小明");
    stringMap.put(3,"小明2");
    stringMap.put(7,"小明3");
    stringMap.put(6,"小明4");
    //物件list求某一個欄位的最小值
    Map<Integer, String> testMap = new LinkedHashMap();
    stringMap.entrySet().stream().sorted(Map.Entry.<Integer, String>comparingByKey()).forEachOrdered(e -> testMap.put(e.getKey(), e.getValue()));
    System.out.println(testMap);

測試結果:
在這裡插入圖片描述

8.對list元素做操作 轉為List 轉為List

  //對list元素做操作 轉為List<String> 轉為List<BigDecimal>
  //第一種方法
    List<BigDecimal> bigDecimalList = stringList.stream().map(p -> {
      return "".equals(p) ? null : new BigDecimal(p);
    }).collect(Collectors.toList());
	//第二種方法
	List<BigDecimal> idsList2 = stringIdList.stream().map(p -> "".equals(p) ? null : new BigDecimal(p)).collect(Collectors.toList());

測試程式碼:

  List<String> stringList = new ArrayList<>();
    stringList.add("1");
    stringList.add("2");
    stringList.add("3");
    stringList.add("5");
    //對list元素做操作 轉為List<String> 轉為List<BigDecimal>
    List<BigDecimal> bigDecimalList = stringList.stream().map(p -> {
      return "".equals(p) ? null : new BigDecimal(p);
    }).collect(Collectors.toList());
    //第二種方法
    List<BigDecimal> idsList2 = stringList.stream().map(p -> "".equals(p) ? null : new BigDecimal(p)).collect(Collectors.toList());
    System.out.println("第一種方法 " + bigDecimalList);
    System.out.println("第二種方法 " + idsList2);

測試結果:
在這裡插入圖片描述

物件list 取某一欄位轉換為用逗號分隔的字串如 [{id:1,name:‘阿鬆大’},{id:2,name:‘阿鬆大’},{id:3,name:‘阿鬆大’}] 取出id的字串 1,2,3

    String testUserStr = userList.stream().map(UserObj::getTid).map(String::valueOf).collect(Collectors.joining(","));

測試程式碼:

//建立list物件
    UserObj userObj = new UserObj(1,"20",55);
    UserObj userObj2 = new UserObj(5,"20",66);
    UserObj userObj3 = new UserObj(10,"20",30);
    //建立集合
    List<UserObj> userList = new ArrayList<>();
    userList.add(userObj);
    userList.add(userObj2);
    userList.add(userObj3);
    String testUserStr = userList.stream().map(UserObj::getTid).map(String::valueOf).collect(Collectors.joining(","));
    System.out.println(testUserStr);

測試結果:
在這裡插入圖片描述