lamda练习使用

package com.hema.cloudification.suandok.application.gate.exchange.http.lamda;

import com.google.common.collect.Lists;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.lang.ArrayUtils;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

import static java.util.stream.Collectors.groupingBy;

public class 员工筛选 {
    public static void main(String[] args) {
        int[] arr={1,2,3};
        Arrays.stream(ArrayUtils.remove(arr, 1)).forEach(System.out::println);
        System.out.println("----------------");
        Arrays.stream(ArrayUtils.removeElement(arr, 1)).forEach(System.out::println);

        List<Person> personList = new ArrayList<Person>();
        personList.add(new Person("Tom", 8900, 15, "male", "New York"));
        personList.add(new Person("Jack", 7000, 15, "male", "Washington"));
        personList.add(new Person("Lily", 7800, 17, "female", "Washington"));
        personList.add(new Person("Anni", 8200, 18, "female", "New York"));
        personList.add(new Person("Owen", 9500, 17, "male", "New York"));
        personList.add(new Person("Alisa", 7900, 22, "female", "New York"));

        //从员工集合中筛选出salary大于8000的员工,并放置到新的集合里。
        System.out.println(personList.stream().filter(item -> item.getSalary() > 8000).collect(Collectors.toList()));
        //统计员工的最高薪资、平均薪资、薪资之和。
        System.out.println("最大薪资1:" + personList.stream().filter(Objects::nonNull).max(Comparator.comparing(Person::getSalary)));
        System.out.println("最大薪资2:" + personList.stream().filter(Objects::nonNull).mapToInt(Person::getSalary).max());
        System.out.println("平均薪资:" + personList.stream().mapToInt(Person::getSalary).average());
        System.out.println("薪资之和:" + personList.stream().mapToInt(Person::getSalary).sum());
        //将员工按薪资从高到低排序,同样薪资者年龄小者在前。
        personList.sort(Comparator.comparing(Person::getSalary).reversed().thenComparing(Person::getAge));
        //将员工按性别分类,将员工按性别和地区分类,将员工按薪资是否高于8000分为两部分。
        System.out.println("将员工按性别分类:" + personList.stream().collect(Collectors.groupingBy(Person::getSex)));
        System.out.println("将员工按性别和地区分类:" + personList.stream().collect(groupingBy(Person::getSex, groupingBy(Person::getArea))));
        System.out.println("将员工按薪资是否高于8000分为两部分:" + personList.stream().collect(Collectors.partitioningBy(x -> x.getSalary() >= 8000)));
        System.out.println("返回所有集合的元素数量:"+personList.stream().collect(Collectors.counting()));
        System.out.println("-------");
        Arrays.stream(personList.toArray()).forEach(System.out::println);
        System.out.println(personList.toArray().getClass());
       /* //1 构造数组
        List<Employee> list = Arrays.asList(new Employee(1L, "e1"), new Employee(2L, "e2"), new Employee(3L, "e3"));

        //2 所有name收集成list。toList
        List<String> nameList = list.stream().map(x -> x.getName()).collect(Collectors.toList());
        //3 name收集成set 。toSet
        Set<String> nameSet = list.stream().map(x -> x.getName()).collect(Collectors.toSet());
        //4 list转为map
        Map<Long, String> map = list.stream().collect(Collectors.toMap(Employee::getId, Employee::getName));
        //  key重复时报错的解决
        Map<Long, String> map2 = list.stream().collect(Collectors.toMap(Employee::getId, Employee::getName, (k2, k1) -> k1));
        //5 根据id分组
        Map<Long, List<Employee>> groupByMap = list.stream().collect(Collectors.groupingBy(Employee::getId));
        //6 分区 分区是分组的特殊情况。  它最多可以分为两组,如id大于2和不大于2的
        Map<Boolean, List<Employee>> part = list.stream().collect(Collectors.partitioningBy(x -> x.getId() > 2));
        //7 name用逗号相连
        String join = list.stream().map(Employee::getName).collect(Collectors.joining(","));*/


        /**
         * 转换成集合
         */
            /* //转换成set集合
            Set<Book> collect2 = books.stream().collect(Collectors.toSet());//HashSet
            Set<Book> set = books.stream().collect(Collectors.toCollection(HashSet::new));

            //
            List<Book> collect3 = books.stream().collect(Collectors.toList());//ArrayList

            LinkedList<Book> list = books.stream().collect(Collectors.toCollection(LinkedList::new));

            ArrayList<Book> collect = books.stream().collect(Collectors.toCollection(ArrayList::new));

            CopyOnWriteArrayList<Book> collect1 = books.stream().collect(Collectors.toCollection(CopyOnWriteArrayList::new));*/

        /**
         *转换成map
         */
            /* //调用merge的时候有空值校验,如果值为null会报空指针//HashMap
            Map<String, Double> collect = books.stream().collect(Collectors.toMap(Book::getName, item -> item.getPrice() == null ? 0 : item.getPrice()));
            System.out.println("书名和价格: " + collect);

            Map<String, String> collect1 = books.stream().collect(Collectors.toMap(Book::getAuthor, Book::getName, (one, two) -> one));//HashMap
            System.out.println("作者和书名-有重复的选择第一个书名: " + collect1);

            ConcurrentHashMap<String, String> collect2 = books.stream().collect(Collectors.toMap(Book::getAuthor, Book::getName,
                    (one, two) -> one.concat(two), ConcurrentHashMap::new));
            System.out.println("作者和书名-有重复的选择第一个书名-指定返回值类型: " + collect2);
        */
        /**
         * 合并两个集合,去掉相同元素
         */
        List<String> list1 = Arrays.asList("1","2","3","4");
        List<String> list2 = Arrays.asList("3","4","5","6");
        List<List<String>> list = Arrays.asList(list1,list2);

        Set<String> collect = list.stream().
                map(item-> Lists.newArrayList(item.get(0),item.get(1),item.get(2),item.get(3)))
                .flatMap(Collection::stream)
                .collect(Collectors.toSet());


    }
}

@Data
class Person {
    private String name;  // 姓名
    private int salary; // 薪资
    private int age; // 年龄
    private String sex; //性别
    private String area;  // 地区

    // 构造方法
    public Person(String name, int salary, int age, String sex, String area) {
        this.name = name;
        this.salary = salary;
        this.age = age;
        this.sex = sex;
        this.area = area;
    }
    // 省略了get和set,请自行添加

}

@Data
class Per {
    private String sex;
    private String area;

    public Per(String sex, String area) {
        this.sex = sex;
        this.area = area;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
信息数据从传统到当代,是一直在变革当中,突如其来的互联网让传统的信息管理看到了革命性的曙光,因为传统信息管理从时效性,还是安全性,还是可操作性等各个方面来讲,遇到了互联网时代才发现能补上自古以来的短板,有效的提升管理的效率和业务水平。传统的管理模式,时间越久管理的内容越多,也需要更多的人来对数据进行整理,并且数据的汇总查询方面效率也是极其的低下,并且数据安全方面永远不会保证安全性能。结合数据内容管理的种种缺点,在互联网时代都可以得到有效的补充。结合先进的互联网技术,开发符合需求的软件,让数据内容管理不管是从录入的及时性,查看的及时性还是汇总分析的及时性,都能让正确率达到最高,管理更加的科学和便捷。本次开发的医院后台管理系统实现了病房管理、病例管理、处方管理、字典管理、公告信息管理、患者管理、药品管理、医生管理、预约医生管理、住院管理、管理员管理等功能。系统用到了关系型数据库中王者MySql作为系统的数据库,有效的对数据进行安全的存储,有效的备份,对数据可靠性方面得到了保证。并且程序也具备程序需求的所有功能,使得操作性还是安全性都大大提高,让医院后台管理系统更能从理念走到现实,确确实实的让人们提升信息处理效率。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值