码迷,mamicode.com
首页 > 其他好文 > 详细

把两个map中的key-value进行替换

时间:2015-04-10 19:25:47      阅读:185      评论:0      收藏:0      [点我收藏+]

标签:

public class Test {
private static Map<Object, Object> sourceMap = new HashMap<Object, Object>();
private static Map<Object, Object> targetmap = new HashMap<Object, Object>();
static {
sourceMap.put("source", "s");
targetmap.put("target", "t");
}
public static void main(String[] args) {
for (Entry<Object, Object> map : sourceMap.entrySet()) {
Object sourceKey = map.getKey();
Object sourceValue = map.getValue();
Object targetKey = Test.getconventKey(targetmap);
Object targetValue = Test.getconventValue(targetmap);
sourceMap.remove(sourceKey);
targetmap.remove(targetKey);
sourceMap.put(targetKey, targetValue);
targetmap.put(sourceKey, sourceValue);
for (Entry<Object, Object> map1 : sourceMap.entrySet()) {
Object sourceKey1 = map1.getKey();
Object sourceValue1 = map1.getValue();
System.out.println("sourceKey1=" + sourceKey1
+ ",sourceValue1=" + sourceValue1);
}
// 这个打印错误,正在分析中
System.out.println("sourceKey=" + sourceKey + ",sourceValue="
+ sourceValue + "\ntargetKey=" + targetKey
+ ",targatValue=" + targetValue);
}
}
private static Object getconventKey(Map<Object, Object> targetmap) {
Object key = null;
for (Entry<Object, Object> map : targetmap.entrySet()) {
key = map.getKey();
}
return key;
}
private static Object getconventValue(Map<Object, Object> targetmap) {
Object value = null;
for (Entry<Object, Object> map : targetmap.entrySet()) {
value = map.getValue();
}
return value;
}
}

把两个map中的key-value进行替换

标签:

原文地址:http://www.cnblogs.com/love-you-girl/p/4415316.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!