zoukankan      html  css  js  c++  java
  • 把两个map中的key-value进行替换

    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 + " targetKey=" + 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;
    }
    }
  • 相关阅读:
    IDEA 现有项目连接SVN
    图片预加载,懒加载
    element-ui table中排序 取消表格默认排序问题
    Linux vim编辑命令
    linux mount 硬盘挂载和卸载
    Linux mke2fs 硬盘格式化
    Linux rm 删除文件
    linux 创建和删除目录
    linux 配置IP
    kickstart自动化安装--tftp+nfs+dhcp
  • 原文地址:https://www.cnblogs.com/love-you-girl/p/4415316.html
Copyright © 2011-2022 走看看