java实战 遍历

Last updated on November 22, 2024 pm

🧙 Questions

关于java的数组和遍历

☄️ Ideas

去除null
@Test
public void test1() {

    // 将list中存在的null去除
    dogs = dogs.stream().filter(Objects::nonNull).collect(Collectors.toList());
    dogs.forEach(System.out::println);
}
去重
static <T> Predicate<T> distinctByKey(Function<? super T, ?> keyExtractor) {
    
    Map<Object, Boolean> seen = new ConcurrentHashMap<>();
    return t -> seen.putIfAbsent(keyExtractor.apply(t), Boolean.TRUE) == null;
}
    

@Test
public void test2() {
    
    // 将某个字段相同的数据去除,保留第一条
    dogs = dogs.stream().filter(distinctByKey(Dog::getName)).collect(Collectors.toList());
    dogs.forEach(System.out::println);
}
遍历赋值
@Test
public void test3(){

    // 遍历操作
    dogs.forEach(metaDog -> metaDog.setIndex(dogs.indexOf(metaDog)));
    dogs.forEach(System.out::println);
}
过滤
@Test
public void test4() {

    // 添加拦截条件
    dogs = dogs.stream()
        .filter(metaDog -> metaDog.getMoney() > 13)
        .collect(Collectors.toList());
    dogs.forEach(System.out::println);
}
排序
@Test
public void test5() {

    // 降序
    dogs= dogs.stream()
        .sorted(Comparator.comparing(Dog::getAge, Collections.reverseOrder()))
        .collect(Collectors.toList());
    dogs.forEach(System.out::println);
    
    // 升序
    dogs = dogs.stream()
        .sorted(Comparator.comparing(Dog::getAge))
        .collect(Collectors.toList());
    dogs.forEach(System.out::println);
    
    // 类型转换排序
    dogs = dogs.stream()
        .sorted(Comparator.comparingDouble(metaDog -> Double.parseDouble(String.valueOf(metaDog.getAge()))))
        .collect(Collectors.toList());
    dogs.forEach(System.out::println);
}
分组
@Test
public void test6() {
    
    // 字段分组
    Map<String, List<Dog>> dogsMap = dogs.stream().collect(Collectors.groupingBy(Dog::getEnjoy));
    dogsMap.forEach((k, v) -> System.out.println(k));
    
    // 分组求和
    Map<String, Double> dogDoubleMap = dogs.stream()
        .collect(Collectors.groupingBy(Dog::getEnjoy, Collectors.collectingAndThen(
            Collectors.toList(), list -> list.stream().mapToDouble(Dog::getMoney).sum()
        )));
    dogDoubleMap.forEach((k, v) -> System.out.println(k + ":" + v));
}
提取字段
@Test
public void test7() {
    
    // 提取某个字段
    List<String> dogsList = dogs.stream().map(Dog::getName).collect(Collectors.toList());
    dogsList.forEach(System.out::println);
}
最大值
 @Test
public void test8() {

  // 获取最大值
  Double maxDogMoney;
  Optional<Dog> maxMoneyDog = dogs.stream().max(Comparator.comparingDouble(Dog::getMoney));
  if (maxMoneyDog.isPresent()) {
      maxDogMoney = maxMoneyDog.get().getMoney();
  } else {
      maxDogMoney = -1.0;
  }
  System.out.println(maxDogMoney);
}
不重复
 @Test
public void test9() {
    
    // 不重复储存
    Set<String> dogsName = new LinkedHashSet<>();
    dogsName.add("zhangsan");
    dogsName.add("lisi");
    dogsName.add("wangwu");
    dogsName.add("wangwu");
    dogsName.forEach(System.out::println);
}
求和
@Test
public void test10() {
    
    // 对某个字段求和
    long sum = dogs.stream().mapToLong(Dog::getAge).sum();
    System.out.println(sum);
}

java实战 遍历
https://ispong.isxcode.com/spring/java/java实战 遍历/
Author
ispong
Posted on
March 1, 2022
Licensed under