我有一个代表 [from, to] 的 HashMap :
1st: [start->sb1]
2nd: [sb0->sb3]
3rd: [sb1->sb0]
4th: [sb3->end]
我想是否有办法找到正确的序列,例如:
start->sb1->sb0->sb3->end
最佳答案
如果您知道开始
键,那就非常简单了:
String key = "start";
while (key != null && !key.equals("end")) {
System.out.print(key + "->");
key = map.get(key);
}
if (key != null)
System.out.println(key);
这假设映射的键和值是字符串。
关于java - 在 hashmap 中查找序列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27917449/