如何用stream流对list集合中对每个对象中的时间字段进行倒序排序
假设List集合中的每个对象都有一个时间字段为timestamp,可以使用流的sorted方法对List集合进行排序,排序时使用Comparator.comparingLong方法来比较每个对象的时间戳,然后使用reversed方法将排序结果倒序排列。
示例代码如下:
List<MyObject> list = ... // 获取List集合
List<MyObject> sortedList = list.stream()
.sorted(Comparator.comparingLong(MyObject::getTimestamp).reversed())
.collect(Collectors.toList());
// sortedList为按时间戳倒序排序后的List集合
其中,MyObject为List集合中的对象类型,getTimestamp为获取时间戳的方法。
如何用stream流对list对象集合里面的时间字段进行倒序排序
假设List中的元素对象为MyObject,其中包含一个时间字段time,可以按照以下步骤进行倒序排序:
- 导入必要的类库:
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
- 定义Comparator对象用于按照时间字段进行排序:
Comparator<MyObject> comparator = Comparator.comparing(MyObject::getTime).reversed();
- 使用stream流对List进行排序:
List<MyObject> sortedList = list.stream().sorted(comparator).collect(Collectors.toList());
- sortedList为按照时间字段倒序排列的List集合。
完整代码示例:
import java.time.LocalDateTime;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
public class Main {
public static void main(String[] args) {
List<MyObject> list = List.of(
new MyObject("obj1", LocalDateTime.of(2021, 1, 1, 0, 0, 0)),
new MyObject("obj2", LocalDateTime.of(2021, 2, 1, 0, 0, 0)),
new MyObject("obj3", LocalDateTime.of(2021, 3, 1, 0, 0, 0))
);
Comparator<MyObject> comparator = Comparator.comparing(MyObject::getTime).reversed();
List<MyObject> sortedList = list.stream().sorted(comparator).collect(Collectors.toList());
sortedList.forEach(System.out::println);
}
}
class MyObject {
private String name;
private LocalDateTime time;
public MyObject(String name, LocalDateTime time) {
this.name = name;
this.time = time;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public LocalDateTime getTime() {
return time;
}
public void setTime(LocalDateTime time) {
this.time = time;
}
@Override
public String toString() {
return "MyObject{" +
"name='" + name + '\'' +
", time=" + time +
'}';
}
}
java8 使用stream流对对象集合按对象创建时间倒序排序
要使用Java 8的Stream
流对对象集合按对象创建时间倒序排序,你可以使用Comparator
和sorted
方法。假设你有一个包含对象的集合,每个对象都有一个表示创建时间的属性。以下是一个示例:
假设你有一个名为Person
的类,其中包含一个名为creationTime
的Date
属性,用于表示对象的创建时间。你可以使用以下代码对Person
对象集合进行按照创建时间倒序排序:
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开始计数的,所以需要对年份进行适当的调整。
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)