当前位置 : 主页 > 编程语言 > java >

Java8-Stream流(中间操作)limit&skip

来源:互联网 收集:自由互联 发布时间:2022-07-04
limit: 限制,截取流中指定数量的元素 skip: 跳过,跳过流中的指定数量的元素 package Stream ; import lombok . * ; import java . util . Objects ; /** * @Author yqq * @Date 2021/10/17 16:55 * @Version 1.0 */ @Getter @

limit: 限制,截取流中指定数量的元素
skip: 跳过,跳过流中的指定数量的元素

package Stream;

import lombok.*;

import java.util.Objects;

/**
* @Author yqq
* @Date 2021/10/17 16:55
* @Version 1.0
*/
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class Student implements Comparable<Student>{
private String name;
private Integer age;
private Integer score;

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

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Student student = (Student) o;
return name.equals(student.name) && age.equals(student.age) && score.equals(student.score);
}

@Override
public int hashCode() {
return Objects.hash(name, age, score);
}

@Override
public int compareTo(Student o) {
return score - o.score;
}
}package Stream;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
* @Author yqq
* @Date 2021/10/17 13:40
* @Version 1.0
*/
public class demo01 {
public static void main(String[] args) {
List<Student> list = new ArrayList<>();
Collections.addAll(list,
new Student("lishi",22,20),
new Student("zhangshan",23,28),
new Student("wanger",24,19),
new Student("mazi",25,29),
new Student("mazi",25,29)
);
list.stream().sorted((s1,s2) -> s2.getScore()-s1.getScore())
.distinct()
.limit(3)//获取成绩的前3名,
.skip(2)//获取成绩的第3名,
.forEach(System.out::println);
}
}

Java8-Stream流(中间操作)limit&skip_ide


上一篇:Java8-Sream流(中间操作)mapToInt
下一篇:没有了
网友评论