Java对List集合元素查找filter(),排序Comparator,判断是否存在,分组groupingBy应用

之前写C#的时候,对集合的操作有linq和lamda表达式,使用起来非常方便。现在用java
实现对集合的类似操作。
1、数据初始化

  • Studnet
package com.atguigu.test;

import java.util.Objects;

public class Student implements Comparable{
    private String stuName;
    private int stuAge;
    private String stuGender;
    private double stuHeight;

    public Student() {
    }

    public Student(String stuName, int stuAge, String stuGender,double stuHeight) {
        this.stuName = stuName;
        this.stuAge = stuAge;
        this.stuGender = stuGender;
        this.stuHeight=stuHeight;
    }

    public String getStuName() {
        return stuName;
    }

    public void setStuName(String stuName) {
        this.stuName = stuName;
    }

    public int getStuAge() {
        return stuAge;
    }

    public void setStuAge(int stuAge) {
        this.stuAge = stuAge;
    }

    public String getStuGender() {
        return stuGender;
    }

    public double getStuHeight() {
        return stuHeight;
    }

    public void setStuGender(String stuGender) {
        this.stuGender = stuGender;
    }

    @Override
    public String toString() {
        return "Student{" +
                "stuName='" + stuName + '\'' +
                ", stuAge=" + stuAge +
                ", stuGender='" + stuGender + '\'' +
                ",stuHeight='" + stuHeight + '\'' +
                '}';
    }

}

  • 对集合操作的代码
package com.atguigu.test;

import com.sun.org.apache.bcel.internal.generic.IF_ACMPEQ;

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

public class TestSix {

    public static void main(String[] args) {

        //初始化数据
        List<Student> list1 = new ArrayList<Student>();
        Student stu = new Student("Tom",20,"男",172);
        Student stu1 = new Student("Jerry",24,"男",180);
        Student stu2 = new Student("Mary",23,"女",160);
        Student stu3 = new Student("June",21,"女",165);

        list1.add(stu);
        list1.add(stu1);
        list1.add(stu2);
        list1.add(stu3);
        System.out.println("-------------------------");

        for (Object o : list1) {
            System.out.println(o);
        }

        //1、查找指定元素 查找年龄为20的学生
        Student stu20=new Student();
        //1.1迭代器方式
        Iterator it = list1.iterator();
        while(it.hasNext())
        {
            Student N= (Student)it.next();
            if (N.getStuAge()==20){
                stu20=N;
                break;
            }
        }
        System.out.println("-----迭代器方式------------");
        System.out.println(stu20);
        //1.2 filter方法
        stu20=  list1.stream().filter(t->t.getStuAge()==20).findFirst().get();

        System.out.println("-----filter方法-------------");
        System.out.println(stu20);

        //2.查找符合条件的集合   名字以为J开头的集合
        List<Student> listcondition= list1.stream().filter(t->t.getStuName().startsWith("J")).collect(Collectors.toList());

        //3、按条件排序
        // 按年龄排序
        List<Student> collect1 = list1.stream().sorted(Comparator.comparing(Student::getStuAge)).collect(Collectors.toList());
        List<Student> collect2 =list1.stream().sorted(Comparator.comparing(Student::getStuAge).reversed()).collect(Collectors.toList());
        //年龄升序,相同年龄身高升序
        List<Student> collect3 = list1.stream().sorted(Comparator.comparing(Student::getStuAge).thenComparing(Student::getStuHeight)).collect(Collectors.toList());

        //4.判断是否存在某元素
        boolean b = list1.stream().anyMatch(t -> t.getStuName() == "Tom");


        //5. 集合分组,类似与c#的groupby,按性别分组
        Map<String, List<Student>> groups = list1.stream().collect(Collectors.groupingBy(Student::getStuGender));


        for (Object o : list1) {
            System.out.println(o);
        }


    }
}

你可能感兴趣的:(Java,java,list,c#)