java 8 的使用

package com.cdyfsz.autoaudit.demo;

import com.cdyfsz.autoaudit.dto.AutoApproveEntryOpinionDTO;
import org.apache.commons.collections.MapUtils;

import java.sql.SQLOutput;
import java.util.ArrayList;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

public class test1 {

    public static void main(String[] strg) {

//        test1();
//        test2();
        test3();
    }

    /**
     * 集合转map
     */
    public static void test1() {
        ArrayList<People>  list   =new ArrayList<>();
        People  P1 =new  People("小佛1","男",16);
        People  P2 =new  People("小佛2","男",17);
        People  P3 =new  People("小佛3","男",18);

        list.add(P1);
        list.add(P2);
        list.add(P3);

        // 用 对象的一个集合结果集,转成map ,以 对象的名字为key ,整个对象为 value 的 转型
        Map<String ,People>  map=list.stream().collect(Collectors.toMap(People::getName,People -> People));
        System.out.println(map);
    }

    /**
     * 遍历
      */
    public   static   void  test2(){
        ArrayList<People>  list   =new ArrayList<>();
        People  P1 =new  People("小佛1","男",16);
        People  P2 =new  People("小佛2","男",17);
        People  P3 =new  People("小佛3","男",18);

        list.add(P1);
        list.add(P2);
        list.add(P3);

        list.forEach(li -> {
            String name = li.getName();
            Integer age = li.getAge();
            String sex = li.getSex();
            System.out.println(name +age + sex );
        });

    }

    /**
     * 双重 循环来 判断
     */

    public   static   void   test3(){
        ArrayList<People>  list   =new ArrayList<>();
        People  P1 =new  People("小佛1","男",16);
        People  P2 =new  People("小佛2","男",17);
        People  P3 =new  People("小佛3","男",18);

        list.add(P1);
        list.add(P2);
        list.add(P3);
        ArrayList<People>  list1   =new ArrayList<>();
        People  P11 =new  People("小佛1","男",16);
        People  P22 =new  People("小佛21","男",17);
        People  P33 =new  People("小佛31","男",18);

        list1.add(P11);
        list1.add(P22);
        list1.add(P33);
        list.forEach(li1 -> {
            list1.forEach( li2 -> {
                if (li1.getName().equals(li2.getName())){
                    System.out.println("就是你了");
                    return;
                }

            });
        });

        for (People pe:list){
           String PeName = pe.getName();
           for (People pe1:list1){
               if (PeName.equals(pe1.getName())){
                   System.out.println("就是你了1111");
                   break;
               }
           }
        }
    }

    /**
     * 获取map中第一个key值
     *
     * @param map 数据源
     * @return
     */
    private Object getValueOrNull(Map<String, Object> map) {
        Object obj = null;
        Set<Map.Entry<String, Object>> entries = map.entrySet();
        for (Map.Entry<String, Object> entry : entries) {
            obj = entry.getValue();
            if (obj != null) {
                break;
            }
        }
        return obj;
    }


}

...

猜你喜欢

转载自www.cnblogs.com/xiaowoniulx/p/11985286.html