java8中的一些lambda操作

先創建兩個實體類

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.math.BigDecimal;

/**
 * @author 宮崎不駿
 * @className Person
 * @Version 1.0
 * @Description: TODO
 * @date 2020/1/2120:41
 */
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class Item  {

    private String name;

    private int qty;

    private BigDecimal price;

}

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * @author 宮崎不駿
 * @className Person
 * @Version 1.0
 * @Description: TODO
 * @date 2020/1/2215:27
 */
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class Person {
    private int id;
    private String name;
    private String address;
}

FilterMap操作

import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author 宮崎不駿
 * @className ExampleFilterMap
 * @Version 1.0
 * @Description: FilterMap操作
 * @date 2020/1/2215:54
 */
public class ExampleFilterMap {

    private static Map<Integer,String> map = new HashMap<>();

    static {
        map.put(1,"熊大");
        map.put(2,"熊二");
        map.put(3,"光頭強");
        map.put(4,"吉吉國王");
        map.put(5,"土坡鼠");
    }
    public static void main(String[] args){
        // java8 之前操作 map
        String result = null;
        for (Map.Entry<Integer,String> entry:map.entrySet()){
            if ("熊大".equals(entry.getValue())){
                result = entry.getValue();
            }
        }
        System.out.println("Before java 8 :"+result);

        //java8 map->stream->filter->string
        result = map.entrySet().stream().filter(map->"熊二".equals(map.getValue()))
                .map(map->map.getValue())
                .collect(Collectors.joining());
        System.out.println("After java 8 "+result);

        Map<Integer,String> collect = map.entrySet().stream().filter(c->c.getKey()==3)
                .collect(Collectors.toMap(p->p.getKey(),p->p.getValue()));
        System.out.println(collect);
    }
}

控制檯輸出

Before java 8 :熊大
After java 8 熊二
{3=光頭強}

List操作

import java.util.ArrayList;
import java.util.List;

/**
 * @author 宮崎不駿
 * @className ExampleList
 * @Version 1.0
 * @Description: List操作
 * @date 2020/1/2120:09
 */
public class ExampleList {
    private static List<String> items = new ArrayList<>();

    static {
        items.add("A");
        items.add("BC");
        items.add("C");
        items.add("BD");
        items.add("E");
    }
    public static void main (String[] args){
        //java8之前操作list
        for (String item:items){
            System.out.println(item);

        }
        System.out.println("-------------");
        //java8 lambda遍歷list
        items.forEach(c-> System.out.println(c));

        System.out.println("-------------");
        items.forEach(item->{
            if ("C".equals(item)){
                System.out.println(item);
            }
        });

        System.out.println("-------------");

        //先過濾
        //stream() − 爲items集合創建串行流。過濾是否包含“B”的
        items.stream().filter(s->s.contains("B")).forEach(c1-> System.out.println(c1));
    }
}

控制檯輸出

A
BC
C
BD
E
-------------
A
BC
C
BD
E
-------------
C
-------------
BC
BD

Map操作

import java.util.HashMap;
import java.util.Map;

/**
 * @author 宮崎不駿
 * @className ExampleMap
 * @Version 1.0
 * @Description: Map操作
 * @date 2020/1/2120:26
 */
public class ExampleMap {

    private static Map<String, Integer> items = new HashMap<>();

    static {
        items.put("A", 10);
        items.put("B", 20);
        items.put("C", 30);
        items.put("D", 40);
        items.put("E", 50);
        items.put("F", 60);
        items.put("G", 70);
        items.put("H", 80);
    }

    public static void main(String[] args){
        //java8 之前遍歷是這樣遍歷map
        for (Map.Entry<String,Integer> entry:items.entrySet()){
            System.out.println("key:" + entry.getKey() +  " value:" + entry.getValue());
        }
        System.out.println("---------------------");

        //java8遍歷map
        items.forEach((key,value)-> System.out.println("key:" + key + " value:" + value));
    }
}

控制檯輸出

key:A value:10
key:B value:20
key:C value:30
key:D value:40
key:E value:50
key:F value:60
key:G value:70
key:H value:80
---------------------
key:A value:10
key:B value:20
key:C value:30
key:D value:40
key:E value:50
key:F value:60
key:G value:70
key:H value:80

Groupingby操作

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author 宮崎不駿
 * @className ExampleMapping
 * @Version 1.0
 * @Description: Groupingby操作
 * @date 2020/1/2210:39
 */
public class ExampleMapping {

    public static void main(String[] args) {

        List<Item> items = Arrays.asList(
                new Item("apple", 10, new BigDecimal(23.5)),
                new Item("apple", 20, new BigDecimal(32.5)),
                new Item("orange", 30, new BigDecimal(13.9)),
                new Item("orange", 20, new BigDecimal(33.5)),
                new Item("orange", 10, new BigDecimal(63.5)),
                new Item("orange", 50, new BigDecimal(41.5)),
                new Item("peach", 20, new BigDecimal(26.5)),
                new Item("peach", 30, new BigDecimal(42.5)),
                new Item("peach", 40, new BigDecimal(24.5)),
                new Item("peach", 10, new BigDecimal(12.5))
        );

        // 分組,計數
        Map<String, Long> counting = items.stream()
                .collect(Collectors.groupingBy(Item::getName, Collectors.counting()));
        System.out.println("counting:"+counting);

        // 分組,計數,數量
        Map<String, Integer> sum = items.stream()
                .collect(Collectors.groupingBy(Item::getName, Collectors.summingInt(Item::getQty)));
        System.out.println("sum:"+sum);

        //分組,計數,金額
        Map<BigDecimal,List<Item>> groupByPriceMap = items.stream().collect(Collectors.groupingBy(Item::getPrice));
        System.out.println("groupByPriceMap:"+groupByPriceMap);

        //分組 轉化list ->set
        Map<BigDecimal, Set<String>> result = items.stream().collect(Collectors.groupingBy(Item::getPrice,Collectors
                .mapping(Item::getName,Collectors.toSet())));
        System.out.println("result:"+result);
    }
}

控制檯輸出

counting:{orange=4, apple=2, peach=4}
sum:{orange=110, apple=30, peach=100}
groupByPriceMap:{41.5=[Item(name=orange, qty=50, price=41.5)], 33.5=[Item(name=orange, qty=20, price=33.5)], 12.5=[Item(name=peach, qty=10, price=12.5)], 63.5=[Item(name=orange, qty=10, price=63.5)], 23.5=[Item(name=apple, qty=10, price=23.5)], 42.5=[Item(name=peach, qty=30, price=42.5)], 26.5=[Item(name=peach, qty=20, price=26.5)], 24.5=[Item(name=peach, qty=40, price=24.5)], 32.5=[Item(name=apple, qty=20, price=32.5)], 13.9000000000000003552713678800500929355621337890625=[Item(name=orange, qty=30, price=13.9000000000000003552713678800500929355621337890625)]}
result:{41.5=[orange], 33.5=[orange], 12.5=[peach], 63.5=[orange], 23.5=[apple], 42.5=[peach], 26.5=[peach], 24.5=[peach], 32.5=[apple], 13.9000000000000003552713678800500929355621337890625=[orange]}


Groupingby操作

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author 宮崎不駿
 * @className ExampleTwoMapping
 * @Version 1.0
 * @Description: Groupingby操作
 * @date 2020/1/2215:28
 */
public class ExampleTwoMapping {
    private static List<Person> personList = new ArrayList<>();

    static {
        personList.add(Person.builder().id(10).name("張三瘋").address("河南").build());
        personList.add(Person.builder().id(11).name("楊過").address("湖北").build());
        personList.add(Person.builder().id(12).name("小龍女").address("江西").build());

    }

    public static void main(String[] args) {
        //分組
        Map<String, List<Person>> collect = personList.stream().collect(Collectors.groupingBy(c -> c.getAddress()));
        System.out.println("collect:" + collect);

        System.out.println("--------分割線-------");
        //java8  list轉換map
        Map<Integer, Person> map_ = personList.stream().collect(Collectors.toMap((key -> key.getId()), (value -> value)));
        map_.forEach((key, value) -> System.out.println(key + ":" + value));

    }
}

控制檯輸出

collect:{河南=[Person(id=10, name=張三瘋, address=河南)], 湖北=[Person(id=11, name=楊過, address=湖北)], 江西=[Person(id=12, name=小龍女, address=江西)]}
--------分割線-------
10:Person(id=10, name=張三瘋, address=河南)
11:Person(id=11, name=楊過, address=湖北)
12:Person(id=12, name=小龍女, address=江西)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章