什么是使用Java 8将2个或多个字段组合成Map中的键的最佳方法?

huangapple go评论64阅读模式
英文:

What is the best way to combine 2 or more fields as a Key in a Map, using Java 8?

问题

我一直面临多个需求,需要将列表转换为映射,并且在某些情况下,我需要将两个字段组合作为映射的键。以前我使用了以下解决方案 -

Map<String, Integer> employeePairs = employees.stream().collect(HashMap<String, Integer>::new,
                (m, c) -> m.put(c.getFirstName() + " " + c.getLastName(), c.employeeId),
                (m, u) -> {
                });

我找到了一个新的方法,但它使用了不同的包apache Pair,代码如下 -

Map<Pair<String, String>, Integer> employeePairs = employees.stream().collect(HashMap<Pair<String, String>, Integer>::new,
                (m, c) -> m.put(Pair.of(c.getFirstName(), c.getLastName()), c.employeeId),
                (m, u) -> {
                });
employeePairs.get(Pair.of("a1", "b1"));// 访问的方式

依赖包 -

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.11</version>
</dependency>

哪种方法更好?还有更好的方法吗?

英文:

I have been facing multiple requirements where I need to convert a List to Map, and in some cases I need to combine 2 fields as keys for that map, Previously I used the below solution -

Map&lt;String, Integer&gt; employeePairs = employees.stream().collect(HashMap&lt;String, Integer&gt;::new,
            (m, c) -&gt; m.put(c.getFirstName() + &quot; &quot; + c.getLastName(), c.employeeId),
            (m, u) -&gt; {
            });

I found a new way but that use a different package apache Pair the code looks like this-

Map&lt;Pair&lt;String, String&gt;, Integer&gt; employeePairs = employees.stream().collect(HashMap&lt;Pair&lt;String, String&gt;, Integer&gt;::new,
            (m, c) -&gt; m.put(Pair.of(c.getFirstName(), c.getLastName()), c.employeeId),
            (m, u) -&gt; {
            });
    employeePairs.get(Pair.of(&quot;a1&quot;, &quot;b1&quot;));// way of accessing

Package -

&lt;dependency&gt;
    &lt;groupId&gt;org.apache.commons&lt;/groupId&gt;
    &lt;artifactId&gt;commons-lang3&lt;/artifactId&gt;
    &lt;version&gt;3.11&lt;/version&gt;
&lt;/dependency&gt;

Which is a better way? Or is there a more better way.

答案1

得分: 1

如果您不想为仅使用Pair类而使用额外的库,您可以使用AbstractMap.SimpleEntry<K,V>来配对两个字段并收集为映射,最好使用Collectors.toMap

Map<AbstractMap.SimpleEntry<String, String>, Integer> employeePairs =   
    employees.stream()
             .collect(Collectors.toMap(
                c -> new AbstractMap.SimpleEntry<>(c.getFirstName(), c.getLastName()),
                c -> c.employeeId
              ));

您可以使用.getKey().getValue()访问AbstractMap.SimpleEntry的数据。

英文:

If you don't want to use an extra library for using only Pair class,
you can use AbstractMap.SimpleEntry<K,V> for pairing two fields and to collect as map, it's better to use Collectors.toMap

Map&lt;AbstractMap.SimpleEntry&lt;String, String&gt;, Integer&gt; employeePairs =   
    employees.stream()
             .collect(Collectors.toMap(
                c -&gt; new AbstractMap.SimpleEntry&lt;&gt;(c.getFirstName(), c.getLastName()),
                c -&gt; c.employeeId
              ));

You can access data of AbstractMap.SimpleEntry using .getKey() and .getValue()

huangapple
  • 本文由 发表于 2020年10月6日 01:15:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/64213300.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定