如何替换树形图中的键映射?

时间:2015-11-22 18:09:07

标签: java dictionary hash tree key

好的,这是我为双键数据库编写的一个类。我已经完成了许多方法,但是我无法获得我想要的更改方法。问题是我成功创建了KeyTwo的新KeyOne,它映射到正确的数据,但之前的keyOne或KeyTwo也将映射到该值。例如,

db.add(68, 15, "Christian"); // Firsy key, second key, data
db.add(66, 100, "Mario");
db.add(14, 31, "Ernest");
db.add(20, 10, "Paul");

//Testing of the change methods.
db.change1(20, 11);
System.out.println(db.key2Find(11));
System.out.println(db.key2Find(10)); //error 10 should not map anymore
OUTPUT:
Paul
Paul

每个数据只能有一个keyOne和一个KeyTwo。

这是我代码的一部分。 change1和change2方法是我遇到麻烦的方法。我怎么能这样做呢?

import java.util.*;
public class TwoKey<k1 extends Comparable, k2 extends Comparable, D>{

private TreeMap<k1,D> key1Map = new TreeMap<k1, D>();
private TreeMap<k2,D> key2Map = new TreeMap<k2, D>();
public D dataGarbage;

 public void add(k1 keyOne, k2 keyTwo, D data){
 //Add data to the database, accessible by two keys.
 //Each key is mapped to one data value, but key 1 and key 2 are in seperate mappings.
  key1Map.put(keyOne, data);
  key2Map.put(keyTwo, data);
}
public D key1Find(k1 keyOne){
//Search the database using key one and return the data associated with it.
  return key1Map.get(keyOne);
}
public D key2Find(k2 keyTwo){
//Search the database using key two and return the data associated with it.
  return key2Map.get(keyTwo);
}
/* Modifying works by storing current data in the dataGarbage instance
 * variable, then making the first data change, 
 * then modifying the other keylist by finding the other key associated with it, 
 * then applying the second data change */
public void modify1(k1 keyOne, D newData){
  if(keyOne == null){
    Set set = key1Map.entrySet();
    Iterator i = set.iterator();
    while(i.hasNext()){
      Map.Entry me = (Map.Entry)i.next();
      if(me.getValue() == dataGarbage){
        key1Map.put((k1)me.getKey(), newData);
      }
    }
    return;
  }
  dataGarbage = key1Map.get(keyOne);
  key1Map.put(keyOne, newData);
  modify2(null, newData);
}
public void modify2(k2 keyTwo, D newData){
  if(keyTwo == null){
    Set set = key2Map.entrySet();
    Iterator i = set.iterator();
    while(i.hasNext()){
      Map.Entry me = (Map.Entry)i.next();
      if(me.getValue() == dataGarbage){
        key2Map.put((k2)me.getKey(), newData);
    }
  }
  return;
} 
dataGarbage = key2Map.get(keyTwo);
key2Map.put(keyTwo, newData);
modify1(null, newData);

}

 public void change1(k1 keyOne, k2 keyTwo){
  //Change the second key associated with the data corresponding to keyOne,to keyTwo
  D k1Data = key1Map.get(keyOne);

D k1Data = key1Map.get(keyOne);

Set set = key1Map.entrySet();
Iterator i = set.iterator();
while(i.hasNext()){
  Map.Entry me = (Map.Entry)i.next();
  if(me.getValue() == k1Data){
    key2Map.remove(me.getKey());
    key2Map.put(keyTwo, k1Data);
  }
}   

 }
   public void change2(k2 keyTwo, k1 keyOne){
    //Change the first key associated with the data corresponding to keyTwo, to KeyOne

  D k2Data = key2Map.get(keyTwo);

   Set set = key2Map.entrySet();
   Iterator i = set.iterator();
   while(i.hasNext()){
    Map.Entry me = (Map.Entry)i.next();
    if(me.getValue() == k2Data){

      key1Map.put(keyOne, k2Data);
  }
}

2 个答案:

答案 0 :(得分:0)

更正您的更改1,如下所示:

     public void change1(k1 keyOne, k2 keyTwo){
       //Change the second key associated with the data corresponding to
       //keyOne,to  keyTwo
       D k1Data = key1Map.get(keyOne);
       Iterator<Map.Entry<k1,D>> i = key1Map.entrySet().iterator();
       while(i.hasNext()){
         Map.Entry me = (Map.Entry)i.next();
         if(me.getKey().equals(keyone){
            i.remove();
         }     
       }
       key2Map.put(keyTwo, k1Data);
   }

答案 1 :(得分:0)

最后为那些感兴趣的人找到了它。

对@Steephen给出的解决方案略有改变。

   public void change1(k1 keyOne, k2 keyTwo){      
    D k1Data =  key1Map.get(keyOne);
     Iterator<Map.Entry<k2,D>> i = key2Map.entrySet().iterator();
      while(i.hasNext()){
       Map.Entry me = (Map.Entry)i.next();
        if(me.getValue().equals(k1Data)){
        i.remove();
        }     
      }
   key2Map.put(keyTwo, k1Data);
   }

必须迭代第二个键映射(key2Map),找到相当于k1Data的相同数据,然后删除迭代索引键处的映射。