Java中如何用lambda表达式来排序

1.lambda表达式排序

我们首先看几个比较常见的排序例子,基本数据类型的排序

    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]

我们可以看到执行结果是符合预期的,但是多数场景我们可能需要针对对象的某个属性进行排序,那么应该怎样做呢?我们看下边的例子:

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=&#39;lucy&#39;, age=10}, Student{name=&#39;lily&#39;, age=11}, Student{name=&#39;jack&#39;, age=12}, Student{name=&#39;john&#39;, age=13}]
[Student{name=&#39;john&#39;, age=13}, Student{name=&#39;jack&#39;, age=12}, Student{name=&#39;lily&#39;, age=11}, Student{name=&#39;lucy&#39;, age=10}]

如果我们需要按照性别分组再排序又该如何实现呢?我们接着看下边的例子

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 = &#39;lily&#39;, sexual = &#39;female&#39;, age = 11
	  }, 
      Student {
		name = &#39;alice&#39;, sexual = &#39;female&#39;, age = 11
	}, Student {
		name = &#39;luck&#39;, sexual = &#39;female&#39;, age = 13
	}, Student {
		name = &#39;jones&#39;, sexual = &#39;female&#39;, age = 15
	}],
   male = [
    Student {
		name = &#39;jack&#39;, sexual = &#39;male&#39;, age = 12
	}, Student {
		name = &#39;li&#39;, sexual = &#39;male&#39;, age = 12
	}, Student {
		name = &#39;john&#39;, sexual = &#39;male&#39;, age = 13
	}, Student {
		name = &#39;han&#39;, sexual = &#39;male&#39;, age = 13
	}, Student {
		name = &#39;david&#39;, sexual = &#39;male&#39;, age = 14
	}]
}

我们看到上边的输出结果存在一个问题,如果年龄相同则没有按照姓名排序,怎样实现这个功能呢?我们接着看下边的例子

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 = &#39;alice&#39;, sexual = &#39;female&#39;, age = 11
	}, Student {
		name = &#39;lily&#39;, sexual = &#39;female&#39;, age = 11
	}, Student {
		name = &#39;luck&#39;, sexual = &#39;female&#39;, age = 13
	}, Student {
		name = &#39;jones&#39;, sexual = &#39;female&#39;, age = 15
	}],
   male = [
     Student {
		name = &#39;jack&#39;, sexual = &#39;male&#39;, age = 12
	}, Student {
		name = &#39;li&#39;, sexual = &#39;male&#39;, age = 12
	}, Student {
		name = &#39;han&#39;, sexual = &#39;male&#39;, age = 13
	}, Student {
		name = &#39;john&#39;, sexual = &#39;male&#39;, age = 13
	}, Student {
		name = &#39;david&#39;, sexual = &#39;male&#39;, age = 14
	}]
}

以上就是Java中如何用lambda表达式来排序的详细内容,更多请关注www.sxiaw.com其它相关文章!