How do I build a Java `Map` out of `List<Map.Entry>`? [duplicate]










-1















This question already has an answer here:



  • Convert Set<Map.Entry<K, V>> to HashMap<K, V>

    6 answers



I have a List<Map.Entry<Long, String>>.



How do I convert this into a Map?



Currently I am doing the following, but it seems a bit verbose (and most importantly, it is not "fluent", i.e. a single expression, but requires a code block).



Map<Long, String> result = new HashMap<>();
entries.forEach(e -> result.put(e.getKey(), e.getValue()));
return result;


Java 10 is fine.










share|improve this question













marked as duplicate by buræquete, Ferrybig, nullpointer java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Nov 11 at 11:29


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.










  • 1




    I would go with Java 11 as 10 is eol.
    – Peter Lawrey
    Nov 11 at 11:42






  • 1




    @PeterLawrey already? Oh my, they are moving fast...
    – Thilo
    Nov 11 at 11:44










  • Java 11 is EOL in 4 months :P At that time many might shift to OpenJDK 11, instead of skipping to 12. e.g. adoptopenjdk.net
    – Peter Lawrey
    Nov 11 at 12:20











  • Anyway, I meant this as in "use of Java 10 API are permissible" (in the hopes that we have something less boilerplatey than the Java 8 streams collectors now). Does Java 11 bring something new to the table here (such as Collection literals)?
    – Thilo
    Nov 11 at 12:24







  • 1




    Not really imho, You can write entries.forEach((var e) -> result.put(e.getKey(), e.getValue())); note the var e, the real advantage is Long Term Support-ability.
    – Peter Lawrey
    Nov 11 at 12:30















-1















This question already has an answer here:



  • Convert Set<Map.Entry<K, V>> to HashMap<K, V>

    6 answers



I have a List<Map.Entry<Long, String>>.



How do I convert this into a Map?



Currently I am doing the following, but it seems a bit verbose (and most importantly, it is not "fluent", i.e. a single expression, but requires a code block).



Map<Long, String> result = new HashMap<>();
entries.forEach(e -> result.put(e.getKey(), e.getValue()));
return result;


Java 10 is fine.










share|improve this question













marked as duplicate by buræquete, Ferrybig, nullpointer java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Nov 11 at 11:29


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.










  • 1




    I would go with Java 11 as 10 is eol.
    – Peter Lawrey
    Nov 11 at 11:42






  • 1




    @PeterLawrey already? Oh my, they are moving fast...
    – Thilo
    Nov 11 at 11:44










  • Java 11 is EOL in 4 months :P At that time many might shift to OpenJDK 11, instead of skipping to 12. e.g. adoptopenjdk.net
    – Peter Lawrey
    Nov 11 at 12:20











  • Anyway, I meant this as in "use of Java 10 API are permissible" (in the hopes that we have something less boilerplatey than the Java 8 streams collectors now). Does Java 11 bring something new to the table here (such as Collection literals)?
    – Thilo
    Nov 11 at 12:24







  • 1




    Not really imho, You can write entries.forEach((var e) -> result.put(e.getKey(), e.getValue())); note the var e, the real advantage is Long Term Support-ability.
    – Peter Lawrey
    Nov 11 at 12:30













-1












-1








-1








This question already has an answer here:



  • Convert Set<Map.Entry<K, V>> to HashMap<K, V>

    6 answers



I have a List<Map.Entry<Long, String>>.



How do I convert this into a Map?



Currently I am doing the following, but it seems a bit verbose (and most importantly, it is not "fluent", i.e. a single expression, but requires a code block).



Map<Long, String> result = new HashMap<>();
entries.forEach(e -> result.put(e.getKey(), e.getValue()));
return result;


Java 10 is fine.










share|improve this question














This question already has an answer here:



  • Convert Set<Map.Entry<K, V>> to HashMap<K, V>

    6 answers



I have a List<Map.Entry<Long, String>>.



How do I convert this into a Map?



Currently I am doing the following, but it seems a bit verbose (and most importantly, it is not "fluent", i.e. a single expression, but requires a code block).



Map<Long, String> result = new HashMap<>();
entries.forEach(e -> result.put(e.getKey(), e.getValue()));
return result;


Java 10 is fine.





This question already has an answer here:



  • Convert Set<Map.Entry<K, V>> to HashMap<K, V>

    6 answers







java






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 11 at 11:25









Thilo

192k77413572




192k77413572




marked as duplicate by buræquete, Ferrybig, nullpointer java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Nov 11 at 11:29


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






marked as duplicate by buræquete, Ferrybig, nullpointer java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Nov 11 at 11:29


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









  • 1




    I would go with Java 11 as 10 is eol.
    – Peter Lawrey
    Nov 11 at 11:42






  • 1




    @PeterLawrey already? Oh my, they are moving fast...
    – Thilo
    Nov 11 at 11:44










  • Java 11 is EOL in 4 months :P At that time many might shift to OpenJDK 11, instead of skipping to 12. e.g. adoptopenjdk.net
    – Peter Lawrey
    Nov 11 at 12:20











  • Anyway, I meant this as in "use of Java 10 API are permissible" (in the hopes that we have something less boilerplatey than the Java 8 streams collectors now). Does Java 11 bring something new to the table here (such as Collection literals)?
    – Thilo
    Nov 11 at 12:24







  • 1




    Not really imho, You can write entries.forEach((var e) -> result.put(e.getKey(), e.getValue())); note the var e, the real advantage is Long Term Support-ability.
    – Peter Lawrey
    Nov 11 at 12:30












  • 1




    I would go with Java 11 as 10 is eol.
    – Peter Lawrey
    Nov 11 at 11:42






  • 1




    @PeterLawrey already? Oh my, they are moving fast...
    – Thilo
    Nov 11 at 11:44










  • Java 11 is EOL in 4 months :P At that time many might shift to OpenJDK 11, instead of skipping to 12. e.g. adoptopenjdk.net
    – Peter Lawrey
    Nov 11 at 12:20











  • Anyway, I meant this as in "use of Java 10 API are permissible" (in the hopes that we have something less boilerplatey than the Java 8 streams collectors now). Does Java 11 bring something new to the table here (such as Collection literals)?
    – Thilo
    Nov 11 at 12:24







  • 1




    Not really imho, You can write entries.forEach((var e) -> result.put(e.getKey(), e.getValue())); note the var e, the real advantage is Long Term Support-ability.
    – Peter Lawrey
    Nov 11 at 12:30







1




1




I would go with Java 11 as 10 is eol.
– Peter Lawrey
Nov 11 at 11:42




I would go with Java 11 as 10 is eol.
– Peter Lawrey
Nov 11 at 11:42




1




1




@PeterLawrey already? Oh my, they are moving fast...
– Thilo
Nov 11 at 11:44




@PeterLawrey already? Oh my, they are moving fast...
– Thilo
Nov 11 at 11:44












Java 11 is EOL in 4 months :P At that time many might shift to OpenJDK 11, instead of skipping to 12. e.g. adoptopenjdk.net
– Peter Lawrey
Nov 11 at 12:20





Java 11 is EOL in 4 months :P At that time many might shift to OpenJDK 11, instead of skipping to 12. e.g. adoptopenjdk.net
– Peter Lawrey
Nov 11 at 12:20













Anyway, I meant this as in "use of Java 10 API are permissible" (in the hopes that we have something less boilerplatey than the Java 8 streams collectors now). Does Java 11 bring something new to the table here (such as Collection literals)?
– Thilo
Nov 11 at 12:24





Anyway, I meant this as in "use of Java 10 API are permissible" (in the hopes that we have something less boilerplatey than the Java 8 streams collectors now). Does Java 11 bring something new to the table here (such as Collection literals)?
– Thilo
Nov 11 at 12:24





1




1




Not really imho, You can write entries.forEach((var e) -> result.put(e.getKey(), e.getValue())); note the var e, the real advantage is Long Term Support-ability.
– Peter Lawrey
Nov 11 at 12:30




Not really imho, You can write entries.forEach((var e) -> result.put(e.getKey(), e.getValue())); note the var e, the real advantage is Long Term Support-ability.
– Peter Lawrey
Nov 11 at 12:30












2 Answers
2






active

oldest

votes


















2














If you know for sure there are no duplicate keys, this is sufficient:



Map<Long, String> result = entries.stream().collect(Collectors.toMap(Map.Entry::getKey,Map.Entry::getValue));


If there may be duplicates, you'll have to add a merge function to handle them.






share|improve this answer




















  • Hmm. I made it a List of Map.Entry specifically to avoid having to specify again what the ::getKey and ::getValue should be, but I guess I'll have to go with that.
    – Thilo
    Nov 11 at 11:38






  • 1




    I do like the Guava answer from the duplicate thread. stackoverflow.com/a/44904884/14955 And java.util.Map.ofEntries is close, too, but does not work with a list.
    – Thilo
    Nov 11 at 11:40











  • What happens if I have duplicates but no merge function?
    – Thilo
    Nov 11 at 12:27






  • 1




    @Thilo An exception is thrown.
    – Eran
    Nov 11 at 12:28


















2














Flatten it:



Map<Long, String> result = 
entries.stream()
.collect(toMap(e -> e.getKey(), e -> e.getValue(), (a, b) -> b);


The (a, b) -> b means that the last value for duplicate keys will be taken, which matches the semantics of your current approach.






share|improve this answer






















  • Map.Entry has no method called entrySet
    – Ferrybig
    Nov 11 at 11:29










  • @Ferrybig my mistake, I thought it was a list of maps.
    – Andy Turner
    Nov 11 at 11:29


















2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














If you know for sure there are no duplicate keys, this is sufficient:



Map<Long, String> result = entries.stream().collect(Collectors.toMap(Map.Entry::getKey,Map.Entry::getValue));


If there may be duplicates, you'll have to add a merge function to handle them.






share|improve this answer




















  • Hmm. I made it a List of Map.Entry specifically to avoid having to specify again what the ::getKey and ::getValue should be, but I guess I'll have to go with that.
    – Thilo
    Nov 11 at 11:38






  • 1




    I do like the Guava answer from the duplicate thread. stackoverflow.com/a/44904884/14955 And java.util.Map.ofEntries is close, too, but does not work with a list.
    – Thilo
    Nov 11 at 11:40











  • What happens if I have duplicates but no merge function?
    – Thilo
    Nov 11 at 12:27






  • 1




    @Thilo An exception is thrown.
    – Eran
    Nov 11 at 12:28















2














If you know for sure there are no duplicate keys, this is sufficient:



Map<Long, String> result = entries.stream().collect(Collectors.toMap(Map.Entry::getKey,Map.Entry::getValue));


If there may be duplicates, you'll have to add a merge function to handle them.






share|improve this answer




















  • Hmm. I made it a List of Map.Entry specifically to avoid having to specify again what the ::getKey and ::getValue should be, but I guess I'll have to go with that.
    – Thilo
    Nov 11 at 11:38






  • 1




    I do like the Guava answer from the duplicate thread. stackoverflow.com/a/44904884/14955 And java.util.Map.ofEntries is close, too, but does not work with a list.
    – Thilo
    Nov 11 at 11:40











  • What happens if I have duplicates but no merge function?
    – Thilo
    Nov 11 at 12:27






  • 1




    @Thilo An exception is thrown.
    – Eran
    Nov 11 at 12:28













2












2








2






If you know for sure there are no duplicate keys, this is sufficient:



Map<Long, String> result = entries.stream().collect(Collectors.toMap(Map.Entry::getKey,Map.Entry::getValue));


If there may be duplicates, you'll have to add a merge function to handle them.






share|improve this answer












If you know for sure there are no duplicate keys, this is sufficient:



Map<Long, String> result = entries.stream().collect(Collectors.toMap(Map.Entry::getKey,Map.Entry::getValue));


If there may be duplicates, you'll have to add a merge function to handle them.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 11 at 11:27









Eran

278k37447533




278k37447533











  • Hmm. I made it a List of Map.Entry specifically to avoid having to specify again what the ::getKey and ::getValue should be, but I guess I'll have to go with that.
    – Thilo
    Nov 11 at 11:38






  • 1




    I do like the Guava answer from the duplicate thread. stackoverflow.com/a/44904884/14955 And java.util.Map.ofEntries is close, too, but does not work with a list.
    – Thilo
    Nov 11 at 11:40











  • What happens if I have duplicates but no merge function?
    – Thilo
    Nov 11 at 12:27






  • 1




    @Thilo An exception is thrown.
    – Eran
    Nov 11 at 12:28
















  • Hmm. I made it a List of Map.Entry specifically to avoid having to specify again what the ::getKey and ::getValue should be, but I guess I'll have to go with that.
    – Thilo
    Nov 11 at 11:38






  • 1




    I do like the Guava answer from the duplicate thread. stackoverflow.com/a/44904884/14955 And java.util.Map.ofEntries is close, too, but does not work with a list.
    – Thilo
    Nov 11 at 11:40











  • What happens if I have duplicates but no merge function?
    – Thilo
    Nov 11 at 12:27






  • 1




    @Thilo An exception is thrown.
    – Eran
    Nov 11 at 12:28















Hmm. I made it a List of Map.Entry specifically to avoid having to specify again what the ::getKey and ::getValue should be, but I guess I'll have to go with that.
– Thilo
Nov 11 at 11:38




Hmm. I made it a List of Map.Entry specifically to avoid having to specify again what the ::getKey and ::getValue should be, but I guess I'll have to go with that.
– Thilo
Nov 11 at 11:38




1




1




I do like the Guava answer from the duplicate thread. stackoverflow.com/a/44904884/14955 And java.util.Map.ofEntries is close, too, but does not work with a list.
– Thilo
Nov 11 at 11:40





I do like the Guava answer from the duplicate thread. stackoverflow.com/a/44904884/14955 And java.util.Map.ofEntries is close, too, but does not work with a list.
– Thilo
Nov 11 at 11:40













What happens if I have duplicates but no merge function?
– Thilo
Nov 11 at 12:27




What happens if I have duplicates but no merge function?
– Thilo
Nov 11 at 12:27




1




1




@Thilo An exception is thrown.
– Eran
Nov 11 at 12:28




@Thilo An exception is thrown.
– Eran
Nov 11 at 12:28













2














Flatten it:



Map<Long, String> result = 
entries.stream()
.collect(toMap(e -> e.getKey(), e -> e.getValue(), (a, b) -> b);


The (a, b) -> b means that the last value for duplicate keys will be taken, which matches the semantics of your current approach.






share|improve this answer






















  • Map.Entry has no method called entrySet
    – Ferrybig
    Nov 11 at 11:29










  • @Ferrybig my mistake, I thought it was a list of maps.
    – Andy Turner
    Nov 11 at 11:29
















2














Flatten it:



Map<Long, String> result = 
entries.stream()
.collect(toMap(e -> e.getKey(), e -> e.getValue(), (a, b) -> b);


The (a, b) -> b means that the last value for duplicate keys will be taken, which matches the semantics of your current approach.






share|improve this answer






















  • Map.Entry has no method called entrySet
    – Ferrybig
    Nov 11 at 11:29










  • @Ferrybig my mistake, I thought it was a list of maps.
    – Andy Turner
    Nov 11 at 11:29














2












2








2






Flatten it:



Map<Long, String> result = 
entries.stream()
.collect(toMap(e -> e.getKey(), e -> e.getValue(), (a, b) -> b);


The (a, b) -> b means that the last value for duplicate keys will be taken, which matches the semantics of your current approach.






share|improve this answer














Flatten it:



Map<Long, String> result = 
entries.stream()
.collect(toMap(e -> e.getKey(), e -> e.getValue(), (a, b) -> b);


The (a, b) -> b means that the last value for duplicate keys will be taken, which matches the semantics of your current approach.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 11 at 12:52

























answered Nov 11 at 11:28









Andy Turner

80k879133




80k879133











  • Map.Entry has no method called entrySet
    – Ferrybig
    Nov 11 at 11:29










  • @Ferrybig my mistake, I thought it was a list of maps.
    – Andy Turner
    Nov 11 at 11:29

















  • Map.Entry has no method called entrySet
    – Ferrybig
    Nov 11 at 11:29










  • @Ferrybig my mistake, I thought it was a list of maps.
    – Andy Turner
    Nov 11 at 11:29
















Map.Entry has no method called entrySet
– Ferrybig
Nov 11 at 11:29




Map.Entry has no method called entrySet
– Ferrybig
Nov 11 at 11:29












@Ferrybig my mistake, I thought it was a list of maps.
– Andy Turner
Nov 11 at 11:29





@Ferrybig my mistake, I thought it was a list of maps.
– Andy Turner
Nov 11 at 11:29




Popular posts from this blog

How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

Syphilis

Darth Vader #20