Home > Java > javaTutorial > How to use lambda expression to sort in Java

How to use lambda expression to sort in Java

WBOY
Release: 2023-05-23 16:58:06
forward
4098 people have browsed it

1.lambda expression sorting

Let’s first look at a few common sorting examples, sorting of basic data types

    List list = Arrays.asList(1,3,2,5,4);
    list.sort(Comparator.naturalOrder());
    System.out.println(list);
    list.sort(Comparator.reverseOrder());
    System.out.println(list);
  
   输出结果:

    [1, 2, 3, 4, 5]
    [5, 4, 3, 2, 1]
Copy after login

We can see that the execution results are in line with expectations, but In most scenarios, we may need to sort a certain attribute of the object, so how should we do it? Let’s look at the example below:

public class Student {
    private String name;
    private String sexual;
    private Integer age;

    public Student(String name, String sexual,Integer age) {
        this.name = name;
        this.sexual = sexual;
        this.age = age;
    }

    public String getName() {
        return name;
    }

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

    public String getSexual() {
        return sexual;
    }

    public void setSexual(String sexual) {
        this.sexual = sexual;
    }

    public Integer getAge() {
        return age;
    }

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

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

public class Starter {
    public static void main(String[] args) {
        List<Student> list = Arrays.asList(
                new Student("jack", 12),
                new Student("john", 13),
                new Student("lily", 11),
                new Student("lucy", 10)
        );
        list.sort(Comparator.comparing(Student::getAge));
        System.out.println(list);
        list.sort(Comparator.comparing(Student::getAge).reversed());
        System.out.println(list);
    }
}

输出结果:

[Student{name='lucy', age=10}, Student{name='lily', age=11}, Student{name='jack', age=12}, Student{name='john', age=13}]
[Student{name='john', age=13}, Student{name='jack', age=12}, Student{name='lily', age=11}, Student{name='lucy', age=10}]
Copy after login

If we need to group and sort by gender, how should we do it? Let's look at the example below

public class Starter {
    public static void main(String[] args) {
        List<Student> list = Arrays.asList(
                new Student("jack", "male", 12),
                new Student("john", "male", 13),
                new Student("lily", "female", 11),
                new Student("david", "male", 14),
                new Student("luck", "female", 13),
                new Student("jones", "female", 15),
                new Student("han", "male", 13),
                new Student("alice", "female", 11),
                new Student("li", "male", 12)
        );
 Map<String, List<Student>> groupMap = list.stream().sorted(Comparator.comparing(Student::getAge))
.collect(Collectors.groupingBy(Student::getSexual, Collectors.toList()));
        System.out.println(groupMap.toString());
    }
}

输出结果:

{
	female = [
      Student {
		name = 'lily', sexual = 'female', age = 11
	  }, 
      Student {
		name = 'alice', sexual = 'female', age = 11
	}, Student {
		name = 'luck', sexual = 'female', age = 13
	}, Student {
		name = 'jones', sexual = 'female', age = 15
	}],
   male = [
    Student {
		name = 'jack', sexual = 'male', age = 12
	}, Student {
		name = 'li', sexual = 'male', age = 12
	}, Student {
		name = 'john', sexual = 'male', age = 13
	}, Student {
		name = 'han', sexual = 'male', age = 13
	}, Student {
		name = 'david', sexual = 'male', age = 14
	}]
}
Copy after login

We see that there is a problem with the output results above. If the ages are the same, they are not sorted by name. How to implement this function? Let’s look at the example below

Map<String, List<Student>> groupMap = list.stream().sorted(Comparator.comparing(Student::getAge)
.thenComparing(Student::getName)).collect(Collectors.groupingBy(Student::getSexual, Collectors.toList()));

输出结果:

{
	female = [
     Student {
		name = 'alice', sexual = 'female', age = 11
	}, Student {
		name = 'lily', sexual = 'female', age = 11
	}, Student {
		name = 'luck', sexual = 'female', age = 13
	}, Student {
		name = 'jones', sexual = 'female', age = 15
	}],
   male = [
     Student {
		name = 'jack', sexual = 'male', age = 12
	}, Student {
		name = 'li', sexual = 'male', age = 12
	}, Student {
		name = 'han', sexual = 'male', age = 13
	}, Student {
		name = 'john', sexual = 'male', age = 13
	}, Student {
		name = 'david', sexual = 'male', age = 14
	}]
}
Copy after login

The above is the detailed content of How to use lambda expression to sort in Java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template