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.
java java-8 java-stream
add a comment |
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.
java java-8 java-stream
Shouldn'tMap.Entry<String, Object> hashMap
beMap<String, Object> hashMap
? You use that variable as if it's aMap
.
– Eran
Nov 22 at 7:46
add a comment |
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.
java java-8 java-stream
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
java java-8 java-stream
asked Nov 22 at 7:27
Cherry
8,7742390161
8,7742390161
Shouldn'tMap.Entry<String, Object> hashMap
beMap<String, Object> hashMap
? You use that variable as if it's aMap
.
– Eran
Nov 22 at 7:46
add a comment |
Shouldn'tMap.Entry<String, Object> hashMap
beMap<String, Object> hashMap
? You use that variable as if it's aMap
.
– 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
add a comment |
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.
It's a bit long winded but... you could implement a mutable version ofContext
which accumulates (key, value) pairs, build it up while streaming the map and then hand it over to the provided context withcontext.addAll( myMutableContext)
– David Soroko
Nov 22 at 15:52
@DavidSoroko Your approach will iteratively create a newContext
for each entry fromContext
(containing previous entries) provided as a parameter toContext.putAll
.
– ETO
Nov 23 at 1:58
add a comment |
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.
add a comment |
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.
It's a bit long winded but... you could implement a mutable version ofContext
which accumulates (key, value) pairs, build it up while streaming the map and then hand it over to the provided context withcontext.addAll( myMutableContext)
– David Soroko
Nov 22 at 15:52
@DavidSoroko Your approach will iteratively create a newContext
for each entry fromContext
(containing previous entries) provided as a parameter toContext.putAll
.
– ETO
Nov 23 at 1:58
add a comment |
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.
It's a bit long winded but... you could implement a mutable version ofContext
which accumulates (key, value) pairs, build it up while streaming the map and then hand it over to the provided context withcontext.addAll( myMutableContext)
– David Soroko
Nov 22 at 15:52
@DavidSoroko Your approach will iteratively create a newContext
for each entry fromContext
(containing previous entries) provided as a parameter toContext.putAll
.
– ETO
Nov 23 at 1:58
add a comment |
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.
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.
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 ofContext
which accumulates (key, value) pairs, build it up while streaming the map and then hand it over to the provided context withcontext.addAll( myMutableContext)
– David Soroko
Nov 22 at 15:52
@DavidSoroko Your approach will iteratively create a newContext
for each entry fromContext
(containing previous entries) provided as a parameter toContext.putAll
.
– ETO
Nov 23 at 1:58
add a comment |
It's a bit long winded but... you could implement a mutable version ofContext
which accumulates (key, value) pairs, build it up while streaming the map and then hand it over to the provided context withcontext.addAll( myMutableContext)
– David Soroko
Nov 22 at 15:52
@DavidSoroko Your approach will iteratively create a newContext
for each entry fromContext
(containing previous entries) provided as a parameter toContext.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
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
edited Nov 23 at 2:01
answered Nov 22 at 22:49
ETO
1,118117
1,118117
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
Shouldn't
Map.Entry<String, Object> hashMap
beMap<String, Object> hashMap
? You use that variable as if it's aMap
.– Eran
Nov 22 at 7:46