Search code examples
javaurlhashmap

How to scroll the list of keys of a java map?


In my Spring Boot project I want to create a URL by concatenating several strings. The first strings I know how to get them, but to these I have to add some fields defined in a map.

How do I get from this map the list of its keys and for each key add the name of the key with its respective value?

This is the part of the code where I stopped:

@Value("${spring.route.source.protocol}")
private String protocol;

@Value("${spring.route.source.ip}")
private String ip;

@Value("${spring.route.source.root}")
private String root;

@Value("${spring.route.source.gets}")
private List<String> gets;

public void getWithRestTemplateGet1(Map<String, String> allGateAccessParams) {
        final String methodName = "getWithRestTemplateGet1()";
    try {
        startLog(methodName);

        List<String> keys = new ArrayList<String>();

        //HOW CAN I ADD KEY E VALUE FROM MAP TO URL?

        String url = protocol + ip + root + gets.get(0);
        HttpHeaders headers = new HttpHeaders();
        headers.setBasicAuth(username, password);
        HttpEntity request = new HttpEntity(headers);

        try {
            if (url.startsWith("https")) {
                restTemplate = getRestTemplateForSelfSsl();
            } else {
                restTemplate = new RestTemplate();
            }
            ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.GET, request, String.class);
            HttpStatus statusCode = response.getStatusCode();
            logger.info("STATUS GET1: " + statusCode);
            logger.info("URL GET1: " + url);
            logger.info("RESPONSE GET1: " + response);

            } catch (HttpStatusCodeException e) {
            logger.error(e.getMessage());
        }

        endLog(methodName);

    } catch (Exception e) {
        logger.error(e.getMessage());
    }
}

Can you help me?


Solution

  • You can get entrySet() from map and iterate entrySet to getKey using getKey() and value using getValue() as below

    for (Map.Entry<String, String> entry : allGateAccessParams.entrySet()) {
            System.out.println(entry.getKey() + ":" + entry.getValue());
        }