问题:如何对List<List<String>>进行排序?

huangapple go评论74阅读模式
英文:

Problems with sorting List<List<String>> java?

问题

// data is List<List<String>>
data.sort(Comparator.comparing(e -> e.get(col)).reversed());
// data is List<List<String>>
data.sort(Comparator.comparing(e -> e.get(col)).thenComparing(e -> e.get(col2)));
英文:

I have one List<List<String>> I want to sort it according to column . I used the following code.

    // data is List&lt;List&lt;String&gt;&gt;
    data.sort(Comparator.comparing(e-&gt; e.get(col)));                

Its working and sorting according column specified. But If I used reversed() or thenComparing() method, It says

error: cannot find symbol
                    data.sort(Comparator.comparing(e-&gt; e.get(col)).reversed()  );
                                                        ^
  symbol:   method get(int)
  location: variable e of type Object

Also with the thenComparing method,

error: cannot find symbol
                    data.sort(Comparator.comparing(e-&gt; e.get(col)).thenComparing(e-&gt;e.get(col2))  );
                                                        ^
  symbol:   method get(int)
  location: variable e of type Object

error: cannot find symbol
                    data.sort(Comparator.comparing(e-&gt; e.get(col)).thenComparing(e-&gt;e.get(col2))  );
                                                                                     ^
  symbol:   method get(int)
  location: variable e of type Object
2 errors

I'm not getting anything from the error messages. Fyi, Im using OpenJDK 11 for this.

答案1

得分: 3

似乎在有第二层的情况下,Java不会自动推断类型。

在第一个例子中:

data.sort(Comparator.comparing(e -> e.get(0)));

类型是从data中推断出来的,但在第二个例子中:

data.sort(Comparator.comparing(e -> e.get(0)).reversed());

comparing的类型没有被推断出来。您可以通过几种方式来解决这个问题,最简单的方法是显式指定类型:

data.sort(Comparator.comparing((List<String> e) -> e.get(0)).reversed());

Java不会自动进行推断似乎有点奇怪。如果我们包括创建比较器的中间步骤,我们可以很清楚地看到它不会自动推断:

Comparator<List<String>> c = Comparator.comparing(e -> e.get(0)).reversed();

现在我们会得到两个错误,一个是因为lambda表达式中的参数是一个Object,第二个错误是因为我们创建了一个Comparator<Object>

我认为它是这样工作的,但我不确定如何验证。reversed方法将采取调用它的实例的类型参数,类型不会被自动推断。在调用reversed时,对象必须已经被实例化并赋予泛型。然后,对comparing的调用没有上限,它只需返回一个Comparator<?>

另一种解决方法是在调用Comparator时指定类型:

Comparator<List<String>> c = Comparator.<List<String>, String>comparing(e -> e.get(0)).reversed();
英文:

Seems like java doesn't infer the type once you have a second layer.

In the first example.

data.sort( Comparator.comparing( e-&gt; e.get(0) ) );

The type is inferred from data, but in the second example.

data.sort( Comparator.comparing( e-&gt; e.get(0) ).reversed() );

The type of 'comparing' is not inferred. You can resolve this a couple ways, the easiest to be explicit.

data.sort( Comparator.comparing( (List&lt;String&gt; e) -&gt; e.get(0) ).reversed() );

It seems funny that java doesn't chain inferences. If we include the intermediate step of creating the comparator, we can see pretty clearly it doesn't.

Comparator&lt;List&lt;String&gt;&gt; c = Comparator.comparing( e-&gt; e.get(0) ).reversed();

> | Error:
| cannot find symbol
| symbol: method get(int)
| Comparator<List<String>> c = Comparator.comparing( e->e.get(0) ).reversed();
| ^---^
| Error:
| incompatible types: java.util.Comparator<java.lang.Object> cannot be converted to java.util.Comparator<java.util.List<java.lang.String>>
| Comparator<List<String>> c = Comparator.comparing( e->e.get(0) ).reversed();
| ^--------------------------------------------^

Now we get two errors, one in the lambda because the argument is an Object, and the second error because we're creating a Comparator&lt;Object&gt;.

I'm thinking it works this way, but I'm not sure how to verify. 'reversed' will take the type argument of the instance calling it, the type is not inferred. By the time 'reversed' is called the object has to have been instantiated and the generic assigned. The call to comparing then has no upper bounds, it just has to return a Comparator<?>.

An alternative solution, specify the types when the Comparator is called.

Comparator&lt;List&lt;String&gt;&gt; c = Comparator.&lt;List&lt;String&gt;, String&gt;comparing( e-&gt;e.get(0) ).reversed();

huangapple
  • 本文由 发表于 2020年8月7日 17:45:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/63299244.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定