关于collections.unmodifiablemap 的一点理解

本文介绍Java中Collections.unmodifiableMap方法的功能,它提供一个只读视图以保护内部Map不受修改。文章通过示例解释了如何使用该方法,并指出尽管Map本身不可修改,但其内部对象仍可能被更新。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这是collections.unmodifiablemap的API文档
* Returns an unmodifiable view of the specified map.  This method
* allows modules to provide users with "read-only" access to internal
* maps.  Query operations on the returned map "read through"
* to the specified map, and attempts to modify the returned
* map, whether direct or via its collection views, result in an
* <tt>UnsupportedOperationException</tt>.<p>

简单来说就是返回一份map的只读视图,任何企图修改map的操作都会直接返回UnsupportedOperationException。

其实这个不可修改的Map指的是Map本身不可修改,里面的对象若支持修改的话,其实也还是可以修改的。

 unmodifiableMap是Map接口的一个实现类,里面包装了一个map对于可能修改map的方法

通过下面的两段代码说明一下(代码和collections.unmodifiablemap 关系不大,只是增加一点理解)

/**
 * Created by Andrew  on 2017/5/30.
 */
public class SafePoint {
    private int x,y;

    private SafePoint(int [] a){ this(a[0],a[1]); }

    public SafePoint(SafePoint p){ this(p.get()); }
    public SafePoint(int x, int y){
        this.x = x;
        this.y = y;
    }

    public synchronized int[] get(){
        return new int[]{x,y};
    }

    public synchronized void set( int x, int y){
        this.x = x;
        this.y = y;
    }
}

import java.util.Collections;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * Created by Andrew  on 2017/5/30.
 */

public class PublishingVehicleTracker {
    private final Map<String,SafePoint> location;
    private final Map<String,SafePoint> unmodifiableMap;

    public PublishingVehicleTracker(Map<String,SafePoint> location){
        this.location = new ConcurrentHashMap<>(location);
        this.unmodifiableMap = Collections.unmodifiableMap(this.location);
    }

    public Map<String,SafePoint>getLocations(){
        return unmodifiableMap;
    }

    public SafePoint getLocation(String id){
        return location.get(id);
    }

    public void  setLocation(String id, int x, int y){
        if (!location.containsKey(id)){
            throw  new IllegalArgumentException("invalid vehicle name: "+id);
        }
            location.get(id).set(x,y);
    }
}

上例中location 和 unmodifiableMap都指向相同的数据,unmodifiableMap不支持修改。但在本例中可以通过unmodifiableMap.get(id).set(x,y)修改map中的元素;可以将SafePoint的x, y设置为final来防止修改。


D:\Program Files\elasticsearch-8.9.0\bin>elasticsearch-setup-passwords.bat interactive 09:23:57.180 [main] WARN org.elasticsearch.deprecation.common.settings.Settings - data_stream.dataset="deprecation.elasticsearch" data_stream.namespace="default" data_stream.type="logs" elasticsearch.event.category="settings" event.code="keystore.password" message="[keystore.password] setting was deprecated in Elasticsearch and will be removed in a future release." Exception in thread "main" org.elasticsearch.ElasticsearchSecurityException: failed to load SSL configuration [xpack.security.transport.ssl] - cannot specify both [keystore.secure_password] and [keystore.password] at org.elasticsearch.xpack.core.ssl.SSLService.lambda$getSSLConfigurations$4(SSLService.java:550) at java.base/java.util.HashMap.forEach(HashMap.java:1429) at java.base/java.util.Collections$UnmodifiableMap.forEach(Collections.java:1553) at org.elasticsearch.xpack.core.ssl.SSLService.getSSLConfigurations(SSLService.java:542) at org.elasticsearch.xpack.core.ssl.SSLService.<init>(SSLService.java:147) at org.elasticsearch.xpack.core.security.CommandLineHttpClient.execute(CommandLineHttpClient.java:149) at org.elasticsearch.xpack.core.security.CommandLineHttpClient.execute(CommandLineHttpClient.java:112) at org.elasticsearch.xpack.security.authc.esnative.tool.SetupPasswordTool$SetupCommand.checkElasticKeystorePasswordValid(SetupPasswordTool.java:340) at org.elasticsearch.xpack.security.authc.esnative.tool.SetupPasswordTool$InteractiveSetup.execute(SetupPasswordTool.java:203) at org.elasticsearch.common.cli.EnvironmentAwareCommand.execute(EnvironmentAwareCommand.java:54) at org.elasticsearch.cli.Command.mainWithoutErrorHandling(Command.java:85) at org.elasticsearch.cli.MultiCommand.execute(MultiCommand.java:94) at org.elasticsearch.cli.Command.mainWithoutErrorHandling(Command.java:85) at org.elasticsearch.cli.Command.main(Command.java:50) at org.elasticsearch.launcher.CliToolLauncher.main(CliToolLauncher.java:64) Caused by: org.elasticsearch.common.ssl.SslConfigException: cannot specify both [keystore.secure_password] and [keystore.password]
最新发布
06-11
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值