Java 如何用地图的值键替换键值
我想写一个方法,它将一个映射作为参数,并用值和键替换该映射的键和值。我试着这样做:Java 如何用地图的值键替换键值,java,Java,我想写一个方法,它将一个映射作为参数,并用值和键替换该映射的键和值。我试着这样做: public class HashMapKeyValueInterchange{ public static Map<String, String> getMyMap(ConcurrentHashMap<String, String> m){ Map<String, String> map2 = new HashMap<String,
public class HashMapKeyValueInterchange{
public static Map<String, String> getMyMap(ConcurrentHashMap<String, String> m){
Map<String, String> map2 = new HashMap<String, String>();
for(Entry<String, String> e:m.entrySet()){
map2.put(e.getValue(), e.getKey());
}
return map2;
}
public static void main(String[] args) {
ConcurrentHashMap<String, String> map1 = new ConcurrentHashMap<String, String>();
map1.put("ajay", "btech");
map1.put("manas", "mca");
map1.put("ashu", "mba");
}
}
公共类HashMapKeyValueInterchange{
公共静态映射getMyMap(ConcurrentHashMap m){
Map map2=新的HashMap();
对于(条目e:m.entrySet()){
map2.put(e.getValue(),e.getKey());
}
返回map2;
}
公共静态void main(字符串[]args){
ConcurrentHashMap map1=新的ConcurrentHashMap();
地图1.put(“ajay”、“btech”);
map1.put(“玛纳斯”、“mca”);
map1.put(“ashu”、“mba”);
}
}
使用此方法,我可以获得一个具有交换键和值的新地图(map2),但我希望交换map1。它是可用的,无需重新发明轮子,如果您使用google collection library Guava,则可以使用 它是一个保持其值的唯一性以及 那是它的钥匙。此约束使BIMAP能够支持 “反向视图”,这是另一个bimap,包含与 此bimap具有相反的键和值
BiMap
的实现是EnumBiMap、EnumHashBiMap、[HashBiMap][2],ImmutableBiMap
如果问题是代码是否有效,答案是肯定的。一些评论:
getMyMap
System.out.println(getMyMap(map1))代码>
Map
而不是ConcurrentHashMap
作为getMyMap
的第一个参数类型,因此该函数更通用,可用于其他映射public static <K,V> void getMyMap(Map<V,K> output,Map<K,V> input) {
for(Entry<K,V> e: input.entrySet() ) {
output.put(e.getValue(), e.getKey());
}
}
public static ConcurrentHashMap<String, String> getMyMap(ConcurrentHashMap<String, String> m){
ConcurrentHashMap<String, String> map2 = new ConcurrentHashMap<String, String>();
for(Entry<String, String> e:m.entrySet()){
map2.put(e.getValue(), e.getKey());
}
return map2;
}
public static void main(String[] args) {
ConcurrentHashMap<String, String> map1 = new ConcurrentHashMap<String, String>();
map1.put("ajay", "btech");
map1.put("manas", "mca");
map1.put("ashu", "mba");
map1 = getMyMap(map1);
}
公共静态ConcurrentHashMap getMyMap(ConcurrentHashMap m){
ConcurrentHashMap map2=新的ConcurrentHashMap();
对于(条目e:m.entrySet()){
map2.put(e.getValue(),e.getKey());
}
返回map2;
}
公共静态void main(字符串[]args){
ConcurrentHashMap map1=新的ConcurrentHashMap();
地图1.put(“ajay”、“btech”);
map1.put(“玛纳斯”、“mca”);
map1.put(“ashu”、“mba”);
map1=getMyMap(map1);
}
BiMap是此类操作的良好选择
BiMap是指
允许您使用Reverse()查看“Reverse”BiMap
确保值是唯一的,使values()成为一个集合
BiMap.put(key,value)
如果试图将一个键映射到一个已经存在的值,将抛出一个IllegalArgumentException
。如果要删除任何具有指定值的预先存在的条目,请改用BiMap.forcePut(键,值)
BiMap<String, Integer> userId = HashBiMap.create();
String userForId = userId.inverse().get(id);
BiMap userId=HashBiMap.create();
字符串userForId=userId.inverse().get(id);
查看更多信息问题是什么?使用HashMapKeyValueInterchange类的代码在哪里?我认为您所做的看起来不错。。我认为没有更好的办法了!你没有调用
getMyMap
方法。在我看来,就像番石榴重新发明了轮子一样,看到了吗
BiMap<String, Integer> userId = HashBiMap.create();
String userForId = userId.inverse().get(id);