1、使用Stream的sum()求和
可以使用Stream的sum()方法对数组、列表和字典进行求和,如下,
import java.util.*; import java.util.stream.Collectors; import java.util.stream.Stream; public class Main { public static class Person { int age; Person (int age) { this.age = age; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } } public static void main(String[] args) { //数组 int[] intArray = new int[]{1, 2, 3,4}; var result = Arrays.stream(intArray).sum(); System.out.println(result); //列表 List<Long> list = new ArrayList<>(Collections.nCopies(5, 0L)); list.set(0,1L); list.set(1,2L); list.set(2,3L); list.set(3,4L); list.set(4,5L); System.out.println(list.stream().mapToDouble(e->e).sum()); List<Person> pList = new ArrayList<>(Collections.nCopies(5, new Person(1))); System.out.println(pList.stream().mapToDouble(e->e.age).sum()); Map<String, Integer> m1= new HashMap<>(); m1.put("C",1); m1.put("Java",2); m1.put("Python",3); System.out.println(m1.values().stream().mapToDouble(d->d).sum()); System.exit(0); //success } }
2、使用Stream的count()计数
可以使用Stream的count()对满足指定条件的元素进行计数,如下,
import java.util.*; import java.util.stream.Collectors; import java.util.stream.Stream; public class Main { public static class Person { int age; Person (int age) { this.age = age; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } } public static void main(String[] args) { //数组 int[] intArray = new int[]{1, 2, 3,4}; var result = Arrays.stream(intArray).filter(e -> e>1).sum(); System.out.println(result); //列表 List<Long> list = new ArrayList<>(Collections.nCopies(5, 0L)); list.set(0,1L); list.set(1,2L); list.set(2,3L); list.set(3,4L); list.set(4,5L); System.out.println(list.stream().filter(e -> e % 2 == 0).count()); List<Person> pList = new ArrayList<>(Collections.nCopies(5, new Person(1))); System.out.println(pList.stream().filter(e -> e.getAge() > 0).count()); Map<String, Integer> m1= new HashMap <>(); m1.put("C",1); m1.put("Java",2); m1.put("Python",3); System.out.println(m1.values().stream().filter(e -> e % 2 == 0).count()); System.exit(0); //success } }
3、使用Stream计算最大值、最小值和平均值
可以使用Stream的max()计算最大值 、min()计算最小值和average()计算平均值,如下,
import java.util.*; import java.util.stream.Collectors; import java.util.stream.Stream; import java.util.Comparator; public class Main { public static class Person { int age; Person (int age) { this.age = age; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } } public static void main(String[] args) { //数组 int[] intArray = new int[]{1, 2, 3,4}; OptionalInt maxOptional = Arrays.stream(intArray).max(); OptionalInt minOptional = Arrays.stream(intArray).min(); OptionalDouble avgOptional = Arrays.stream(intArray).average(); minOptional.ifPresent(e -> System.out.println("Min: " + e)); maxOptional.ifPresent(e -> System.out.println("Max: " + e)); avgOptional.ifPresent(e -> System.out.println("Avg: " + e)); //list List<Person> list = Arrays.asList(new Person(11),new Person(22), new Person(33), new Person(44), new Person(55)); Optional<Person> maxListOptional = list.stream().max(Comparator.comparingInt(Person::getAge)); Optional<Person> minListOptional = list.stream().min(Comparator.comparingInt(Person::getAge)); OptionalDouble avgListOptional = list.stream().mapToDouble(e->e.age).average(); minListOptional.ifPresent(e -> System.out.println("Min: " + e.getAge())); maxListOptional.ifPresent(e -> System.out.println("Max: " + e.getAge())); avgListOptional.ifPresent(e -> System.out.println("Avg: " + e)); System.exit(0); //success } }