是否可以用特定形式打印哈希图的键?

时间:2018-09-28 17:22:18

标签: java junit hashmap

我要使用Java中的hashmap Junit测试。 我的代码如下:

HashMap <String,String> var = new HashMap <String,String>(); 
var.put("key1","value1");
var.put("key2","value2");
var.put("key3","value3");
var.put("key4","value4");

Iterator<String> itKey = var.keySet().iterator();

String Existing_coaches = "" ;

 while(itKey.hasNext()){

 String key = (String)itKey.next();

// Existing_coaches = i use concat function 

    } 
 return  Existing_coaches ;

我想做的就是以这种形式返回itkeys:

key1,key2,key3,key4

我们从第一个键+逗号等开始。

所以我需要知道第一个键和最后一个键是什么。

关于我们如何做到这一点的任何想法?

3 个答案:

答案 0 :(得分:3)

用逗号和空格连接键:

model.predict_classes

输出:

import java.util.*;

public class MyClass {
    public static void main(String args[]) {
        Map<String, String> m = new LinkedHashMap<>();
        m.put("key1", "val1");
        m.put("key2", "val2");
        m.put("key3", "val3");
        m.put("key4", "val4");
        System.out.println(String.join(", ", m.keySet()));
    }
}

答案 1 :(得分:0)

如果您不熟悉String.join,请使用第一个,如果喜欢,请使用第二个。两种方法都可以:

import java.util.HashMap;
import java.util.Iterator;

public class Hash {
    public static void main(String args[]) {
        HashMap <String, String> var = new HashMap <String, String>(); 

        var.put("A", "1");
        var.put("B", "2");
        var.put("C", "3");
        var.put("D", "4");

        System.out.println("method 1: " + myHash(var));
        System.out.println("method 2: " + myHash2(var));
    }

    public static String myHash(HashMap var) {
        Iterator<String> itKey = var.keySet().iterator();
        String Existing_coaches = "" ;

        while(itKey.hasNext()){
            String key = (String)itKey.next();
            Existing_coaches = Existing_coaches + key + ", ";
        }

        return  Existing_coaches.substring(0, Existing_coaches.length() -2) ;
    }

    public static String myHash2(HashMap var) {
        return String.join(", ", var.keySet());
    }
}

输出为:

method 1: A, B, C, D
method 2: A, B, C, D

答案 2 :(得分:0)

       Map<String, String> map = new LinkedHashMap<>();

       // Approach 1 : use advanced for (foreach) loop
        StringBuilder keyString = new StringBuilder();
        for(String key: map.keySet()){
            keyString = keyString.append(key + ", ");
        }
        String resultStr = keyString.toString().substring(0, keyString.toString().length()-2); // deducting 2 for comma and space
        System.out.println(resultStr);

        // Approach 2: use for loop
        StringBuilder keyString2 = new StringBuilder();
        Object[] keySet =  (Object[]) map.keySet().toArray();
        for(int i=0; i < keySet.length; i++) {
            if(i == keySet.length-1) {
                keyString2 = keyString2.append(keySet[i].toString());
            } else {
                keyString2 = keyString2.append(keySet[i].toString() + ", ");
            }

        }
        System.out.println(keyString2.toString());

        // Approach 3: Use String join
        System.out.println(String.join(", ", map.keySet()));