如何将哈希映射内容添加到文本文件

时间:2016-09-12 09:49:08

标签: java collections

您好Hashmap内容为

mapping :{[unknown, unknown, nicholas@123.com, nicholas@123.com, nicholas@123.com, WHEEL@123.com, WHEEL@123.com]=[STANDARD CHARTERED B, STANDARD CHARTERED B, DBS BANK LIMITED HON, DBS BANK LIMITED HON, DBS BANK LIMITED HON, DBS BANK (HONG KONG), DBS BANK (HONG KONG)], [unknown, unknown, nicholas@123.com, nicholas@123.com, nicholas@123.com, WHEEL@123.com, WHEEL@123.com]=[STANDARD CHARTERED B, STANDARD CHARTERED B, DBS BANK LIMITED HON, DBS BANK LIMITED HON, DBS BANK LIMITED HON, DBS BANK (HONG KONG), DBS BANK (HONG KONG)], [unknown, unknown, nicholas@123.com, nicholas@123.com, nicholas@123.com, WHEEL@123.com, WHEEL@123.com]=[STANDARD CHARTERED B, STANDARD CHARTERED B, DBS BANK LIMITED HON, DBS BANK LIMITED HON, DBS BANK LIMITED HON, DBS BANK (HONG KONG), DBS BANK (HONG KONG)], [unknown, unknown, nicholas@123.com, nicholas@123.com, nicholas@123.com, WHEEL@123.com, WHEEL@123.com]=[STANDARD CHARTERED B, STANDARD CHARTERED B, DBS BANK LIMITED HON, DBS BANK LIMITED HON, DBS BANK LIMITED HON, DBS BANK (HONG KONG), DBS BANK (HONG KONG)], [unknown, unknown, nicholas@123.com, nicholas@123.com, nicholas@123.com, WHEEL@123.com, WHEEL@123.com]=[STANDARD CHARTERED B, STANDARD CHARTERED B, DBS BANK LIMITED HON, DBS BANK LIMITED HON, DBS BANK LIMITED HON, DBS BANK (HONG KONG), DBS BANK (HONG KONG)], [unknown, unknown, nicholas@123.com, nicholas@123.com, nicholas@123.com, WHEEL@123.com, WHEEL@123.com]=[STANDARD CHARTERED B, STANDARD CHARTERED B, DBS BANK LIMITED HON, DBS BANK LIMITED HON, DBS BANK LIMITED HON, DBS BANK (HONG KONG), DBS BANK (HONG KONG)], [unknown, unknown, nicholas@123.com, nicholas@123.com, nicholas@123.com, WHEEL@123.com, WHEEL@123.com]=[STANDARD CHARTERED B, STANDARD CHARTERED B, DBS BANK LIMITED HON, DBS BANK LIMITED HON, DBS BANK LIMITED HON, DBS BANK (HONG KONG), DBS BANK (HONG KONG)]}

我想将其作为唯一值添加到文件中,并使用其映射

unknown STANDARD CHARTERED B
nicholas@123.com DBS BANK LIMITED HON
WHEEL@123.com DBS BANK (HONG KONG)

我的java代码如下....

System.out.println("mapping :"+compIdmap);


            Iterator<Entry<ArrayList<String>, ArrayList<String>>> it = compIdmap.entrySet().iterator();

            while (it.hasNext()){
                Entry<ArrayList<String>, ArrayList<String>> line = it.next();
                String entryLine = line.getKey() + " -> " + line.getValue();
                br1.write(entryLine);
                }

它不是以映射格式写的... 1行的hashmap正在添加..我只希望将hashmap的唯一元素添加到文本文件中。

1 个答案:

答案 0 :(得分:0)

试试这个:

创建另一个HashMap以添加唯一值

  HashMap<String, String> hashmap = new HashMap<String, String>();

在新的hashmap中添加项目

hashmap.put(keys[i], values[i]);

``

   Iterator<Entry<ArrayList<String>, ArrayList<String>>> it =  compIdmap.entrySet().iterator();
   HashMap<String, String> hashmap = new HashMap<String, String>();
   while (it.hasNext()) {
        Entry<ArrayList<String>, ArrayList<String>> line = it.next();
        String keys[] = line.getKey().split(",");
        String values[] = line.getValue().split(",");
        for (int i = 0; i < keys.length; i++) {
            hashmap.put(keys[i], values[i]);
        }
    }

迭代地图和写入文件

   Iterator iterator = mp.entrySet().iterator();
   while (it.hasNext()) {
        Map.Entry pair = (Map.Entry)iterator.next();
        String entryLine=pair.getKey() + " = " + pair.getValue();
        br1.write(entryLine);
        //write  here in file ...........
    }
}