我有两个键和值都是字符串的集合。我需要对集合进行排序,因此我决定使用“ TreeMap”来保持排序。
我想要:
1)分别打印出两个集合“ garList”和“ noGarList”;
2)将第一个集合的每个元素与第二个集合的每个元素进行比较;
3)从第一个集合('garList')中删除出现在第二个集合('noGarList')中的那些元素。
我编写了以下代码来完成这3个任务:
public class TryTask_A_copy {
public static void main(String[] args) {
// First collection:
TreeMap<String, String> garList = new TreeMap<>();
// Second collection:
TreeMap<String, String> noGarList = new TreeMap<>();
// Fill the "Properties" obj for 'Gar':
garList.put("Gar_1", "rotura de lunas");
garList.put("Gar_2", "arbitraje de ley");
garList.put("Gar_3", "Adaptación del hogar");
// Fill the "Properties" obj for 'noGar':
noGarList.put("noGar_1", "rotura de lunas");
noGarList.put("noGar_2", "reembolso total");
noGarList.put("noGar_3", "Adaptación del coche");
// Get a set of the entries:
Set garSet = garList.entrySet();
Set noGarSet = noGarList.entrySet();
// Def strings needed for the comparison:
String strGar;
String strNoGar;
// Get an iterator:
Iterator i_gar = garSet.iterator();
Iterator i_noGar = noGarSet.iterator();
// Display 'Gar' elements:
while(i_gar.hasNext()){
String me_Gar = (String)i_gar.next(); // Exception in thread "main" java.lang.ClassCastException: java.util.TreeMap$Entry cannot be cast to java.lang.String
strGar = (String) i_gar.next();
System.out.println(strGar + " : " + garList.get(strGar) + ".");
}
System.out.println();
// Display 'noGar' elements:
while(i_noGar.hasNext()){
String me_noGar = (String)i_noGar.next();
strNoGar = (String) i_noGar.next();
System.out.println(strNoGar + " : " + garList.get(strNoGar) + ".");
}
// Get new iterators:
Iterator itr_gar = garSet.iterator();
Iterator itr_noGar = noGarSet.iterator();
// Compare elements from 'Gar' list with elements from 'noGar' list
// and remove those elements from 'Gar' list that appear in 'noGar' list as well:
while(itr_gar.hasNext()){
strGar = (String) itr_gar.next();
while(itr_noGar.hasNext()){
String str1 = garList.get(strGar);
strNoGar = (String) itr_noGar.next();
String str2 = noGarList.get(strNoGar);
boolean result = str1.equalsIgnoreCase(str2);
System.out.println(strGar + " : " + str1 + ".");
System.out.println(strNoGar + " : " + str2 + ".");
System.out.println(result);
System.out.println();
// if an element appears in both lists, then remove this element from "Gar" list:
if(result != true){
} else {
Object garList_new = garList.remove(strGar);
System.out.println("Removed element: " + garList_new);
System.out.println();
}
}
itr_noGar = noGarSet.iterator();
}
}
}
但我得到:
第51行的线程“主”中的异常java.lang.ClassCastException:java.util.TreeMap $ Entry无法转换为java.lang.String”。
我知道我的两个TreeMap对象的元素是“地图项”类型,不能转换为“字符串”类型,对吗?
但是,如何获得键和值均为字符串的有序集合呢?
写类似的东西是garSet.removeAll(noGarSet)
行不通的,因为这意味着键和值都重合。但就我而言,在两个键具有不同键的同时,我有一些值是一致的。
因此,我需要以下解决方案:有一个有序集合,其中键和值均为String,并且可以比较和删除值。
你能帮我这个忙吗?
对于您要实现的目标,我还不太清楚,但是如果您要基于键删除,则可以执行以下操作:
garSet.removeAll(noGarSet);
通常,您的代码似乎要比您要实现的目标复杂得多。例如,要打印Map中的所有字符串,只需执行以下操作:
for (Entry<String, String> entry: garMap.entrySet()) {
System.out.println(entry.key() + " : " + entry.value() + ".");
}
如果您这样做:
map1.keySet().removeAll(map2.keySet())
然后将根据密钥删除所有重复项。
map1.values().removeAll(map2.values())
将根据价值删除所有重复项。
map1.entryset().removeAll(map2.entrySet())
将根据键/值对删除所有重复项。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句