Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/373.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JavaGuava/collections:如何使用预定义列表作为谓词对另一个列表进行排序?_Java_Sorting_Collections_Guava - Fatal编程技术网

JavaGuava/collections:如何使用预定义列表作为谓词对另一个列表进行排序?

JavaGuava/collections:如何使用预定义列表作为谓词对另一个列表进行排序?,java,sorting,collections,guava,Java,Sorting,Collections,Guava,所以我有一个这样的列表,['a',Z',D',E',B',H',C'],我做了一些事情(这是为了这个问题不可更改的目的)我得到一个返回值[{id:D,num:5},{id:a,num:5},{id:C,num:3},{id:Z,num:3}],我想保持这样的事实,它是按num字段排序的,而且返回值总是要按num字段排序。但现在我想在该排序中进行排序,以保留原始列表顺序,这样我想要的输出是: [{id:A,num:5},{id:D,num:5},{id:Z,num:3},{id:C,num:3}]

所以我有一个这样的列表,['a',Z',D',E',B',H',C'],我做了一些事情(这是为了这个问题不可更改的目的)我得到一个返回值[{id:D,num:5},{id:a,num:5},{id:C,num:3},{id:Z,num:3}],我想保持这样的事实,它是按num字段排序的,而且返回值总是要按num字段排序。但现在我想在该排序中进行排序,以保留原始列表顺序,这样我想要的输出是:

[{id:A,num:5},{id:D,num:5},{id:Z,num:3},{id:C,num:3}]


我对fluenterables/番石榴之类的东西不是很在行,但我觉得这是最好的选择。有谁能给我一个很好的例子吗?

您可以编写一个比较器,用相同的
num
对元素进行排序。这个比较器实际上取决于第一个列表

首先将您的第一个列表转换为地图

 map - > [A,1] [Z,2] ......[C,7]
一旦你得到了排序后的列表

 list -> [{id:D, num:5},{id:A,num:5}, {id:C,num:3}, {id:Z,num:3}]
遵循以下步骤:

result;
currentIndex=0;
while currentIndex < list.size()
   currentNum = list.get(currentIndex).getNum();
   while(currentIndex < lis.size() && list.get(currentIndex).getNum() == currentNum)
          add current item to currentList
          currentIndex++;
   Collections.sort(currentList,new CustomComparator());
   result.addAll(currentList)
结果;
currentIndex=0;
而currentIndex
您的比较对象将是:

  int compare(obj1,obj2){
     if(map.get(obj1.getId() < map.get(obj1.getId())
         return -1;
     else if map.get(obj1.getId() > map.get(obj1.getId()
          return 1;
     else return 0;
  }
int比较(obj1、obj2){
if(map.get(obj1.getId()map.get(obj1.getId())
返回1;
否则返回0;
}

您可以编写一个比较器,用相同的
num对元素进行排序。这个比较器实际上取决于第一个列表

首先将您的第一个列表转换为地图

 map - > [A,1] [Z,2] ......[C,7]
一旦你得到了排序后的列表

 list -> [{id:D, num:5},{id:A,num:5}, {id:C,num:3}, {id:Z,num:3}]
遵循以下步骤:

result;
currentIndex=0;
while currentIndex < list.size()
   currentNum = list.get(currentIndex).getNum();
   while(currentIndex < lis.size() && list.get(currentIndex).getNum() == currentNum)
          add current item to currentList
          currentIndex++;
   Collections.sort(currentList,new CustomComparator());
   result.addAll(currentList)
结果;
currentIndex=0;
而currentIndex
您的比较对象将是:

  int compare(obj1,obj2){
     if(map.get(obj1.getId() < map.get(obj1.getId())
         return -1;
     else if map.get(obj1.getId() > map.get(obj1.getId()
          return 1;
     else return 0;
  }
int比较(obj1、obj2){
if(map.get(obj1.getId()map.get(obj1.getId())
返回1;
否则返回0;
}

为了使用预定义列表作为排序顺序,有。您可以将其与按id字段对输出列表进行排序相结合

要将“按num排序”字段与“按id排序”字段组合在一起,可以使用

因此,所需的
排序
实例的完整定义大致如下:

Ordering.natural().onResultOf(extractNumFunction)
        .compound(Ordering.explicit(inputList).onResultOf(extractIdFunction))
也许您需要添加泛型类型信息


然后,您可以使用此
排序
实例作为参数,或使用类似的方法。

为了使用预定义列表作为排序顺序,有。您可以将此与结合使用,以按id字段对输出列表进行排序

要将“按num排序”字段与“按id排序”字段组合在一起,可以使用

因此,所需的
排序
实例的完整定义大致如下:

Ordering.natural().onResultOf(extractNumFunction)
        .compound(Ordering.explicit(inputList).onResultOf(extractIdFunction))
也许您需要添加泛型类型信息


然后,您可以使用此
排序
实例作为参数,或使用类似的方法。

如果您想使用番石榴来解决此问题,则可以使用
排序
类。它基本上是一个增强的
比较器
。更具体地说,它提供了以下示例中使用的内容:

  • 指定由元素的
    列表定义的自定义排序顺序
  • 不要直接比较元素,而是比较应用于元素的
    函数的结果
  • 从其他几个
    订单
    中创建一个复合
    订单
    ,因此如果第一个返回
    0
    请尝试链中的下一个
因此,您可以将
num
属性的DESC顺序与
id
属性的自定义顺序相结合,如下所示:

List<String> keys = ImmutableList.of("A", "Z", "D", "E", "B", "H", "C");

List<Map<String, String>> items = ImmutableList.of(
    ImmutableMap.of("id", "D", "num", "5"),
    ImmutableMap.of("id", "A", "num", "5"),
    ImmutableMap.of("id", "C", "num", "3"),
    ImmutableMap.of("id", "Z", "num", "3")
);

Ordering<Map<String, String>> numOrdering =
    Ordering.natural().reverse().onResultOf(item -> item.get("num"));
Ordering<Map<String, String>> idOrdering =
    Ordering.explicit(keys).onResultOf(item -> item.get("id"));

Ordering<Map<String, String>> compoundOrdering = numOrdering.compound(idOrdering);

List<Map<String, String>> result = compoundOrdering.sortedCopy(items);

System.out.println(result);
List key=ImmutableList.of(“A”、“Z”、“D”、“E”、“B”、“H”、“C”);
列表项=ImmutableList.of(
不可变映射(“id”、“D”、“num”、“5”),
不可变映射(“id”、“A”、“num”、“5”),
不可变映射(“id”、“C”、“num”、“3”),
不可变映射(“id”、“Z”、“num”、“3”)
);
有序化=
Ordering.natural().reverse().onResultOf(item->item.get(“num”));
排序=
Ordering.explicit(key).onResultOf(item->item.get(“id”);
排序复合排序=numOrdering.component(idOrdering);
列表结果=compoundOrdering.sortedCopy(项目);
系统输出打印项次(结果);

如果您想使用番石榴来解决这个问题,那么您可以使用
排序
类。它基本上是一个增强的
比较器
。更具体地说,它提供了以下示例中使用的内容:

  • 指定由元素的
    列表定义的自定义排序顺序
  • 不要直接比较元素,而是比较应用于元素的
    函数的结果
  • 从其他几个
    订单
    中创建一个复合
    订单
    ,因此如果第一个返回
    0
    请尝试链中的下一个
因此,您可以将
num
属性的DESC顺序与
id
属性的自定义顺序相结合,如下所示:

List<String> keys = ImmutableList.of("A", "Z", "D", "E", "B", "H", "C");

List<Map<String, String>> items = ImmutableList.of(
    ImmutableMap.of("id", "D", "num", "5"),
    ImmutableMap.of("id", "A", "num", "5"),
    ImmutableMap.of("id", "C", "num", "3"),
    ImmutableMap.of("id", "Z", "num", "3")
);

Ordering<Map<String, String>> numOrdering =
    Ordering.natural().reverse().onResultOf(item -> item.get("num"));
Ordering<Map<String, String>> idOrdering =
    Ordering.explicit(keys).onResultOf(item -> item.get("id"));

Ordering<Map<String, String>> compoundOrdering = numOrdering.compound(idOrdering);

List<Map<String, String>> result = compoundOrdering.sortedCopy(items);

System.out.println(result);
List key=ImmutableList.of(“A”、“Z”、“D”、“E”、“B”、“H”、“C”);
列表项=ImmutableList.of(
不可变映射(“id”、“D”、“num”、“5”),
不可变映射(“id”、“A”、“num”、“5”),
不可变映射(“id”、“C”、“num”、“3”),
不可变映射(“id”、“Z”、“num”、“3”)
);
有序化=
订购。