Java8通过管道流(stream)来实现集合的一些聚合函数

stream的一些聚合函数包括:
count(), findFirst(), max(), min(), reduce(), sum()

SimpleStreamDemo.java

package corejava8.functional;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class SimpleStreamDemo {

static class Hero {
String name;
int age;

public Hero(String name, int age) {
this.name = name;
this.age = age;
}
}

static Hero[] heroes = {
new Hero("Grelber", 21), new Hero("Roderick", 12),
new Hero("Francisco", 35), new Hero("Superman", 65),
new Hero("Jumbletron", 22), new Hero("Mavericks", 1),
new Hero("Palladin", 50), new Hero("Athena", 50) };

public static void main(String[] args) {

long adultYearsExperience = Arrays.stream(heroes).filter(b -> b.age >= 18)
.mapToInt(b -> b.age).sum();
System.out.println("We're in good hands! The adult superheros have "
+ adultYearsExperience + " years of experience");

List<Object> sorted = Arrays.stream(heroes)
.sorted((h1, h2) -> h1.name.compareTo(h2.name)).map(h -> h.name)
.collect(Collectors.toList());
System.out.println("Heroes by name: " + sorted);
}
}

运行结果:
We're in good hands! The adult superheros have 243 years of experience
Heroes by name: [Athena, Francisco, Grelber, Jumbletron, Mavericks, Palladin, Roderick, Superman]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要使用Java 8的`Stream`对对象集合按对象创建时间倒序排序,你可以使用`Comparator`和`sorted`方法。假设你有一个包含对象的集合,每个对象都有一个表示创建时间的属性。以下是一个示例: 假设你有一个名为`Person`的类,其中包含一个名为`creationTime`的`Date`属性,用于表示对象的创建时间。你可以使用以下代码对`Person`对象集合进行按照创建时间倒序排序: ```java import java.util.ArrayList; import java.util.Comparator; import java.util.Date; import java.util.List; import java.util.stream.Collectors; public class PersonSorting { public static void main(String[] args) { List<Person> persons = new ArrayList<>(); persons.add(new Person("John", new Date(2022, 0, 1))); persons.add(new Person("Alice", new Date(2021, 4, 15))); persons.add(new Person("Bob", new Date(2021, 8, 30))); List<Person> sortedPersons = persons.stream() .sorted(Comparator.comparing(Person::getCreationTime).reversed()) .collect(Collectors.toList()); System.out.println("排序前的集合:" + persons); System.out.println("排序后的集合:" + sortedPersons); } } class Person { private String name; private Date creationTime; public Person(String name, Date creationTime) { this.name = name; this.creationTime = creationTime; } public Date getCreationTime() { return creationTime; } @Override public String toString() { return name; } } ``` 在上面的例子中,首先创建了一个`Person`类,其中包含了`name`和`creationTime`属性。然后创建了一个`persons`集合,并添加了一些`Person`对象。 使用`stream()`方法将集合转换为,然后使用`sorted()`方法并传递一个按照`creationTime`属性排序的比较器。在这个例子中,我们使用了`Comparator.comparing()`方法和方法引用(`Person::getCreationTime`)来指定按照`creationTime`属性进行比较。通过调用`.reversed()`方法,我们可以实现倒序排序。 最后,使用`collect(Collectors.toList())`方法将排序后的元素收集到一个新的列表中。 在控制台上,你将看到排序前和排序后的集合输出结果。注意,这里使用了Java 8的日期类库,但请注意Java中的年份是从1900开始计数的,所以需要对年份进行适当的调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值