1Stream流
1.1Stream流初体验
public class StreamDemo1 {
public static void main(String[] args) {
List<String> list= new ArrayList<>();
list.add("张三疯");
list.add("张三");
list.add("熊大");
list.add("张小小");
list.stream().filter((name)->{
return name.startsWith("张");
}).filter(name->name.length()==3).forEach((name)-> System.out.println(name));
}
}
1.2Stream流的三类方法
- 获取Stream流
- 中间方法
- 流水线上的操作。
- 一次操作完毕之后,还可以继续进行其他操作
- 终结方法
- 一个Stream流只能有一个终结方法
- 是流水线上的最后一个操作
1.3Stream流的获取方法
- 单列集合
- 可以使用Collection接口中的默认方法stream()生成流
default Stream stream()
- 双列集合
- 双列集合不能直接获取 , 需要间接的生成流
可以先通过keySet或者entrySet获取一个Set集合,再获取Stream流
- 数组
public class StreamDemo2 {
public static void main(String[] args) {
List<String> list =new ArrayList<>();
list.add("string");
Map<String,String> map =new HashMap<>();
map.put("双列","集合");
int[] arr={1,2,3,4,5};
Stream<String> s1=list.stream();
Set<Map.Entry<String,String>> entries=map.entrySet();
Stream<Map.Entry<String, String>> s2 = entries.stream();
IntStream s3=Arrays.stream(arr);
Stream<Integer> s4=Stream.of(11,22,33,44,55);
}
}
1.4Stream流的中间方法
- Stream filter(Predicate predicate):用于对流中的数据进行过滤
- Predicate接口中的方法 : boolean test(T t):对给定的参数进行判断,返回一个布尔值
- Stream limit(long maxSize):截取指定参数个数的数据
- Stream skip(long n):跳过指定参数个数的数据
- static Stream concat(Stream a, Stream b):合并a和b两个流为一个流
- Stream distinct():去除流中重复的元素。依赖(hashCode和equals方法)
- Stream sorted () : 将流中元素按照自然排序的规则排序
- Stream sorted (Comparator<? super T> comparator) : 将流中元素按照自定义比较器规则排序
public class StreamDemo3 {
public static void main(String[] args) {
List<Integer> list = new ArrayList<>();
Collections.addAll(list, 1, 2, 3, 4, 5, 6, 7);
list.stream().filter((num)->num>5).forEach(num-> System.out.println(num));
System.out.println("--------------------------------------------------------");
Stream<Integer> s1 =list.stream();
Stream<Integer> s2 =s1.filter(num->num>5);
s2.forEach(num-> System.out.println(num));
}
}
public class StreamDemo4 {
public static void main(String[] args) {
List<Integer> list = new ArrayList<>();
Collections.addAll(list, 1, 2, 3, 4, 5, 6, 7);
list.stream().limit(3).forEach(num-> System.out.println(num));
}
}
public class StreamDemo5 {
public static void main(String[] args) {
List<Integer> list = new ArrayList<>();
Collections.addAll(list, 1, 2, 3, 4, 5, 6, 7);
list.stream()
.skip(3)
.forEach(num -> System.out.println(num));
}
}
public class StreamDemo6 {
public static void main(String[] args) {
List<Integer> list = new ArrayList<>();
Collections.addAll(list, 1, 2, 3, 4, 5, 6, 7);
Integer[] arr = {8,9,10};
Stream<Integer> s1 =list.stream();
Stream<Integer> s2 = Arrays.stream(arr);
Stream<Integer> newStream= Stream.concat(s1,s2);
newStream.forEach(num -> System.out.println(num));
}
}
public class StreamDemo7 {
public static void main(String[] args) {
List<Integer> list = new ArrayList<>();
Collections.addAll(list, 1, 2, 3, 2, 5, 3, 6, 4, 7);
list.stream()
.distinct()
.forEach(num -> System.out.println(num));
}
}
public class Student {
private String name;
private int age;
public Student() {
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Student student = (Student) o;
return age == student.age && Objects.equals(name, student.name);
}
@Override
public int hashCode() {
return Objects.hash(name, age);
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
public class StreamDemo8 {
public static void main(String[] args) {
List<Student> studentList = new ArrayList<>();
studentList.add(new Student("张三",22));
studentList.add(new Student("李四",24));
studentList.add(new Student("张三",22));
studentList.add(new Student("王五",25));
studentList.add(new Student("李四",23));
studentList.stream()
.distinct()
.forEach(student -> System.out.println(student));
}
}
public class StreamDemo9 {
public static void main(String[] args) {
List<String> list = new ArrayList<>();
Collections.addAll(list, "张三", "李四");
list.stream()
.map(name->new Student(name))
.forEach(student-> System.out.println(student));
}
}
public class StreamDemo10 {
public static void main(String[] args) {
List<String> list = new ArrayList<>();
Collections.addAll(list, "shanghai", "beijing");
list.stream()
.map(city->city.toUpperCase())
.forEach(city-> System.out.println(city));
}
}
public class StreamDemo11 {
public static void main(String[] args) {
List<Integer> list = new ArrayList<>();
Collections.addAll(list, 11, 2, 13, 42, 15, 6, 7);
list.stream()
.filter(num->num<30)
.sorted()
.forEach(num-> System.out.println(num));
}
}
public class StreamDemo2 {
public static void main(String[] args) {
List<Integer> list = new ArrayList<>();
Collections.addAll(list, 11, 2, 13, 42, 15, 6, 7);
list.stream()
.filter(num -> num < 30)
.sorted( (num1, num2) -> num2-num1)
.forEach(num -> System.out.println(num));
}
}