161

I've just started looking at Java 8 and to try out lambdas I thought I'd try to rewrite a very simple thing I wrote recently. I need to turn a Map of String to Column into another Map of String to Column where the Column in the new Map is a defensive copy of the Column in the first Map. Column has a copy constructor. The closest I've got so far is:

    Map<String, Column> newColumnMap= new HashMap<>();
    originalColumnMap.entrySet().stream().forEach(x -> newColumnMap.put(x.getKey(), new Column(x.getValue())));

but I'm sure there must be a nicer way to do it and I'd be grateful for some advice.

Stuart Marks
  • 112,017
  • 32
  • 182
  • 245
annesadleir
  • 1,722
  • 2
  • 10
  • 8

7 Answers7

256

You could use a Collector:

import java.util.*;
import java.util.stream.Collectors;

public class Defensive {

  public static void main(String[] args) {
    Map<String, Column> original = new HashMap<>();
    original.put("foo", new Column());
    original.put("bar", new Column());

    Map<String, Column> copy = original.entrySet()
        .stream()
        .collect(Collectors.toMap(Map.Entry::getKey,
                                  e -> new Column(e.getValue())));

    System.out.println(original);
    System.out.println(copy);
  }

  static class Column {
    public Column() {}
    public Column(Column c) {}
  }
}
McDowell
  • 102,869
  • 29
  • 193
  • 261
  • 10
    I think the important (and slight counter-intuitive) thing to note in the above is that the transformation takes place in the collector, *rather* than in a map() stream operation – Brian Agnew May 03 '19 at 11:40
27
Map<String, Integer> map = new HashMap<>();
map.put("test1", 1);
map.put("test2", 2);

Map<String, Integer> map2 = new HashMap<>();
map.forEach(map2::put);

System.out.println("map: " + map);
System.out.println("map2: " + map2);
// Output:
// map:  {test2=2, test1=1}
// map2: {test2=2, test1=1}

You can use the forEach method to do what you want.

What you're doing there is:

map.forEach(new BiConsumer<String, Integer>() {
    @Override
    public void accept(String s, Integer integer) {
        map2.put(s, integer);     
    }
});

Which we can simplify into a lambda:

map.forEach((s, integer) ->  map2.put(s, integer));

And because we're just calling an existing method we can use a method reference, which gives us:

map.forEach(map2::put);
Oleksandr Pyrohov
  • 13,194
  • 4
  • 51
  • 85
Arrem
  • 935
  • 8
  • 10
  • 8
    I like how you've explained exactly what's going on behind the scenes here, it makes it much clearer. But I think that just gives me a straight copy of my original map, not a new map where the values have been turned into defensive copies. Am I understanding you correctly that the reason we can just use (map2::put) is that the same arguments are going into the lambda, e.g. (s,integer) as are going into the put method? So to do a defensive copy would it need to be `originalColumnMap.forEach((string, column) -> newColumnMap.put(string, new Column(column)));` or could I shorten that? – annesadleir Mar 30 '14 at 16:11
  • Yes you are. If you're just passing in the same arguments you can use a method reference, however in this case, since you have the `new Column(column)` as a paremeter, you'll have to go with that. – Arrem Mar 30 '14 at 16:19
  • I like this answer better because it works if both maps are already provided to you, such as in session renewal. – David Stanley Nov 02 '16 at 18:38
  • Not sure to understand the point of such an answer. It is rewriting the constructor of mostly all Map implementations from an existing Map - like [this](https://docs.oracle.com/javase/8/docs/api/java/util/HashMap.html#HashMap-java.util.Map-). – Kineolyan Apr 24 '20 at 13:37
13

The way without re-inserting all entries into the new map should be the fastest it won't because HashMap.clone internally performs rehash as well.

Map<String, Column> newColumnMap = originalColumnMap.clone();
newColumnMap.replaceAll((s, c) -> new Column(c));
leventov
  • 12,780
  • 10
  • 60
  • 91
  • That's a very readable way to do it and quite concise. It looks like as well as HashMap.clone() there's `Map newColumnMap = new HashMap<>(originalColumnMap);`. I don't know if it's any different under the covers. – annesadleir Mar 30 '14 at 21:33
  • 2
    This approach has the advantage of keeping the `Map` implementation’s behavior, i.e. if `Map` is an `EnumMap` or a `SortedMap`, the resulting `Map` will be as well. In case of a `SortedMap` with a special `Comparator` it might make a huge semantic difference. Oh well, the same applies to an `IdentityHashMap`… – Holger Aug 01 '14 at 08:24
9

Keep it Simple and use Java 8:-

 Map<String, AccountGroupMappingModel> mapAccountGroup=CustomerDAO.getAccountGroupMapping();
 Map<String, AccountGroupMappingModel> mapH2ToBydAccountGroups = 
              mapAccountGroup.entrySet().stream()
                         .collect(Collectors.toMap(e->e.getValue().getH2AccountGroup(),
                                                   e ->e.getValue())
                                  );
kutschkem
  • 6,194
  • 3
  • 16
  • 43
Anant Khurana
  • 101
  • 1
  • 3
6

If you use Guava (v11 minimum) in your project you can use Maps::transformValues.

Map<String, Column> newColumnMap = Maps.transformValues(
  originalColumnMap,
  Column::new // equivalent to: x -> new Column(x) 
)

Note: The values of this map are evaluated lazily. If the transformation is expensive you can copy the result to a new map like suggested in the Guava docs.

To avoid lazy evaluation when the returned map doesn't need to be a view, copy the returned map into a new map of your choosing.
Andrea Bergonzo
  • 2,051
  • 2
  • 16
  • 25
  • Note: According to the docs, this returns a lazy evaluated view on the originalColumnMap, so the function Column::new is reevaluated each time the entry is accessed (which may not be desirable when the mapping function is expensive) – Erric Nov 11 '19 at 11:57
  • Correct. If the transformation is expensive you are probably fine with the overhead of copying that to a new map like suggested in the docs.`To avoid lazy evaluation when the returned map doesn't need to be a view, copy the returned map into a new map of your choosing.` – Andrea Bergonzo Nov 11 '19 at 19:33
  • True, but it might be worth adding as a footnote in the answer for those who tend to skip reading the docs. – Erric Nov 20 '19 at 05:45
  • @Erric Makes sense, just added. – Andrea Bergonzo Nov 20 '19 at 14:48
4

Here is another way that gives you access to the key and the value at the same time, in case you have to do some kind of transformation.

Map<String, Integer> pointsByName = new HashMap<>();
Map<String, Integer> maxPointsByName = new HashMap<>();

Map<String, Double> gradesByName = pointsByName.entrySet().stream()
        .map(entry -> new AbstractMap.SimpleImmutableEntry<>(
                entry.getKey(), ((double) entry.getValue() /
                        maxPointsByName.get(entry.getKey())) * 100d))
        .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
Lucas Ross
  • 912
  • 5
  • 14
1

If you don't mind using 3rd party libraries, my cyclops-react lib has extensions for all JDK Collection types, including Map. You can directly use the map or bimap methods to transform your Map. A MapX can be constructed from an existing Map eg.

  MapX<String, Column> y = MapX.fromMap(orgColumnMap)
                               .map(c->new Column(c.getValue());

If you also wish to change the key you can write

  MapX<String, Column> y = MapX.fromMap(orgColumnMap)
                               .bimap(this::newKey,c->new Column(c.getValue());

bimap can be used to transform the keys and values at the same time.

As MapX extends Map the generated map can also be defined as

  Map<String, Column> y
John McClean
  • 4,767
  • 1
  • 20
  • 30