简体   繁体   中英

convert hashmap items into another object and return as a list in java

I have this following code:

private List<Header> getHeadersList(HashMap<String, String> headersMap){
        List<Header> headers = new ArrayList<>();
        for(Map.Entry<String, String> headerItem : headersMap.entrySet()) {
            Header header = new BasicHeader(headerItem.getKey(), headerItem.getValue());
            headers.add(header);
        }

        return headers;
    }

I wonder if there is a way of doing it using Java 8 stream library?

headersMap.entrySet().stream()
   .map(e->new BasicHeader(e.getKey(), e.getValue()))
   .collect(Collectors.toList());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM