2023.2.21学习日志

一.Stream流

1. forEach遍历

forEach:该方法接收一个Consumer接口函数,将每一个流元素交给该函数处理

forEach方法:用来遍历流中的数据

注:是一个终结方法,遍历之后就不能继续调用Stream流中的其他方法

public class Stream_ForEach {

public static void main(String[] args) {

//获取一个Stream流

Stream<String>stream= Stream.of("张三","李四","王五","赵六");

//使用Stream流的方法forEach对stream流中的数据遍历

stream.forEach((String name)->{

System.out.println(name);

});

}

}

2. filter过滤

filter:用于对Stream流中的数据进行过滤

filter(Predicate<? super T>predicate)

filter方法的参数Predicate是一个函数式接口,可以使用lambda表达式

Predicate中的抽象方法

boolean test(T t)

public class Stream_filter {

public static void main(String[] args) {

//创建一个Stream流

Stream<String> stream = Stream.of("张三", "李四", "王五", "赵六1", "刘老七");

//单条件过滤

Stream<String> stream1 = stream.filter((String name) -> {

return name.startsWith("刘");

});

//多条件过滤

List<String> stream2 = stream.filter((String name) -> {

if(name.length()>=3 && name.equals("刘老七")){

return true;

}

return false;

}).collect(Collectors.toList);

//遍历stream1

stream1.forEach((name)-> System.out.println(name));

//输出stream2

System.out.println(stream2)

}

}

3. distinct去重

public class Stream_distinct {

public static void main(String[] args) {

//创建一个Stream流

Stream<String> stream = Stream.of("张三", "张三","张三","李四", "王五", "赵六1", "刘老七");

//去重

List<String> stream1 = stream().distinct().collect(Collectors.toList());

//输出stream1

System.out.println(stream1)

}

}

4. limit截取

limit:用于截取流中的元素

limit可以对流进行截取,只取用前n个

limit(long maxSize);

参数是一个long型,如果集合当前长度大于参数则进行截取,否则不进行操作

limit是一个延迟方法,可以继续使用Stream流方法

public class Stream_limit {

public static void main(String[] args) {

//创建一个Stream流

Stream<String> stream = Stream.of("张三", "张三","张三","李四", "王五", "赵六1", "刘老七");

//去重

List<String> list = stream().distinct().collect(Collectors.toList());

//截取去重后的前2个元素

list = list.stream().limit(2).collect(Collectors.toList();

//输出stream1

System.out.println(list)

}

}

5. skip跳过

skip方法:用于跳过元素

skip(long n)

如果流的当前长度大于n,则跳过前n个,否则将会得到一个长度为0的空流

public class Stream_skip {

public static void main(String[] args) {

//创建一个Stream流

Stream<String> stream = Stream.of("张三", "张三","张三","李四", "王五", "赵六1", "刘老七");

//去重

List<String> list = stream().distinct().collect(Collectors.toList());

//跳过去重后的前2个元素

list = list.stream().skip(2).collect(Collectors.toList());

//输出stream1

System.out.println(list)

}

}

6.排序sorted

public class Stream_sorted {

public static void main(String[] args) {

List<Test> list = new ArrayList<>();

list.add(new Test("张三",23,new BigDecimal("3000"),new BigDecimal("1.1")));

list.add(new Test("李四",24,new BigDecimal("2800"),new BigDecimal("1.2")));

list.add(new Test("王五",22,new BigDecimal("3200"),new BigDecimal("1.3")));

//根据年龄从大到小排序

list = list.stream()

.sorted(Comparator.comparing(Test::getAge).reversed())

.collect(Collectors.toList());

list.forEach(System.out::println);

}

}

7.最值max,min

public class Stream_max_min {

public static void main(String[] args) {

List<Test> list = new ArrayList<>();

list.add(new Test("张三",23,new BigDecimal("3000"),new BigDecimal("1.1")));

list.add(new Test("李四",24,new BigDecimal("2800"),new BigDecimal("1.2")));

list.add(new Test("王五",22,new BigDecimal("3200"),new BigDecimal("1.3")));

//获取年龄最大的人

Test maxPeople = list.stream().max(Comparator.companing(Test::getAge)).get();

//获取年龄最小的人

Test minPeople = list.stream().min(Comparator.companing(Test::getAge)).get();

}

}

8. 统计reduce

public class Stream_reduce {

public static void main(String[] args) {

List<Test> testList = new ArrayList<Test>();

testList.add(new Test("小明",23,new BigDecimal("3000"),new BigDecimal("1.1")));

testList.add(new Test("小红",24,new BigDecimal("2800"),new BigDecimal("1.2")));

testList.add(new Test("小兰",22,new BigDecimal("3200"),new BigDecimal("1.3")));

//统计年龄总和

int totalAge =testList.stream().mapToInt(Test::getAge).sum();

//统计工资总和

BigDecimal totalSalary = testList.stream().map(Test::getSalary)

//统计工资乘以各自系数的总和(向上保留两位)

BigDecimal totalRatioSalary = testList.stream()

.map(s->s.getSalary()

.multiply(s.getRatio())

.setScale(2,BigDecimal.ROUND_HALF_UP))

.reduce(BigDecimal.ZERO,BigDecimal::add);

}

}

9. List结构转换Map结构

public class Stream_List_Map {

public static void main(String[] args) {

List<Test> testList = new ArrayList<Test>();

testList.add(new Test("张三",23,new BigDecimal("3000"),new BigDecimal("1.1")));

testList.add(new Test("李四",24,new BigDecimal("2800"),new BigDecimal("1.2")));

testList.add(new Test("王五",22,new BigDecimal("3200"),new BigDecimal("1.3")));

//根据姓名转map,map的key为name

Map<String, Test> nameMap= testList.stream().collect(Collectors.toMap(Test::getName, Test -> Test);

System.out.println(map);

}

}

10. List<Object>对象转List<String>

public class Stream_object_string {

public static void main(String[] args) {

List<Test> testList = new ArrayList<Test>();

testList.add(new Test("张三",23,new BigDecimal("3000"),new BigDecimal("1.1")));

testList.add(new Test("李四",24,new BigDecimal("2800"),new BigDecimal("1.2")));

testList.add(new Test("王五",22,new BigDecimal("3200"),new BigDecimal("1.3")));

//获取姓名集合

List<String> nameList = testList.stream().map(Test::getName()).collect(Collectors.toList());

System.out.println("value:"+nameList);

}

}

11. List<Object>对象转List<Object>

public class Stream_object_object {

public static void main(String[] args) {

List<People> peopleList = new ArrayList<People>();

peopleList.add(new People("张三",23,new BigDecimal("3000"),new BigDecimal("1.1")));

peopleList.add(new People("李四",24,new BigDecimal("2800"),new BigDecimal("1.2")));

peopleList.add(new People("王五",22,new BigDecimal("3200"),new BigDecimal("1.3")));

//对象转对象

List<Student> studentList = peopleList.stream().map(s->{

return new Student(s.getName(),s.getAge());

}).collect(Collectors.toList());

System.out.println("value:"+studentList);

}

}

二.方法引用

情况1:对象::实例方法名

public class MethodRefTest {
  //Consumer中的void accept(T t)
  //PrintStream中的void println(T t)
  @Test
  public void test() {
//Lambda 方式
    Consumer<String> c1 = str -> System.out.println(str);
    c1.accept("兖州");
//方法引用方式
    PrintStream ps = System.out;
    Consumer<String> c2 = ps::println;
    c2.accept("xian");
  }
  //Supplier中的T get()
  //Employee中的String getName()
  public void test2() {
    Employee emp = new Employee(004,"Nice",19,4200);
//Lambda 方式
    Supplier<String> sk1 = () -> emp.getName();
    System.out.println(sk1.get());
//方法引用方式
    Supplier<String> sk2 = emp::getName;
    System.out.println(sk2.get());
  }
}

 

情况2:类 :: 静态方法

public class MethodRefTest {
//Comparator中的int compare(T t1,T t2)
//Integer中的int compare(T t1,T t2)
  public void test3() {
//Lambda 方式
    Comparator<Integer> com1 = (t1, t2) -> Integer.compare(t1,t2);
    System.out.println(com1.compare(21,20));
//方法引用方式
    Comparator<Integer> com2 = Integer::compare;
    System.out.println(com2.compare(15,7));
  }
//Function中的R apply(T t)
//Math中的Long round(Double d)
  public void test4() {
    Function<Double,Long> func = new Function<Double, Long>() {
      @Override
      public Long apply(Double d) {
        return Math.round(d);
      }
    };
//Lambda 方式
    Function<Double,Long> func1 = d -> Math.round(d);
    System.out.println(func1.apply(14.1));
//方法引用方式
    Function<Double,Long> func2 = Math::round;
    System.out.println(func2.apply(17.4));
  }
}

情况3:类::实例方法

public class MethodRefTest {
// Comparator中的int comapre(T t1,T t2)
// String中的int t1.compareTo(t2)
  public void test5() {
//Lambda 方式
    Comparator<String> com1 = (s1,s2) -> s1.compareTo(s2);
    System.out.println(com1.compare("abc","abd"));
//方法引用方式
    Comparator<String> com2 = String :: compareTo;
    System.out.println(com2.compare("abd","abm"));
  }

//BiPredicate中的boolean test(T t1, T t2);
//String中的boolean t1.equals(t2)
  public void test6() {
//Lambda 方式
    BiPredicate<String,String> pre1 = (s1, s2) -> s1.equals(s2);
    System.out.println(pre1.test("MON","MON"));
//方法引用方式
    BiPredicate<String,String> pre2 = String :: equals;
    System.out.println(pre2.test("MON","MON"));
  }

// Function中的R apply(T t)
// Employee中的String getName();
  public void test7() {
    Employee employee = new Employee(007, "Ton", 21, 8000);
//Lambda 方式
    Function<Employee,String> func1 = e -> e.getName();
    System.out.println(func1.apply(employee));
//方法引用方式
    Function<Employee,String> f2 = Employee::getName;
    System.out.println(f2.apply(employee));
  }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值