在 Java 中使用 stream.sorted() 对列表进行排序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40517977/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Sorting a list with stream.sorted() in Java
提问by Ivan C
I'm interested in sorting a list from a stream. This is the code I'm using:
我对从流中排序列表很感兴趣。这是我正在使用的代码:
list.stream()
.sorted((o1, o2)->o1.getItem().getValue().compareTo(o2.getItem().getValue()))
.collect(Collectors.toList());
Am I missing something? The list is not sorting.
我错过了什么吗?该列表未排序。
It should sort the lists according to the item with the lowest value.
它应该根据具有最低值的项目对列表进行排序。
for (int i = 0; i < list.size(); i++)
{
System.out.println("list " + (i+1));
print(list, i);
}
And the print method:
和打印方法:
public static void print(List<List> list, int i)
{
System.out.println(list.get(i).getItem().getValue());
}
采纳答案by Matt
This is not like Collections.sort()where the parameter reference gets sorted. In this case you just get a sorted stream that you need to collect and assign to another variable eventually:
这不像Collections.sort()参数引用被排序的地方。在这种情况下,您只会得到一个排序的流,您需要收集它并最终分配给另一个变量:
List result = list.stream().sorted((o1, o2)->o1.getItem().getValue().
compareTo(o2.getItem().getValue())).
collect(Collectors.toList());
You've just missed to assign the result
你刚刚错过了分配结果
回答by Tanmay Baid
It seems to be working fine:
它似乎工作正常:
List<BigDecimal> list = Arrays.asList(new BigDecimal("24.455"), new BigDecimal("23.455"), new BigDecimal("28.455"), new BigDecimal("20.455"));
System.out.println("Unsorted list: " + list);
final List<BigDecimal> sortedList = list.stream().sorted((o1, o2) -> o1.compareTo(o2)).collect(Collectors.toList());
System.out.println("Sorted list: " + sortedList);
Example Input/Output
示例输入/输出
Unsorted list: [24.455, 23.455, 28.455, 20.455]
Sorted list: [20.455, 23.455, 24.455, 28.455]
Are you sure you are not verifying list instead of sortedList[in above example] i.e. you are storing the result of stream()in a new Listobject and verifying that object?
您确定您不是在验证列表而不是sortedList[在上面的示例中] 即您将结果存储stream()在一个新List对象中并验证该对象吗?
回答by River
Use list.sortinstead:
使用list.sort来代替:
list.sort((o1, o2) -> o1.getItem().getValue().compareTo(o2.getItem().getValue()));
and make it more succinct using Comparator.comparing:
并使用Comparator.comparing以下方法使其更简洁:
list.sort(Comparator.comparing(o -> o.getItem().getValue()));
After either of these, listitself will be sorted.
在其中任何一个之后,list本身将被排序。
Your issue is that
list.stream.sortedreturnsthe sorted data, it doesn't sort in place as you're expecting.
您的问题是
返回排序后的数据,它没有按照您的预期进行排序。list.stream.sorted
回答by Venkat IndianEagle
Collection<Map<Item, Integer>> itemCollection = basket.values();
Iterator<Map<Item, Integer>> itemIterator = itemCollection.stream().sorted(new TestComparator()).collect(Collectors.toList()).iterator();
package com.ie.util;
import com.ie.item.Item;
import java.util.Comparator;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
public class TestComparator implements Comparator<Map<Item, Integer>> {
// comparator is used to sort the Items based on the price
@Override
public int compare(Map<Item, Integer> o1, Map<Item, Integer> o2) {
// System.out.println("*** compare method will be called *****");
Item item1 = null;
Item item2 = null;
Set<Item> itemSet1 = o1.keySet();
Iterator<Item> itemIterator1 = itemSet1.iterator();
if(itemIterator1.hasNext()){
item1 = itemIterator1.next();
}
Set<Item> itemSet2 = o2.keySet();
Iterator<Item> itemIterator2 = itemSet2.iterator();
if(itemIterator2.hasNext()){
item2 = itemIterator2.next();
}
return -item1.getPrice().compareTo(item2.getPrice());
}
}
**** this is helpful to sort the nested map objects like Map> here i sorted based on the Item object price .
**** 这有助于对嵌套地图对象进行排序,例如 Map> 此处我根据 Item 对象 price 进行排序。
回答by Udaya Shankara Gandhi Thalabat
Java 8 provides different utility api methods to help us sort the streams better.
Java 8 提供了不同的实用 api 方法来帮助我们更好地对流进行排序。
If your list is a list of Integers(or Double, Long, String etc.,) then you can simply sort the list with default comparators provided by java.
如果您的列表是整数(或双精度、长整型、字符串等)列表,那么您可以简单地使用 java 提供的默认比较器对列表进行排序。
List<Integer> integerList = Arrays.asList(1, 4, 3, 4, 5);
Creating comparator on fly:
即时创建比较器:
integerList.stream().sorted((i1, i2) -> i1.compareTo(i2)).forEach(System.out::println);
With default comparator provided by java 8 when no argument passed to sorted():
当没有参数传递给 sorted() 时,使用 java 8 提供的默认比较器:
integerList.stream().sorted().forEach(System.out::println); //Natural order
If you want to sort the same list in reverse order:
如果要以相反的顺序对同一列表进行排序:
integerList.stream().sorted(Comparator.reverseOrder()).forEach(System.out::println); // Reverse Order
If your list is a list of user defined objects, then:
如果您的列表是用户定义对象的列表,则:
List<Person> personList = Arrays.asList(new Person(1000, "First", 25, 30000),
new Person(2000, "Second", 30, 45000),
new Person(3000, "Third", 35, 25000));
Creating comparator on fly:
即时创建比较器:
personList.stream().sorted((p1, p2) -> ((Long)p1.getPersonId()).compareTo(p2.getPersonId()))
.forEach(person -> System.out.println(person.getName()));
Using Comparator.comparingLong() method(We have comparingDouble(), comparingInt() methods too):
使用 Comparator.comparingLong() 方法(我们也有 compareDouble()、comparingInt() 方法):
personList.stream().sorted(Comparator.comparingLong(Person::getPersonId)).forEach(person -> System.out.println(person.getName()));
Using Comparator.comparing() method(Generic method which compares based on the getter method provided):
使用 Comparator.comparing() 方法(基于提供的 getter 方法进行比较的通用方法):
personList.stream().sorted(Comparator.comparing(Person::getPersonId)).forEach(person -> System.out.println(person.getName()));
We can do chaining too using thenComparing() method:
我们也可以使用 thenComparing() 方法进行链接:
personList.stream().sorted(Comparator.comparing(Person::getPersonId).thenComparing(Person::getAge)).forEach(person -> System.out.println(person.getName())); //Sorting by person id and then by age.
Person class
人物类
public class Person {
private long personId;
private String name;
private int age;
private double salary;
public long getPersonId() {
return personId;
}
public void setPersonId(long personId) {
this.personId = personId;
}
public Person(long personId, String name, int age, double salary) {
this.personId = personId;
this.name = name;
this.age = age;
this.salary = salary;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
}

