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=江西)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值