好的,我有一个HashMap,我试图遍历地图并检查其他节点,看看它们的值是否重叠。
例如: {job1 = [[16,18],[21,23]],job2 = [[16,17]]有重叠[16,18]和[16,17]。
在我的Node类中,它需要一个NumberPairs列表,而我的NumberPairs类需要一对数字(整数)。无论如何,我遇到的问题是我不知道如何相互检查关键值..任何想法?
到目前为止,我已经
了for (Map.Entry<String, Node> entry : myVertices.entrySet())
{
//do stuff
}
答案 0 :(得分:2)
首先定义我们的重叠逻辑,这可以很简单:
private static boolean overlaps(final NumberPairs first, final NumberPairs second) {
final boolean before = first.end < second.start;
final boolean after = first.start > second.end;
return !before && !after;
}
接下来迭代所有条目及其所有对,以查找重叠条件是否匹配。非常不优化的方法是:
for (final Map.Entry<String, Node> entry : myVertices.entrySet()) {
final Node entryValue = entry.getValue();
for (final Map.Entry<String, Node> candidate : myVertices.entrySet()) {
if (entry == candidate) {
continue; // entry will overlap with itself
}
final Node candidateValue = candidate.getValue();
for (final NumberPairs entryPair : entryValue.pairs) {
for (final NumberPairs candidatePair : candidateValue.pairs) {
if (overlaps(entryPair, candidatePair)) {
System.out.printf("%s overlaps with %s in the pairs %s and %s\n", entry.getKey(), candidate.getKey(), entryPair, candidatePair);
}
}
}
}
}
在少量节点和NodePairs上,这已经足够了,但是对于更大数量的值,interval tree structure会更好。
答案 1 :(得分:0)
您可以使用Map的containsValue()方法来检查元素。我假设Node类及其setter方法正确解释。
HashMap map= new HashMap();
Node obj = new Node();
node.setFirst(10);
node.setLast(20);
map.put("First Entry",node);
int value = 16;// assuming to find in map
for (Map.Entry<String, Node> entry : myVertices.entrySet())
{
Node node= entry.getValue();
if(node.getFirst()==value){
System.out.println("Value found");
//do other stuff
break;
}
}
答案 2 :(得分:0)
假设Node
和NumberPair
的结构如下:
public class Node {
List <NumberPair> numbers = new ArrayList<>();
// Implementation
}
public class NumberPair {
int x, y;
// ctor, getters, setters, etc
}
Map <String, Map<String, Integer>> returnOverlaps(Map <String, Node> myVertices) {
Map <String, Map<String, Integer> > overlaps = new HashMap<>();
for (Map.Entry<String, Node> entry : myVertices.entrySet())
{
for (Map.Entry<String, Node> entry2 : myVertices.entrySet()) {
compare(entry, entry2, overlaps);
}
}
return overlaps;
}
void compare (Map.Entry <String, Integer> entry1, Map.Entry <String, Integer> entry2, Map <String, Map<String, Integer>> overlaps) {
String key1 = entry1.getKey();
Map <String, Integer> map = overlaps.get(key1);
if (map == null)
map = new HashMap<>();
String key2 = entry2.getkey();
Node node1 = entry1.getValue();
Node node2 = entry2.getValue();
if (key1.equals(key2)) {
// self
map.put(key1, node1.getNumbers().size());
}
else {
map.put(key2, 0);
for (NumberPair x : node1.getNumbers()) {
for (NumberPair y : node2.getNumbers()) {
if (x.first() == y.first()) {
// Crude brute force compare, I like 'Zoran Regvart''s approach above of sorting and comparing upper/lower bounds.
map.put(key2, map.get(key2) + 1 );
}
}
}
}
overlaps.put(key1, map);
}
有点复杂,这里是overlaps
的样子。
它将每个jobName映射到一个地图,其中包含其他jobNames以及与它们共享的重叠数
所以如果你有:
job1=[[16, 18], [21, 23]], job2=[[16, 17]],
job3= [[20, 21]], job4= [[21, 29], [20, 24]], [16, 2]]
overlaps
看起来像
job1 -> [job1 = 1], [job2 = 1], [job3 = 0], [job4 = 2]
job2 -> [job1 = 1], [job2 = 1], [job3 = 0], [job4 = 2]
job3 -> [job3 = 1], [job2 = 0], [job1 = 0], [job4 = 1]
job4 -> [job3 = 1], [job2 = 0], [job1 = 1], [job4 = 3]
如果您想查看job3和job4分享的重叠次数,您可以
(overlaps.get("job3")).get("job4");
我还没有对此进行过测试,也没有对此进行优化(它不会避免多余的工作,例如,如果您已经找到了job1与job3的重叠,那么您就不应该&#39;为job3和job1做这件事。)看来一个有趣的问题,所以我想我试一试:)