在Java 8中,如何将Map转换为另一个Map使用lambda?

时间:2022-10-06 19:35:24

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:

我刚刚开始研究Java 8,并试着用lambdas试着重写我最近写的一件非常简单的事情。我需要将一个字符串映射到另一个字符串映射到列中,新映射中的列是第一个映射中的列的防御性副本。列有一个复制构造函数。到目前为止,我最接近的是:

    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.

但我相信一定有更好的方法来做这件事,我很感激能给你一些建议。

6 个解决方案

#1


125  

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) {}
  }
}

#2


19  

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.

你可以用forEach方法来做你想做的事。

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);

#3


10  

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.

不需要将所有条目重新插入新映射的方法应该是最快的,因为HashMap不会。复制内部也执行rehash。

Map<String, Column> newColumnMap = originalColumnMap.clone();
newColumnMap.replaceAll((s, c) -> new Column(c));

#4


2  

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.

如果您不介意使用第三方库,我的cyclops-react lib会为所有JDK集合类型提供扩展,包括Map。您可以直接使用map或bimap方法来转换您的地图。MapX可以从现有的地图中构造出来。

  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.

bimap可以同时转换键值和值。

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

当MapX扩展Map时,生成的映射也可以被定义为。

  Map<String, Column> y

#5


0  

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));

#6


-1  

Keep it Simple and use Java 8:-

保持简单并使用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())
                                  );

#1


125  

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) {}
  }
}

#2


19  

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.

你可以用forEach方法来做你想做的事。

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);

#3


10  

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.

不需要将所有条目重新插入新映射的方法应该是最快的,因为HashMap不会。复制内部也执行rehash。

Map<String, Column> newColumnMap = originalColumnMap.clone();
newColumnMap.replaceAll((s, c) -> new Column(c));

#4


2  

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.

如果您不介意使用第三方库,我的cyclops-react lib会为所有JDK集合类型提供扩展,包括Map。您可以直接使用map或bimap方法来转换您的地图。MapX可以从现有的地图中构造出来。

  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.

bimap可以同时转换键值和值。

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

当MapX扩展Map时,生成的映射也可以被定义为。

  Map<String, Column> y

#5


0  

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));

#6


-1  

Keep it Simple and use Java 8:-

保持简单并使用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())
                                  );