按升序和降序动态对地图列表进行排序
我在 List
中有 Map
。
我需要根据输入动态对 List
进行排序,方法是将其作为参数传递到方法 sortData
中。
它按预期工作,但问题是无法按相反顺序对列表进行排序。
我收到错误:The method get(String) is undefined for the type Object
Code
public class TestingClass {
public static void main(String[] args) {
List< LinkedHashMap<String,Object> > list = new ArrayList<>();
/*Map 2*/
LinkedHashMap<String,Object> lhm1 = new LinkedHashMap<>();
lhm1.put("LONG_HEADER", 2l);
lhm1.put("STRING_HEADER1", "C");
lhm1.put("STRING_HEADER2", "D");
/*Map 2*/
LinkedHashMap<String,Object> lhm2 = new LinkedHashMap<>();
lhm2.put("LONG_HEADER", 1l);
lhm2.put("STRING_HEADER1", "E");
lhm2.put("STRING_HEADER2", "F");
/*Map 3*/
LinkedHashMap<String,Object> lhm3 = new LinkedHashMap<>();
lhm3.put("LONG_HEADER", 3l);
lhm3.put("STRING_HEADER1", "A");
lhm3.put("STRING_HEADER2", "B");
list.add(lhm1);
list.add(lhm2);
list.add(lhm3);
List< LinkedHashMap<String,Object> > sortedList1 = sortData(list,"LONG_HEADER","ASC" );
System.out.println("Output 1 After sorting" +sortedList1);
List< LinkedHashMap<String,Object> > sortedList2 = sortData(list,"STRING_HEADER1","ASC" );
System.out.println("Output 2 After sorting"+sortedList2);
List< LinkedHashMap<String,Object> > sortedList3 = sortData(list,"STRING_HEADER2","ASC" );
System.out.println("Output 3 After sorting"+sortedList3);
/* It won't work */
sortData(list,"LONG_HEADER","DESC" );
sortData(list,"STRING_HEADER1","DESC" );
sortData(list,"STRING_HEADER2","DESC" );
}
private static List<LinkedHashMap<String, Object>> sortData(List<LinkedHashMap<String, Object>> inputGridData,
String sortBy, String sortOrder) {
List<LinkedHashMap<String, Object>> lOutputGridDat = null;
if (inputGridData.get(0).get(sortBy) instanceof Long) {
if( "ASC".equals(sortOrder)) {
lOutputGridDat = inputGridData.stream().sorted(Comparator.comparingLong(o -> ((Long) o.get(sortBy))))
.collect(Collectors.toList());
}
else {
/*Here am getting error if i use .reversed() method*/
//Error : The method get(String) is undefined for the type Object
lOutputGridDat = inputGridData.stream().sorted(Comparator.comparingLong(o -> ((Long) o.get(sortBy))).reversed())
.collect(Collectors.toList());
}
} else {
if( "ASC".equals(sortOrder)) {
lOutputGridDat = inputGridData.stream().sorted(Comparator.comparing(o -> ((String) o.get(sortBy))))
.collect(Collectors.toList());
}
else {
/*Here am getting error if i use .reversed() method*/
// Error : The method get(String) is undefined for the type Object
lOutputGridDat = inputGridData.stream().sorted(Comparator.comparing(o -> ((String) o.get(sortBy))).reversed())
.collect(Collectors.toList());
}
}
return lOutputGridDat;
}
}
Map 1 排序后
[{LONG_HEADER=1, STRING_HEADER1=E, STRING_HEADER2=F}, {LONG_HEADER=2, STRING_HEADER1=C, STRING_HEADER2=D}, {LONG_HEADER=3, STRING_HEADER1=A, STRING_HEADER2=B}]
Map 2 排序后
[{LONG_HEADER=3, STRING_HEADER1=A, STRING_HEADER2=B}, {LONG_HEADER=2, STRING_HEADER1=C, STRING_HEADER2=D}, {LONG_HEADER=1, STRING_HEADER1=E, STRING_HEADER2=F}]
地图 3 排序后
[{LONG_HEADER=3, STRING_HEADER1=A, STRING_HEADER2=B}, {LONG_HEADER=2, STRING_HEADER1=C, STRING_HEADER2=D}, {LONG_HEADER=1, STRING_HEADER1=E, STRING_HEADER2=F}]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,值得指出您提供的代码的一些重要问题:
Object不是一个好的做法
作为泛型类型。引入泛型是为了强制类型安全,使用Object
作为泛型类型与根本不使用泛型一样糟糕。instanceof
检查和类型转换。LinkedHashMap
这样的具体类编写代码 - 变量类型必须是Map
。如果您怀疑这些建议是否有价值,您可以在此网站上找到有关上述所有帐户的更详细的解释。
关于您的问题,比较器 您的代码中定义的将无法编译。
这就是你可以解决的问题:
当你链接方法时,编译器无法根据元素的类型推断
comparing
中参数o
的类型流的。参数
o
不被视为Map
,而是被视为Object
,因此您无法对其调用get()
。需要显式提供通用类型信息:
。其中第一部分 - 传递给
比较
的参数类型(即流的元素),第二部分是将用于比较的值类型(即字符串)。有关泛型方法语法的信息,请查看本教程
Firstly, it's worth to point out at some important issues with the code you've provided :
Object
as generic type. Generics were introduced to enforce the type safety, usingObject
as generic type is as bad as don't use generics at all.instanceof
checks and type casting.LinkedHashMap
- variable type has to beMap
instead.You can find more elaborate explanation on all account mentioned above on this site if you doubt if these suggestions have a value.
With regard to your question, comparators as defined in your'r code will not compile.
That's you can fix it:
When you're chaining the methods, the compiler is unable to infer the type of the prameter
o
inside thecomparing
based on the type of the elements of the stream.Parameter
o
is treated not as aMap
but asObject
therefore you can't invokeget()
on it.Generic type information needs to be provided explicitly:
<Map<String, Object>, String>
. Where the first part - type of the argument passed intocomparing
(i.e. element of the stream), the second is a type of value that will be used for comparison (i.e. string).for information on the syntax of generic methods, take a look at this tutorial