java 如何使用 stream().sorted() 对带有对象的 ArrayList 进行排序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29265947/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 14:57:02  来源:igfitidea点击:

How to sort an ArrayList with object using stream().sorted()

javaarraylistdoublejava-streamsorted

提问by Ludi Dado

I am having real trouble with using stream and sorted to sort my ArrayList and hope someone can help out. The code uses Croatian words, but I don't think that will be a problem for someone who understands what I mean.

我在使用流和排序对我的 ArrayList 进行排序时遇到了真正的麻烦,希望有人能提供帮助。代码使用克罗地亚语单词,但我认为对于理解我的意思的人来说这不是问题。

This is the ArrayList

这是 ArrayList

ArrayList<Publikacija> listaPublikacija = new ArrayList<>();

listaPublikacija.add(prvaKnjiga);
listaPublikacija.add(drugaKnjiga);
listaPublikacija.add(prviCasopis);
listaPublikacija.add(drugiCasopis);

In my assignment I am supposed to sort these objects above by getCijena() which is double.

在我的作业中,我应该通过双倍的 getCijena() 对上面的这些对象进行排序。

This is the best I got and it still doesn't sort it as it should...

这是我得到的最好的,但它仍然没有按应有的方式排序......

listaPublikacija.stream().sorted((s1, s2) -> Double.compare(s1.getCijena(), s2.getCijena()));

Any kind of help or advice appreciated... I already made the sort in different ways, but it is neccessary to sort it via the sorted method in stream...

感谢任何形式的帮助或建议......我已经以不同的方式进行了排序,但是有必要通过流中的排序方法对其进行排序......

I'll post the class script below for easier understanding of the question above:

为了更容易理解上面的问题,我将在下面发布类脚本:

public Publikacija(String nazivKnjige, int godinaIzdanja, int brojStr, VrstaPublikacije vrstaPub, double cijenaPoStr, double cijena){
this.nazivKnjige= nazivKnjige;
this.godinaIzdanja = godinaIzdanja;
this.brojStr = brojStr;
this.vrstaPub = vrstaPub;
this.cijenaPoStr = cijenaPoStr;
if(getClass().equals(Casopis.class)){this.cijena= Casopis.CIJENA_PO_PRIMJERKU;}
else this.cijena = provjeraCijene(cijena(brojStr,vrstaPub,cijenaPoStr).doubleValue());

回答by Crazyjavahacking

You are not storing the results of the sorted array back to collection or array.Stream operations do not mutate the underlying collection:

您没有将排序数组的结果存储回集合或数组。流操作不会改变底层集合:

    List<String> names = Arrays.asList("Katka", "Martin", "John");

    Object[] sortedNames = names.stream().sorted(String::compareTo).toArray();

    System.out.println("array: " + Arrays.toString(sortedNames));

回答by Ludi Dado

    Object[] sortirano = listaPublikacija.stream().sorted((s1, s2) -> Double.compare(s1.getCijena(), s2.getCijena())).toArray();

this worked, ty for the answer

这有效,你的答案

回答by sunil kumar

Below code is used for to sort the your Publikacija object type Arraylist on behalf of "getCijena"

下面的代码用于代表“getCijena”对您的 Publikacija 对象类型 Arraylist 进行排序

' Collections.sort(data, new Comparator<Publikacija>() {
            public int compare(Publ`enter code here`ikacija s1, Publikacija s2) {

                return s1.getCijena.compareTo(s2.getCijena);
            }
        });'

回答by lczapski

You can also do it in that way:

你也可以这样做:

listaPublikacija.stream().sorted(Comparator.comparingDouble(Publikacija::getCijena));

回答by DoNuT

Publikacija[] sortedArray = listaPublikacija.stream()
   .sorted(Comparators.comparing(Publikacija::getCijena, Double::compareTo)
   .toArray();

First method reference extracts the identifying key, second provides the compare function.

第一个方法引用提取识别键,第二个提供比较功能。