英文:
hashtable key-value pair comparison to find out unique pair
问题
public static void main(String[] args) {
Hashtable<String, String> ht1 = new Hashtable<>();
ht1.put("10", "ghu");
ht1.put("20", "lo");
Hashtable<String, String> ht2 = new Hashtable<>();
ht2.put("10", "ko");
ht2.put("20", "lo");
// Compare and find unique entries
for (String key : ht1.keySet()) {
String value1 = ht1.get(key);
String value2 = ht2.get(key);
if (!value1.equals(value2)) {
System.out.println("(" + key + ", " + value1 + "); this is from first hashtable");
System.out.println("(" + key + ", " + value2 + "); this is from second hashtable");
}
}
}
英文:
public static void main(String[] args) {
Hashtable<String, String> ht1= new Hashtable<>();
ht1.put("10", ghu");
ht1.put("20", "lo");
Hashtable<String, String> ht2= new Hashtable<>();
ht2.put("10", "ko");
ht2.put("20", "lo");
how can i compare both hash table by using key-value pair to each other find unique entry of key-value pair from both hash table..
expected output...
("10", "ghu"); this is from first hashtable
("10", "ko"); this is from second hashtable
答案1
得分: 0
HashTable保存一个Entry,可以通过使用getEntrySet()方法来提取。因此,您应该编写类似下面的伪代码。您可能需要对下面的代码片段进行一些更改,以使其能够编译。
Set<Entry> entrySet = ht1.getEntrySet(); // 获取所有值
// 遍历这些值
for (Entry entry : entrySet) {
String htVal = ht2.get(entry.getKey());
if (htVal != null && htVal.equals(entry.getValue())) {
// 非唯一 - 忽略
} else {
// 存储ht1和ht2中唯一的条目
}
}
英文:
HashTable keeps an Entry which can get extracted by using getEntrySet() method.
Thus you should something like pseudoCode below. You might need to make some change to below snippet to make it compilable.
Set<Entry> entrySet= ht1.getEntrySet();//get all values
//iterate over those
for(Entry entry :entrySet ){
String htVal = ht2.get(entry.getKey());
if (htval!=null && htVal.equals(entry.getValue())){
//non unique -ignore
}else{
//store ht1 and ht2 unique entries
}
}
专注分享java语言的经验与见解,让所有开发者获益!
评论