Java 如何删除映射中重复的键值对
我一直在研究如何将键值对从map1传输到map2,前提是每个键在map1中都有唯一的值 假设我有以下地图:Java 如何删除映射中重复的键值对,java,map,key-value,Java,Map,Key Value,我一直在研究如何将键值对从map1传输到map2,前提是每个键在map1中都有唯一的值 假设我有以下地图: 地图1:[1,2][2,4][4,4] map2:[1,2][2,4] 我想算法应该是: 循环浏览第一个映射中的条目 向map2添加一个键 向集合中添加一个值,该集合将检查map2的值 如果值重复,则不会将该值添加到集合中,而忽略将其对应的键添加到map2 代码段: public static <K,V> Map<K,V> unique (Map<K,V&
- 地图1:[1,2][2,4][4,4]
- map2:[1,2][2,4]
public static <K,V> Map<K,V> unique (Map<K,V> m) {
Map<K,V> newMap = new ArrayMap<K,V>();
//Remember all values in the newMap.
Set<V> holding = new ArraySet<V>(newMap.values());
for (Map.Entry<K, V> graphEntry : m.entries()) {
//not sure.
}
return newMap;
}
公共静态映射唯一(映射m){
Map newMap=newarraymap();
//记住newMap中的所有值。
Set holding=newarrayset(newMap.values());
对于(Map.Entry graphEntry:m.entries()){
//不确定。
}
返回newMap;
}
我的想法是否正确?此处完全丢失。从地图创建地图
,当且仅当钥匙不在地图中时,才会添加项目。使用此地图
,重新创建您的地图
publicstaticmap-createMap(Map m){
Map Map=newhashmap();
Map tmpMap=newhashmap();
对于(Map.Entry:m.entrySet()){
如果(!tmpMap.containsKey(entry.getValue())){
tmpMap.put(entry.getValue(),entry.getKey());
}
}
对于(Map.Entry:tmpMap.entrySet()){
map.put(entry.getValue(),entry.getKey());
}
返回图;
}
如果需要保留数据的保留顺序,请使用LinkedHashMap
而不是HashMap
从Map
创建一个Map
,当且仅当键不在映射中时才添加项。使用此地图
,重新创建您的地图
publicstaticmap-createMap(Map m){
Map Map=newhashmap();
Map tmpMap=newhashmap();
对于(Map.Entry:m.entrySet()){
如果(!tmpMap.containsKey(entry.getValue())){
tmpMap.put(entry.getValue(),entry.getKey());
}
}
对于(Map.Entry:tmpMap.entrySet()){
map.put(entry.getValue(),entry.getKey());
}
返回图;
}
如果需要保留数据的保留顺序,请使用LinkedHashMap
而不是HashMap
签出。。这就是你需要的
虽然您的问题已经解决,但您可以查看以下代码,以使用Guava
API实现您的目标:-
public void removeDuplicateValue() {
Map<Integer, String> existingMap = new HashMap<Integer, String>();
existingMap.put(1, "a");
existingMap.put(2, "b");
// Create a new BiMap
BiMap<Integer, String> biMap = HashBiMap.create();
for (Integer val: existingMap.keySet()) {
// forcePut will add a key-value pair, and overwrite the duplicate value.
biMap.forcePut(val, existingMap.get(val));
}
// Create Inverse Map for newly created BiMap.
BiMap<String, Integer> inverseBiMap = biMap.inverse();
for(String val: inverseBiMap.keySet()) {
System.out.println(val + ":" + biMap.get(val));
}
}
public void removedUpplicateValue(){
Map existingMap=new HashMap();
现有地图。放置(1,“a”);
现有地图。放置(2,“b”);
//创建新的BiMap
BiMap BiMap=HashBiMap.create();
for(整数val:existingMap.keySet()){
//forcePut将添加一个键值对,并覆盖重复的值。
biMap.forcePut(val,existingMap.get(val));
}
//为新创建的BiMap创建反向贴图。
BiMap inverseBiMap=BiMap.inverse();
for(字符串val:inverseBiMap.keySet()){
System.out.println(val+“:“+biMap.get(val));
}
}
签出。。这就是你需要的
虽然您的问题已经解决,但您可以查看以下代码,以使用Guava
API实现您的目标:-
public void removeDuplicateValue() {
Map<Integer, String> existingMap = new HashMap<Integer, String>();
existingMap.put(1, "a");
existingMap.put(2, "b");
// Create a new BiMap
BiMap<Integer, String> biMap = HashBiMap.create();
for (Integer val: existingMap.keySet()) {
// forcePut will add a key-value pair, and overwrite the duplicate value.
biMap.forcePut(val, existingMap.get(val));
}
// Create Inverse Map for newly created BiMap.
BiMap<String, Integer> inverseBiMap = biMap.inverse();
for(String val: inverseBiMap.keySet()) {
System.out.println(val + ":" + biMap.get(val));
}
}
public void removedUpplicateValue(){
Map existingMap=new HashMap();
现有地图。放置(1,“a”);
现有地图。放置(2,“b”);
//创建新的BiMap
BiMap BiMap=HashBiMap.create();
for(整数val:existingMap.keySet()){
//forcePut将添加一个键值对,并覆盖重复的值。
biMap.forcePut(val,existingMap.get(val));
}
//为新创建的BiMap创建反向贴图。
BiMap inverseBiMap=BiMap.inverse();
for(字符串val:inverseBiMap.keySet()){
System.out.println(val+“:“+biMap.get(val));
}
}
试试这个
Map<String, String> myMap1 = new TreeMap<String, String>();
myMap1.put("1", "One");
myMap1.put("2", "Two");
myMap1.put("3", "One");
myMap1.put("4", "Three");
myMap1.put("5", "Two");
myMap1.put("6", "Three");
Set<String> mySet = new HashSet<String>();
for (Iterator itr = myMap1.entrySet().iterator(); itr.hasNext();)
{
Map.Entry<String, String> entrySet = (Map.Entry) itr.next();
String value = entrySet.getValue();
if (!mySet.add(value))
{
itr.remove();
}
}
Map<String, String> myMap2 = new TreeMap<String, String>(myMap1);
System.out.println("Result :"+myMap2);
Map myMap1=newtreemap();
myMap1.put(“1”、“1”);
myMap1.put(“2”、“2”);
myMap1.put(“3”,“1”);
myMap1.put(“4”,“3”);
myMap1.put(“5”、“2”);
myMap1.put(“6”,“三”);
Set mySet=newhashset();
for(迭代器itr=myMap1.entrySet().Iterator();itr.hasNext();)
{
Map.Entry entrySet=(Map.Entry)itr.next();
String value=entrySet.getValue();
如果(!mySet.add(value))
{
itr.remove();
}
}
Map myMap2=新树映射(myMap1);
System.out.println(“结果:+myMap2”);
结果:{1=1,2=2,4=3}试试这个
Map<String, String> myMap1 = new TreeMap<String, String>();
myMap1.put("1", "One");
myMap1.put("2", "Two");
myMap1.put("3", "One");
myMap1.put("4", "Three");
myMap1.put("5", "Two");
myMap1.put("6", "Three");
Set<String> mySet = new HashSet<String>();
for (Iterator itr = myMap1.entrySet().iterator(); itr.hasNext();)
{
Map.Entry<String, String> entrySet = (Map.Entry) itr.next();
String value = entrySet.getValue();
if (!mySet.add(value))
{
itr.remove();
}
}
Map<String, String> myMap2 = new TreeMap<String, String>(myMap1);
System.out.println("Result :"+myMap2);
Map myMap1=newtreemap();
myMap1.put(“1”、“1”);
myMap1.put(“2”、“2”);
myMap1.put(“3”,“1”);
myMap1.put(“4”,“3”);
myMap1.put(“5”、“2”);
myMap1.put(“6”,“三”);
Set mySet=newhashset();
for(迭代器itr=myMap1.entrySet().Iterator();itr.hasNext();)
{
Map.Entry entrySet=(Map.Entry)itr.next();
String value=entrySet.getValue();
如果(!mySet.add(value))
{
itr.remove();
}
}
Map myMap2=新树映射(myMap1);
System.out.println(“结果:+myMap2”);
结果:{1=1,2=2,4=3}是否要将对从map1添加到map2,并避免在map2中过度加载值?是否可以发布请求的out pot?为什么您的算法不生成{1:2,4:4}
的map2?为什么选择2
?这无关紧要吗?map1中的两个键的值分别为4、[2,4]和[4,4]。另一个地图将只有来自map1的前两个键[1,2][2,4]。它必须是2:4,因为它按顺序通过每个键,因此当它达到4:4时,它将被视为重复。是否要将对从map1添加到map2,并避免在map2中过度加载值?您可以发布请求的输出池吗?为什么您的算法不生成{1:2,4:4}
的map2?为什么选择2
?这无关紧要吗?map1中的两个键的值分别为4、[2,4]和[4,4]。另一个贴图将只具有来自map1的前两个键[1,2]