dEs12ZER
dEs12ZER

Reputation: 848

is it possible to print the keys of a hashmap with a specific form?

I'm about working with Junit test , hashmap in java . My code looks like :

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 ;

What i want to do is to return the itkeys as this forms :

key1, key2, key3, key4

we start with the first key + comma etc .

so i need to know what's the first key and the last one .

Any idea on how we can do that ?

Upvotes: 0

Views: 84

Answers (3)

Ray Toal
Ray Toal

Reputation: 88468

Join the keys with a comma and a space:

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()));
    }
}

Output:

key1, key2, key3, key4

Upvotes: 3

jhenrique
jhenrique

Reputation: 868

If you are not familiar with String.join, use the first one, or use the second if you like it. Both ways work:

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());
    }
}

Output is:

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

Upvotes: 0

grindlewald
grindlewald

Reputation: 338

       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()));

Upvotes: 0

Related Questions