赞
踩
如果需要将流中的元素映射到另一个流中,可以使用map方法。方法声明:
<R> Stream<R> map(Function<? super T,? extends R> mapper);
该接口需要一个Function函数式接口参数,可以将当前流中的T类型数据转换为另一个R类型的流。
- import java.util.stream.Stream;
-
- public class Demo {
-
- public static void main(String[] args) {
- Stream<String> original = Stream.of("11","22","33");
-
- //Map可以将一种类型的流转换成另一种类型的流
- //将Stream流中的字符串转成Integer
- //Stream<Integer> stream = original.map((String s)->{
- // return Integer.parseInt(s);
- //});
- //original.map(s->Integer.parseInt(s)).forEach(System.out::println);
- //map方法的参数通过方法引用,将字符串类型转换成为int类型(并自动装箱为Integer类对象)
- original.map(Integer::parseInt).forEach(System.out::println);
- }
- }
执行上述代码,其输出结果为:
- 11
- 22
- 33
- import lombok.Data;
-
- @Data
- public class Person {
-
- private String name;
- private int sex;
- private String address;
- private String phoneNumber;
- }
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import java.util.stream.Collectors;
-
- public class Demo {
-
- private static List<Person> buildData(int number){
- List<Person> list =new ArrayList<>();
- for(int i=0;i<number;i++){
- Person person=new Person();
- person.setName("张三"+i);
- person.setSex(1);
- person.setPhoneNumber("1880521321"+i);
- person.setAddress("北京天通苑"+i);
- list.add(person);
- }
- return list;
- }
-
- public static void main(String[] args){
- List<Person> personList=buildData(3);
- List<Map> collect = personList.stream().map(x -> {
- Map map = new HashMap();
- map.put("name", x.getName());
- map.put("sex", x.getSex());
- map.put("address", x.getAddress());
- return map;
- }).collect(Collectors.toList());
- System.out.println(collect);
- }
- }
执行上述代码,其输出结果为:
[{address=北京天通苑0, sex=1, name=张三0}, {address=北京天通苑1, sex=1, name=张三1}, {address=北京天通苑2, sex=1, name=张三2}]
- import lombok.AllArgsConstructor;
- import lombok.Data;
- import lombok.NoArgsConstructor;
-
- import java.util.ArrayList;
- import java.util.List;
-
- @NoArgsConstructor
- @Data
- @AllArgsConstructor
- public class Student {
-
- private int classId;
- private String name;
- private int score;
-
- public static List<Student> buildData(){
- List<Student> studentList = new ArrayList<>();
- Student student1 = new Student(1,"张三",80);
- Student student2 = new Student(1,"李四",59);
- Student student3 = new Student(2,"王五",90);
- studentList.add(student1);
- studentList.add(student3);
- studentList.add(student2);
- return studentList;
- }
- }
- import java.util.List;
- import java.util.stream.Collectors;
-
- public class Demo {
-
- public static void main(String[] args) {
- List<Student> studentList = Student.buildData();
- List<String> names = studentList.stream().map(Student::getName).collect(Collectors.toList());
- System.out.println(names);
- }
- }
执行上述代码,其输出结果为:
[张三, 王五, 李四]
如果需要将数据排序,可以使用sorted方法。方法声明:
- Stream<T> sorted();
- Stream<T> sorted(Comparator<? super T> comparator);
- import java.util.stream.Stream;
-
- public class Demo {
-
- public static void main(String[] args) {
- Stream<Integer> stream = Stream.of(33,22,11,55);
- // sorted():根据元素的自然顺序排序
- //stream.sorted().forEach(System.out::println);
- // sorted(Comparator<? super T> comparator):根据比较指定的规则排序
- stream.sorted((Integer i1,Integer i2)->{
- return i2-i1;
- }).forEach(System.out::println);
- }
- }
执行上述代码,其输出结果为:
- 55
- 33
- 22
- 11
上述stream.sorted()方法还可以进一步简化:
stream.sorted((i1,i2)-> i2-i1).forEach(System.out::println);
sorted方法除了可以根据元素的自然顺序排序,也可以指定比较器排序
- //对象集合以类属性一升序排序
- list.stream().sorted(Comparator.comparing(类::属性一));
例如下面这个示例就是以User类的id属性进行升序排序:
- import lombok.AllArgsConstructor;
- import lombok.Data;
-
- import java.util.ArrayList;
- import java.util.List;
-
- @Data
- @AllArgsConstructor
- public class User {
-
- private int id;
- private String name;
-
- public static List<User> buildData(){
- List<User> userList = new ArrayList<>();
- User user1 = new User(1,"张三");
- User user2 = new User(2,"李四");
- User user3 = new User(3,"王五");
- userList.add(user2);
- userList.add(user1);
- userList.add(user3);
- return userList;
- }
- }
- import java.util.Comparator;
- import java.util.List;
- import java.util.stream.Collectors;
-
- public class SortedDemo {
-
- public static void main(String[] args){
- List<User> userList = User.buildData();
- System.out.println("排序前:");
- userList.forEach(System.out::println);
-
- System.out.println("排序后:");
- userList = userList.stream().sorted(Comparator.comparing(User::getId)).collect(Collectors.toList());
- userList.forEach(System.out::println);
- }
- }
执行上述代码,其输出结果为:
- 排序前:
- User(id=2, name=李四)
- User(id=1, name=张三)
- User(id=3, name=王五)
- 排序后:
- User(id=1, name=张三)
- User(id=2, name=李四)
- User(id=3, name=王五)
如果需要去除重复数据,可以使用distinct方法。方法声明:
Stream<T> distinct();
- import java.util.stream.Stream;
-
- public class Demo {
-
- public static void main(String[] args) {
- Stream<Integer> stream = Stream.of(22,33,22,11,33);
- stream.distinct().forEach(System.out::println);
- Stream<String> stream1 = Stream.of("aa","bb","aa","bb","cc");
- stream1.distinct().forEach(System.out::println);
- }
- }
执行上述代码,其输出结果为:
- 22
- 33
- 11
- aa
- bb
- cc
distinct对自定义对象去除重复,首先需要对类的equals()和hscode()进行重写
- public class Person {
-
- private String name;
- private int age;
-
- public Person(String name, int age) {
- this.name = name;
- this.age = age;
- }
-
- @Override
- public boolean equals(Object o) {
- if(this==o) {
- return true;
- }
- if(o==null || getClass()!=o.getClass()) {
- return false;
- }
- Person person =(Person)o;
- if(age!=person.age) {
- return false;
- }
- return name!=null?name.equals(person.name):person.name ==null;
- }
-
- @Override
- public int hashCode(){
- int result =name!=null?name.hashCode():0;
- result=31*result+age;
- return result;
- }
-
- 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 String toString() {
- return "Person{" +
- "name=" + name +
- ", age=" + age +
- '}';
- }
- }
- public static void main(String[] args) {
- Stream<Person> stream = Stream.of(
- new Person("貂蝉",18),
- new Person("西施",18),
- new Person("西施",18),
- new Person("王昭君",18)
- );
- stream.distinct().forEach(System.out::println);
- }
执行上述代码,其输出结果为:
- Person{name=貂蝉, age=18}
- Person{name=西施, age=18}
- Person{name=王昭君, age=18}
如果需要判断数据是否匹配指定的条件,可以使用Match相关方法。方法声明:
- boolean allMatch(Predicate<? super T> predicate);
- boolean anyMatch(Predicate<? super T> predicate);
- boolean noneMatch(Predicate<? super T> predicate);
- public static void main(String[] args) {
- Stream<Integer> stream = Stream.of(7,8,6,4);
- //boolean b=stream.allMatch(i->i>0); //allMatch:匹配所有元素,所有元素都需要满足条件
- boolean b =stream.noneMatch(i-> i<5);//noneMatch:匹配所有元素,所有元素都不满足条件则返回true
- System.out.println(b);
- }
执行上述代码,其输出结果为:
false
anyMatch:只要有一个元素满足条件,就返回true,否则返回false。
- import java.util.ArrayList;
- import java.util.List;
-
- public class Demo {
-
- private static List<String> buildData(){
- List<String> list =new ArrayList<>();
- list.add("张三");
- list.add("李四");
- list.add("王五");
- return list;
- }
-
- public static void main(String[] args){
- // list列表中只要有一个为“张三”,那么就返回True,否则的话返回False
- List<String> list = buildData();
- boolean flag= list.stream().anyMatch((String name) -> "张三".equals(name));
- System.out.println(flag);
- }
- }
执行上述代码,其输出结果为:
true
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。