gpt4 book ai didi

java - 对 List> 进行排序

转载 作者:行者123 更新时间:2023-11-30 02:51:30 25 4
gpt4 key购买 nike

我有一个字符串映射列表:

List<Map<String, String>> list = new ArrayList<Map<String, String>>();

这会填充以下内容:

Map<String, String> action1 = new LinkedHashMap<>();
map.put("name", "CreateFirstName");
map.put("nextAction", "CreateLastName");

Map<String, String> action2 = new LinkedHashMap<>();
map.put("name", "CreateAddress");
map.put("nextAction", "CreateEmail");

Map<String, String> action3 = new LinkedHashMap<>();
map.put("name", "CreateLastName");
map.put("nextAction", "CreateAddress");

Map<String, String> action4 = new LinkedHashMap<>();
map.put("name", "CreateEmail");

list.add(action1);
list.add(action2);
list.add(action3);
list.add(action4);

action4 没有 nextAction,因为它是最后一个操作,但给它一个 nextAction 作为没有下一个操作的占位符可能会更容易?

问题:如何对列表进行排序,以便操作按顺序排列?即:一个 Action 的下一个 Action ,与列表中下一个 Action 的名称相同。

最佳答案

虽然这似乎是 XY-Problem 的情况,并且这个 map 列表肯定不是“精心设计的数据模型”,并且可能存在在许多方面“更好”的表示(尽管没有人可以给出关于“最佳”模型是什么的建议,只要总体目标未知),这是您手头的任务,解决方法如下:

首先,您必须确定排序列表的第一个元素。这正是具有 "name" 条目的 map ,该条目不会显示为任何其他 map 的 "nextAction" 条目。

获得第一张 map 后,您可以将其添加到(已排序)列表中。然后,确定下一个元素归结为查找 "name" 与前一个映射的 "nextAction" 相同的映射。要快速找到这些后继者,您可以构建一个映射,将每个 "name" 条目映射到映射本身。

以下是此排序方法的基本实现:

import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class SortListWithMaps
{
public static void main(String[] args)
{
List<Map<String, String>> list = new ArrayList<Map<String, String>>();

Map<String, String> action1 = new LinkedHashMap<>();
action1.put("name", "CreateFirstName");
action1.put("nextAction", "CreateLastName");

Map<String, String> action2 = new LinkedHashMap<>();
action2.put("name", "CreateAddress");
action2.put("nextAction", "CreateEmail");

Map<String, String> action3 = new LinkedHashMap<>();
action3.put("name", "CreateLastName");
action3.put("nextAction", "CreateAddress");

Map<String, String> action4 = new LinkedHashMap<>();
action4.put("name", "CreateEmail");

list.add(action1);
list.add(action2);
list.add(action3);
list.add(action4);

// Make it a bit more interesting...
Collections.shuffle(list);

System.out.println("Before sorting");
for (Map<String, String> map : list)
{
System.out.println(map);
}

List<Map<String, String>> sortedList = sort(list);

System.out.println("After sorting");
for (Map<String, String> map : sortedList)
{
System.out.println(map);
}
}

private static List<Map<String, String>> sort(
List<Map<String, String>> list)
{
// Compute a map from "name" to the actual map
Map<String, Map<String, String>> nameToMap =
new LinkedHashMap<String, Map<String,String>>();
for (Map<String, String> map : list)
{
String name = map.get("name");
nameToMap.put(name, map);
}

// Determine the first element for the sorted list. For that,
// create the set of all names, and remove all of them that
// appear as the "nextAction" of another entry
Set<String> names =
new LinkedHashSet<String>(nameToMap.keySet());
for (Map<String, String> map : list)
{
String nextAction = map.get("nextAction");
names.remove(nextAction);
}
if (names.size() != 1)
{
System.out.println("Multiple possible first elements: " + names);
return null;
}

// Insert the elements, in sorted order, into the result list
List<Map<String, String>> result =
new ArrayList<Map<String, String>>();
String currentName = names.iterator().next();
while (currentName != null)
{
Map<String, String> element = nameToMap.get(currentName);
result.add(element);
currentName = element.get("nextAction");
}
return result;
}
}

关于java - 对 List<Map<String, String>> 进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38512320/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com