如何用Java创建不可变的Map

你好朋友,

在本教程中,我们将看到如何用Java创建不可变的Map。

–不可变的类或对象是什么意思?

–什么是不可变地图?

–如何在Java中创建不可变的Map?

不变的类或对象是什么意思?

不可变的类或对象是其状态一旦创建就不会改变的类或对象。例如,Java中的String类是不可变的,因此,如果我们尝试在String对象中进行更改,它将创建一个新的String对象,但是当前对象的状态不会改变。因此,如果实例化一个不可变的类,则一旦创建该实例,便无法更改其状态。

什么是不可变地图?

因此,考虑到以上对不可变的定义,不可变地图是指我们一旦创建就无法插入,更新或删除元素的地图,通常要求此类地图具有不希望更改的内容,例如国家/地区这是货币。

如何在Java中创建不可变的Map?

我们可以通过多种方式创建不可变地图。

–使用Collections.unmodifiableMap()

–使用Map.of()

–使用Map.ofEntries()

–使用Map.copyOf()

使用Collections.unmodifiableMap()

当我们使用Collections.unmodifiableMap(originalMap)时,它会在原始地图上创建一个视图,这样我们就不能在该视图上添加,删除或更新,如果尝试这样做,则会出现UnSupportedOperation异常,但我们只能查看在原始地图中。

我们仍然可以更新原始地图,并且在更改原始地图时,更改也将反映在视图中。 因此这并不是真正意义上的创建不可变映射。但是,我们仍然可以使用Collections.unmodifiableMap()创建不可变映射。 对于第二个例子。

Map<String, Integer> originalMap1 = new HashMap<String, Integer>(); 
    originalMap1.put("a", 1); 
    originalMap1.put("b", 2);
    originalMap1.put("c", 3);
    Map<String, Integer> unmodifiableMap1 = Collections.unmodifiableMap(originalMap1);
    //unmodifiableMap1.put("d", 4);
    System.out.println("Size of originalMap1 before adding new data:"+originalMap1.size());
    System.out.println("Size of unmodifiableMap1 before adding new data:"+ unmodifiableMap1.size());
    originalMap1.put("e", 5);
    System.out.println("Size of originalMap1 after adding new data:"+originalMap1.size());
    System.out.println("Size of unmodifiableMap1 after adding new data:"+unmodifiableMap1.size());

例子2

Map<String, Integer> originalMap2 = new HashMap<String, Integer>(); 
   originalMap2.put("a", 1); 
   originalMap2.put("b", 2);
   originalMap2.put("c", 3);
   Map<String, Integer> unmodifiableMap2 = Collections.unmodifiableMap(new HashMap<String, Integer>(originalMap2));
   //unmodifiableMap2.put("d", 4);
    System.out.println("Size of originalMap2 before adding new data:"+originalMap2.size());
    System.out.println("Size of unmodifiableMap2 before adding new data:"+ unmodifiableMap2.size());
    originalMap2.put("e", 5);
    System.out.println("Size of originalMap2 after adding new data:"+originalMap2.size());
    System.out.println("Size of unmodifiableMap2 after adding new data:"+unmodifiableMap2.size());

在这里,我们没有传递对原始地图的引用,而是创建了一个HashMap的新实例,将其传递给原始hashMap,然后将该HashMap的新实例传递给Collecitons.unmodifiableMap()方法。我们在此处获得的“ unmodifiableMap2”是不可变的,因此即使您在原始哈希图中进行了更改,也不会反映在unmodifiableMap2实例中。

使用Map.of()

Map.of()是Java 9中引入的。以下示例是不言自明的。如果我们有少于等于10个键值对,则应使用此方法,因为如果看到Of()方法的重载,则最多有10个Of()方法的重载中允许的键值对。

static <K, V> Map<K, V> of() 
static <K, V> Map<K, V> of(K k1, V v1) 
static <K, V> Map<K, V> of(K k1, V v1, K k2, V v2)
.
.
.
static <K, V> Map<K, V> of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5,
                               K k6, V v6, K k7, V v7, K k8, V v8, K k9, V v9, K k10, V v10)

Map<String, Integer> immutableMap1 = Map.of("a", 1, "b",2, "c",3);
   //immutableMap1.put("d", 4);     //Will throw UnsupportedOperaironException
    System.out.println("Size of immutableMap1:"+ immutableMap1.size());

使用Map.ofEntries()

Java 9中也引入了Map.ofEntries(),当我们拥有超过10个键值对时,可以使用这种创建不可变映射的方法。

该方法的签名如下:

static <K, V> Map<K, V> ofEntries(Entry<? extends K, ? extends V>... entries)

Map<String, Integer> immutableMap2 = Map.ofEntries(
    entry("a",1),
    entry("b",2),
    entry("c",3));
    //immutableMap2.put("d", 4);
    System.out.println("Size of immutableMap2 :"+immutableMap2.size());

使用Map.copyOf()

Java 10中引入了Map.copyOf()。

该方法的签名如下:

static <K, V> Map<K, V> copyOf(Map<? extends K, ? extends V> map)

Map<String, Integer> originalMap5 = new HashMap<String, Integer>(); 
    originalMap5.put("a", 1); 
    originalMap5.put("b", 2);
    originalMap5.put("c", 3);
    Map<String, Integer> immutableMap3 = Map.copyOf(originalMap5);
    //immutableMap3.put("d", 4);
    System.out.println("Size of originalMap5 before adding new data:"+originalMap5.size());
    originalMap5.put("e", 5);
    System.out.println("Size of originalMap5 after adding new data:"+originalMap5.size());
    System.out.println("Size of immutableMap3 after adding new data:"+immutableMap3.size());

package com.blogspot.javasolutionsguide.immutable_map_java_example;

import static java.util.Map.entry;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

/**
 * ImmutableMapTest.
 *
 */
public class ImmutableMapTest 
{
    public static void main( String[] args ) {
     
     //Unmodifiable 
     Map<String, Integer> originalMap1 = new HashMap<String, Integer>(); 
     originalMap1.put("a", 1); 
     originalMap1.put("b", 2);
     originalMap1.put("c", 3);
     Map<String, Integer> unmodifiableMap1 = Collections.unmodifiableMap(originalMap1);
     //unmodifiableMap1.put("d", 4);
     System.out.println("Size of originalMap1 before adding new data:"+originalMap1.size());
     originalMap1.put("e", 5);
     System.out.println("Size of originalMap1 after adding new data:"+originalMap1.size());
     System.out.println("Size of unmodifiableMap1 after adding new data:"+unmodifiableMap1.size());
        
     
     //Example 2
     Map<String, Integer> originalMap2 = new HashMap<String, Integer>(); 
     originalMap2.put("a", 1); 
     originalMap2.put("b", 2);
     originalMap2.put("c", 3);
     Map<String, Integer> unmodifiableMap2 = Collections.unmodifiableMap(new HashMap<String, Integer>(originalMap2));
     //unmodifiableMap2.put("d", 4);
     System.out.println("Size of originalMap2 before adding new data:"+originalMap2.size());
     originalMap2.put("e", 5);
     System.out.println("Size of originalMap2 after adding new data:"+originalMap2.size());
     System.out.println("Size of unmodifiableMap2 after adding new data:"+unmodifiableMap2.size());
     
     //Example 3
    Map<String, Integer> immutableMap1 = Map.of("a", 1, "b",2, "c",3);
   //immutableMap1.put("d", 4);     //Will throw UnsupportedOperaironException
    System.out.println("Size of immutableMap1:"+ immutableMap1.size());
     
     //Example 4
    Map<String, Integer> immutableMap2 = Map.ofEntries(
    entry("a",1),
    entry("b",2),
    entry("c",3));
    //immutableMap2.put("d", 4);
    System.out.println("Size of immutableMap2 :"+immutableMap2.size());
     
     //Example 5
     Map<String, Integer> originalMap5 = new HashMap<String, Integer>(); 
     originalMap5.put("a", 1); 
     originalMap5.put("b", 2);
     originalMap5.put("c", 3);
     Map<String, Integer> immutableMap3 = Map.copyOf(originalMap5);
     //immutableMap3.put("d", 4);
     System.out.println("Size of originalMap5 before adding new data:"+originalMap5.size());
     originalMap5.put("e", 5);
     System.out.println("Size of originalMap5 after adding new data:"+originalMap5.size());
     System.out.println("Size of immutableMap3 after adding new data:"+immutableMap3.size());
     
    }
}

总结:

因此,在本教程中,我们了解了如何在Java中创建不可变映射。当我们知道将来映射的内容不会改变时,这非常有用。我们了解了JDK如何使用各种方法来创建不可变映射。

谢谢阅读。 订阅我们的博客以获取更多此类有趣的帖子。

翻译自: https://www.javacodegeeks.com/2020/03/how-to-create-immutabe-map-in-java.html

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值