Home > Java > javaTutorial > body text

How to get max value from stream in java8

WBOY
Release: 2023-05-14 15:43:12
forward
2536 people have browsed it

java8's stream takes max

 public static void main(String[] args) {
        List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6);
        Integer max = list.stream().max((a, b) -> {
            if (a > b) {
                return 1;
            } else return -1;
        }).get();

        System.out.println(max);
    }
Copy after login

Note: The size is judged here through positive and negative numbers and 0 values. Instead of writing it directly as

if (a > b) {
return a;
} else return b;
Copy after login

, you can simplify the writing

int max = list.stream().max((a, b) -> a > b ? 1 : -1).get();
Copy after login

java8 stream detailed explanation~aggregation (max/min/count)

max, You must be familiar with the words min and count. Yes, we often use them for data statistics in mysql.

These concepts and usages are also introduced in Java stream, which greatly facilitates our data statistics work on collections and arrays.

How to get max value from stream in java8

"Case 1: Get the longest element in the String collection."

public class StreamTest {
 public static void main(String[] args) {
  List<String> list = Arrays.asList("adnm", "admmt", "pot", "xbangd", "weoujgsd");
 
  Optional<String> max = list.stream().max(Comparator.comparing(String::length));
  System.out.println("最长的字符串:" + max.get());
 }
}
Copy after login

"Case 2: Get the maximum value in the Integer collection."

public class StreamTest {
 public static void main(String[] args) {
  List<Integer> list = Arrays.asList(7, 6, 9, 4, 11, 6);
 
  // 自然排序
  Optional<Integer> max = list.stream().max(Integer::compareTo);
  // 自定义排序
  Optional<Integer> max2 = list.stream().max(new Comparator<Integer>() {
   @Override
   public int compare(Integer o1, Integer o2) {
    return o1.compareTo(o2);
   }
  });
  System.out.println("自然排序的最大值:" + max.get());
  System.out.println("自定义排序的最大值:" + max2.get());
 }
}
Copy after login

"Case 3: Get the employee with the highest salary."

public class StreamTest {
 public static void main(String[] args) {
  List<Person> personList = new ArrayList<Person>();
  personList.add(new Person("Tom", 8900, 23, "male", "New York"));
  personList.add(new Person("Jack", 7000, 25, "male", "Washington"));
  personList.add(new Person("Lily", 7800, 21, "female", "Washington"));
  personList.add(new Person("Anni", 8200, 24, "female", "New York"));
  personList.add(new Person("Owen", 9500, 25, "male", "New York"));
  personList.add(new Person("Alisa", 7900, 26, "female", "New York"));
 
  Optional<Person> max = personList.stream().max(Comparator.comparingInt(Person::getSalary));
  System.out.println("员工工资最大值:" + max.get().getSalary());
 }
}
Copy after login

"Case 4: Calculate the number of elements greater than 6 in the Integer set."

import java.util.Arrays;
import java.util.List;
 
public class StreamTest {
 public static void main(String[] args) {
  List<Integer> list = Arrays.asList(7, 6, 4, 8, 2, 11, 9);
 
  long count = list.stream().filter(x -> x > 6).count();
  System.out.println("list中大于6的元素个数:" + count);
 }
}
Copy after login

The above is the detailed content of How to get max value from stream in java8. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template