How map stream to immutable java class?











up vote
1
down vote

favorite












Consider a piece of code:



imnport reactor.util.context.Context

public Context addAll (Context ctx, Map.Entry<String, Object> hashMap) {
Context ctxVar = ctx;
for (Map.Entry<String, Object> e : hashMap.entrySet()) {
if (e.getValue() != null) {
ctxVar = ctxVar.put(e.getKey(), e.getValue());
}
}
return ctxVar;
}


reactor.util.context.Context is immutable class. So put merges old context with new added value and returns new
context.



The question is - is there more compact way to "combine" HashMap into immutable object using java 8 streams? (Not for Context class only)



Note: I have read about java stream collect and it seems that does not work because I have to supply initial Context
and combine several contexts after map but recreate entire context for combine operations I think is too much.










share|improve this question






















  • Shouldn't Map.Entry<String, Object> hashMap be Map<String, Object> hashMap? You use that variable as if it's a Map.
    – Eran
    Nov 22 at 7:46















up vote
1
down vote

favorite












Consider a piece of code:



imnport reactor.util.context.Context

public Context addAll (Context ctx, Map.Entry<String, Object> hashMap) {
Context ctxVar = ctx;
for (Map.Entry<String, Object> e : hashMap.entrySet()) {
if (e.getValue() != null) {
ctxVar = ctxVar.put(e.getKey(), e.getValue());
}
}
return ctxVar;
}


reactor.util.context.Context is immutable class. So put merges old context with new added value and returns new
context.



The question is - is there more compact way to "combine" HashMap into immutable object using java 8 streams? (Not for Context class only)



Note: I have read about java stream collect and it seems that does not work because I have to supply initial Context
and combine several contexts after map but recreate entire context for combine operations I think is too much.










share|improve this question






















  • Shouldn't Map.Entry<String, Object> hashMap be Map<String, Object> hashMap? You use that variable as if it's a Map.
    – Eran
    Nov 22 at 7:46













up vote
1
down vote

favorite









up vote
1
down vote

favorite











Consider a piece of code:



imnport reactor.util.context.Context

public Context addAll (Context ctx, Map.Entry<String, Object> hashMap) {
Context ctxVar = ctx;
for (Map.Entry<String, Object> e : hashMap.entrySet()) {
if (e.getValue() != null) {
ctxVar = ctxVar.put(e.getKey(), e.getValue());
}
}
return ctxVar;
}


reactor.util.context.Context is immutable class. So put merges old context with new added value and returns new
context.



The question is - is there more compact way to "combine" HashMap into immutable object using java 8 streams? (Not for Context class only)



Note: I have read about java stream collect and it seems that does not work because I have to supply initial Context
and combine several contexts after map but recreate entire context for combine operations I think is too much.










share|improve this question













Consider a piece of code:



imnport reactor.util.context.Context

public Context addAll (Context ctx, Map.Entry<String, Object> hashMap) {
Context ctxVar = ctx;
for (Map.Entry<String, Object> e : hashMap.entrySet()) {
if (e.getValue() != null) {
ctxVar = ctxVar.put(e.getKey(), e.getValue());
}
}
return ctxVar;
}


reactor.util.context.Context is immutable class. So put merges old context with new added value and returns new
context.



The question is - is there more compact way to "combine" HashMap into immutable object using java 8 streams? (Not for Context class only)



Note: I have read about java stream collect and it seems that does not work because I have to supply initial Context
and combine several contexts after map but recreate entire context for combine operations I think is too much.







java java-8 java-stream






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 22 at 7:27









Cherry

8,7742390161




8,7742390161












  • Shouldn't Map.Entry<String, Object> hashMap be Map<String, Object> hashMap? You use that variable as if it's a Map.
    – Eran
    Nov 22 at 7:46


















  • Shouldn't Map.Entry<String, Object> hashMap be Map<String, Object> hashMap? You use that variable as if it's a Map.
    – Eran
    Nov 22 at 7:46
















Shouldn't Map.Entry<String, Object> hashMap be Map<String, Object> hashMap? You use that variable as if it's a Map.
– Eran
Nov 22 at 7:46




Shouldn't Map.Entry<String, Object> hashMap be Map<String, Object> hashMap? You use that variable as if it's a Map.
– Eran
Nov 22 at 7:46












2 Answers
2






active

oldest

votes

















up vote
0
down vote













You can use reduce:



Context ctxVar = hashMap.entrySet()
.stream()
.filter(e -> e.getValue() != null)
.reduce(ctx,
(c, e) -> c.put(e.getKey(), e.getValue()),
(c1, c2) -> c1.putAll(c2));


It does seem wasteful, though (in the same way your original loop is wasteful), since it creates multiple Context instances when only the last one is needed.



It would make more sense if you write a static method of the Context class (or a constructor) that accepts a Map and only creates a single Context instance for the entries of the Map. However, I now noticed that you didn't write this Context class, so you can't change it.






share|improve this answer























  • It's a bit long winded but... you could implement a mutable version of Context which accumulates (key, value) pairs, build it up while streaming the map and then hand it over to the provided context with context.addAll( myMutableContext)
    – David Soroko
    Nov 22 at 15:52












  • @DavidSoroko Your approach will iteratively create a new Context for each entry from Context(containing previous entries) provided as a parameter to Context.putAll.
    – ETO
    Nov 23 at 1:58




















up vote
0
down vote













Your question interested me. I researched the subject.



Here is what I came up with:



package reactor.util.context;

import java.util.Map;
import java.util.Map.Entry;
import static java.util.stream.Collectors.toMap;

public class ContextUtils {
public static Context putAll(Context context, Map map) {
if (map.isEmpty()) {
return context;
} else {
Map contextMap = context.stream()
.filter(e -> e.getValue() != null)
.collect(toMap(Entry::getKey, Entry::getValue));
return new ContextN(contextMap, map);
}
}
}


This utility method creates a new Context containing the entries from both initial context and provided map.



This solution may still look not optimal, as it creates a new HashMap with initiaCapacity = 0 and loadFactor = 0.75f. In fact, this is not a problem, since ContextN itself is a subclass of HashMap with precise capacity and loadFactor = 1. The data from intermediary map will be copied to the context and later it will be collected by GC.



Note: the utility class has to be in package reactor.util.context. ContextN is package-private so it is not accessible from other packages.






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53425826%2fhow-map-stream-to-immutable-java-class%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    You can use reduce:



    Context ctxVar = hashMap.entrySet()
    .stream()
    .filter(e -> e.getValue() != null)
    .reduce(ctx,
    (c, e) -> c.put(e.getKey(), e.getValue()),
    (c1, c2) -> c1.putAll(c2));


    It does seem wasteful, though (in the same way your original loop is wasteful), since it creates multiple Context instances when only the last one is needed.



    It would make more sense if you write a static method of the Context class (or a constructor) that accepts a Map and only creates a single Context instance for the entries of the Map. However, I now noticed that you didn't write this Context class, so you can't change it.






    share|improve this answer























    • It's a bit long winded but... you could implement a mutable version of Context which accumulates (key, value) pairs, build it up while streaming the map and then hand it over to the provided context with context.addAll( myMutableContext)
      – David Soroko
      Nov 22 at 15:52












    • @DavidSoroko Your approach will iteratively create a new Context for each entry from Context(containing previous entries) provided as a parameter to Context.putAll.
      – ETO
      Nov 23 at 1:58

















    up vote
    0
    down vote













    You can use reduce:



    Context ctxVar = hashMap.entrySet()
    .stream()
    .filter(e -> e.getValue() != null)
    .reduce(ctx,
    (c, e) -> c.put(e.getKey(), e.getValue()),
    (c1, c2) -> c1.putAll(c2));


    It does seem wasteful, though (in the same way your original loop is wasteful), since it creates multiple Context instances when only the last one is needed.



    It would make more sense if you write a static method of the Context class (or a constructor) that accepts a Map and only creates a single Context instance for the entries of the Map. However, I now noticed that you didn't write this Context class, so you can't change it.






    share|improve this answer























    • It's a bit long winded but... you could implement a mutable version of Context which accumulates (key, value) pairs, build it up while streaming the map and then hand it over to the provided context with context.addAll( myMutableContext)
      – David Soroko
      Nov 22 at 15:52












    • @DavidSoroko Your approach will iteratively create a new Context for each entry from Context(containing previous entries) provided as a parameter to Context.putAll.
      – ETO
      Nov 23 at 1:58















    up vote
    0
    down vote










    up vote
    0
    down vote









    You can use reduce:



    Context ctxVar = hashMap.entrySet()
    .stream()
    .filter(e -> e.getValue() != null)
    .reduce(ctx,
    (c, e) -> c.put(e.getKey(), e.getValue()),
    (c1, c2) -> c1.putAll(c2));


    It does seem wasteful, though (in the same way your original loop is wasteful), since it creates multiple Context instances when only the last one is needed.



    It would make more sense if you write a static method of the Context class (or a constructor) that accepts a Map and only creates a single Context instance for the entries of the Map. However, I now noticed that you didn't write this Context class, so you can't change it.






    share|improve this answer














    You can use reduce:



    Context ctxVar = hashMap.entrySet()
    .stream()
    .filter(e -> e.getValue() != null)
    .reduce(ctx,
    (c, e) -> c.put(e.getKey(), e.getValue()),
    (c1, c2) -> c1.putAll(c2));


    It does seem wasteful, though (in the same way your original loop is wasteful), since it creates multiple Context instances when only the last one is needed.



    It would make more sense if you write a static method of the Context class (or a constructor) that accepts a Map and only creates a single Context instance for the entries of the Map. However, I now noticed that you didn't write this Context class, so you can't change it.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 22 at 7:41

























    answered Nov 22 at 7:35









    Eran

    274k35439521




    274k35439521












    • It's a bit long winded but... you could implement a mutable version of Context which accumulates (key, value) pairs, build it up while streaming the map and then hand it over to the provided context with context.addAll( myMutableContext)
      – David Soroko
      Nov 22 at 15:52












    • @DavidSoroko Your approach will iteratively create a new Context for each entry from Context(containing previous entries) provided as a parameter to Context.putAll.
      – ETO
      Nov 23 at 1:58




















    • It's a bit long winded but... you could implement a mutable version of Context which accumulates (key, value) pairs, build it up while streaming the map and then hand it over to the provided context with context.addAll( myMutableContext)
      – David Soroko
      Nov 22 at 15:52












    • @DavidSoroko Your approach will iteratively create a new Context for each entry from Context(containing previous entries) provided as a parameter to Context.putAll.
      – ETO
      Nov 23 at 1:58


















    It's a bit long winded but... you could implement a mutable version of Context which accumulates (key, value) pairs, build it up while streaming the map and then hand it over to the provided context with context.addAll( myMutableContext)
    – David Soroko
    Nov 22 at 15:52






    It's a bit long winded but... you could implement a mutable version of Context which accumulates (key, value) pairs, build it up while streaming the map and then hand it over to the provided context with context.addAll( myMutableContext)
    – David Soroko
    Nov 22 at 15:52














    @DavidSoroko Your approach will iteratively create a new Context for each entry from Context(containing previous entries) provided as a parameter to Context.putAll.
    – ETO
    Nov 23 at 1:58






    @DavidSoroko Your approach will iteratively create a new Context for each entry from Context(containing previous entries) provided as a parameter to Context.putAll.
    – ETO
    Nov 23 at 1:58














    up vote
    0
    down vote













    Your question interested me. I researched the subject.



    Here is what I came up with:



    package reactor.util.context;

    import java.util.Map;
    import java.util.Map.Entry;
    import static java.util.stream.Collectors.toMap;

    public class ContextUtils {
    public static Context putAll(Context context, Map map) {
    if (map.isEmpty()) {
    return context;
    } else {
    Map contextMap = context.stream()
    .filter(e -> e.getValue() != null)
    .collect(toMap(Entry::getKey, Entry::getValue));
    return new ContextN(contextMap, map);
    }
    }
    }


    This utility method creates a new Context containing the entries from both initial context and provided map.



    This solution may still look not optimal, as it creates a new HashMap with initiaCapacity = 0 and loadFactor = 0.75f. In fact, this is not a problem, since ContextN itself is a subclass of HashMap with precise capacity and loadFactor = 1. The data from intermediary map will be copied to the context and later it will be collected by GC.



    Note: the utility class has to be in package reactor.util.context. ContextN is package-private so it is not accessible from other packages.






    share|improve this answer



























      up vote
      0
      down vote













      Your question interested me. I researched the subject.



      Here is what I came up with:



      package reactor.util.context;

      import java.util.Map;
      import java.util.Map.Entry;
      import static java.util.stream.Collectors.toMap;

      public class ContextUtils {
      public static Context putAll(Context context, Map map) {
      if (map.isEmpty()) {
      return context;
      } else {
      Map contextMap = context.stream()
      .filter(e -> e.getValue() != null)
      .collect(toMap(Entry::getKey, Entry::getValue));
      return new ContextN(contextMap, map);
      }
      }
      }


      This utility method creates a new Context containing the entries from both initial context and provided map.



      This solution may still look not optimal, as it creates a new HashMap with initiaCapacity = 0 and loadFactor = 0.75f. In fact, this is not a problem, since ContextN itself is a subclass of HashMap with precise capacity and loadFactor = 1. The data from intermediary map will be copied to the context and later it will be collected by GC.



      Note: the utility class has to be in package reactor.util.context. ContextN is package-private so it is not accessible from other packages.






      share|improve this answer

























        up vote
        0
        down vote










        up vote
        0
        down vote









        Your question interested me. I researched the subject.



        Here is what I came up with:



        package reactor.util.context;

        import java.util.Map;
        import java.util.Map.Entry;
        import static java.util.stream.Collectors.toMap;

        public class ContextUtils {
        public static Context putAll(Context context, Map map) {
        if (map.isEmpty()) {
        return context;
        } else {
        Map contextMap = context.stream()
        .filter(e -> e.getValue() != null)
        .collect(toMap(Entry::getKey, Entry::getValue));
        return new ContextN(contextMap, map);
        }
        }
        }


        This utility method creates a new Context containing the entries from both initial context and provided map.



        This solution may still look not optimal, as it creates a new HashMap with initiaCapacity = 0 and loadFactor = 0.75f. In fact, this is not a problem, since ContextN itself is a subclass of HashMap with precise capacity and loadFactor = 1. The data from intermediary map will be copied to the context and later it will be collected by GC.



        Note: the utility class has to be in package reactor.util.context. ContextN is package-private so it is not accessible from other packages.






        share|improve this answer














        Your question interested me. I researched the subject.



        Here is what I came up with:



        package reactor.util.context;

        import java.util.Map;
        import java.util.Map.Entry;
        import static java.util.stream.Collectors.toMap;

        public class ContextUtils {
        public static Context putAll(Context context, Map map) {
        if (map.isEmpty()) {
        return context;
        } else {
        Map contextMap = context.stream()
        .filter(e -> e.getValue() != null)
        .collect(toMap(Entry::getKey, Entry::getValue));
        return new ContextN(contextMap, map);
        }
        }
        }


        This utility method creates a new Context containing the entries from both initial context and provided map.



        This solution may still look not optimal, as it creates a new HashMap with initiaCapacity = 0 and loadFactor = 0.75f. In fact, this is not a problem, since ContextN itself is a subclass of HashMap with precise capacity and loadFactor = 1. The data from intermediary map will be copied to the context and later it will be collected by GC.



        Note: the utility class has to be in package reactor.util.context. ContextN is package-private so it is not accessible from other packages.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 23 at 2:01

























        answered Nov 22 at 22:49









        ETO

        1,118117




        1,118117






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53425826%2fhow-map-stream-to-immutable-java-class%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            What visual should I use to simply compare current year value vs last year in Power BI desktop

            Alexandru Averescu

            Trompette piccolo