首页 > 技术文章 > java 8 的使用

xiaoniuniu886 2019-12-04 20:19 原文

package com.cdyfsz.autoaudit.demo;

import com.alibaba.fastjson.JSON;
import com.cdyfsz.autoaudit.dto.AutoApproveEntryOpinionDTO;
import com.cdyfsz.autoaudit.dto.OtherDocInfoDTO;
import org.apache.commons.collections.MapUtils;

import java.sql.SQLOutput;
import java.util.*;
import java.util.stream.Collectors;

public class test1 {

    public static void main(String[] strg) {

//        test1();
//        test2();
//        test3();
//        test4();
//        test5();
        test6();


        //第一种数组转list
        String[] ss ={"杭州:DDSelectField-K43RGPMS","宁波:DDSelectField-K43RGPMT","温州:DDSelectField-K43RGPMU", "合肥:DDSelectField-K43RGPN1","芜湖:TextareaField-K43RGPN2","南京:DDSelectField-K43RGPN3","无锡:DDSelectField-K43RGPN4"};

        String sssss = JSON.toJSONString(ss);
        long millis =System.currentTimeMillis();
        List<String> ls = JSON.parseArray(sssss,String.class);
        long m = System.currentTimeMillis() - millis;
        System.out.println("共计耗时多少毫秒"+m);
        System.err.println(ls );


        //第二种数组转list集合
        String[] ss1 ={"杭州:DDSelectField-K43RGPMS","宁波:DDSelectField-K43RGPMT","温州:DDSelectField-K43RGPMU", "合肥:DDSelectField-K43RGPN1","芜湖:TextareaField-K43RGPN2","南京:DDSelectField-K43RGPN3","无锡:DDSelectField-K43RGPN4"};
        long s2s =System.currentTimeMillis();
        List<String> s2 = new ArrayList<>();
        for (String s : ss1) {
            s2.add(s);
        }

        long result = System.currentTimeMillis() - s2s;
        System.err.println(s2);
        System.out.println("共计耗时多少毫秒"+result);

        List<String> s3 = new ArrayList<>();
        long s2s3 =System.currentTimeMillis();
        Arrays.stream(ss1).forEach(l1->{
            s3.add(l1);
        });
        long result1 = System.currentTimeMillis() - s2s3;
        System.err.println(s3);
        System.out.println("共计耗时多少毫秒"+result1);

    }

    /**
     * 集合转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;
    }


    /**
     * filter
     */
    public  static    void  test4(){
        ArrayList<People>  list111   =new ArrayList<>();
        List<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<People>   aa = list.stream().filter(li -> li.getAge()>= 17).collect(Collectors.toList());

        System.out.println(aa);
    }

    public static  void  test5(){
        Map<String,People>  map=new HashMap<String,People>();
        People  P1 =new  People("小佛1","男",16);
        People  P2 =new  People("小佛2","男",17);
        People  P3 =new  People("小佛3","男",18);
        map.put(P1.getName(),P1);
        map.put(P2.getName(),P2);
        map.put(P3.getName(),P3);
        map.forEach((k,v) ->{
            if (k.equals("小佛1")){
                System.out.println("111"+v.toString());
            }

            if (v.getAge()==18){
                System.out.println("2222"+v.toString());
            }

        });

    }

    public static  void  test6(){
       List<People>  ABC=new  ArrayList<People>();
        People  P1 =new  People("小佛1","男",16);
        People  P2 =new  People("小佛2","男",17);
        People  P3 =new  People("小佛3","男",18);
        ABC.add(P1);
        ABC.add(P2);
        ABC.add(P3);
        // 只获取年龄的数据
        List<Integer> bb=ABC.stream().map(People::getAge).collect(Collectors.toList());

        Integer[] bb1=ABC.stream().map(People::getAge).toArray(Integer[]::new);

        for (Integer b1:bb){
            System.out.println("1111+++"+b1);
        }

        for (int i = 0; i <bb1.length ; i++) {
            System.out.println("8888+++++"+bb1[i]);

        }



    }






}
package com.cdyfsz.autoaudit.demo;

public class People extends Object {
String name ;
String sex ;
int age ;

public People(String name, String sex, int age) {
this.name = name;
this.sex = sex;
this.age = age;
}

public String getName() {
return name;
}

public String getSex() {
return sex;
}

public int getAge() {
return age;
}

public void setName(String name) {
this.name = name;
}

public void setSex(String sex) {
this.sex = sex;
}

public void setAge(int age) {
this.age = age;
}

@Override
public String toString() {
return "People{" +
"name='" + name + '\'' +
", sex='" + sex + '\'' +
", age=" + age +
'}';
}
}

推荐阅读