【Java】关于java.lang.UnsupportedOperationException异常错误的处理

今天在使用,set集合的add方法时,抛出了一个异常,java.lang.UnsupportedOperationException

下面进行错误的复盘,代码如下:

public static void main(String[] args) {
        Set<Object> set = PropertiesUtil.getKeyValue("xzqhall");
        set.add("这个字符串加不进去");//该方法执行会抛出异常,java.lang.UnsupportedOperationException
        Set<Object> set_copy = new HashSet<Object>();
        set_copy.addAll(set);
        set_copy.add("这条加得进去");
        System.out.println("加进去之后:");
        System.out.println(set_copy);//输出:[这条加得进去, 420172, 420111, 420132, 420130, 420115, 420104, 420114, 420103, 420102, 420112, 420119, 420108, 420118, 420129, 420107, 420117, 420128, 420106, 420116, 420105]
    }

其中,xzqhall为属性文件,内容如下:

420102=江岸大队
420103=江汉大队
420104=硚口大队
420105=汉阳大队
420106=武昌大队
420107=青山大队
420108=开发区大队
420111=洪山大队
420112=东西湖大队
420114=蔡甸大队
420115=江夏大队
420116=黄陂大队
420117=新洲大队
420118=东新大队
420172=化工大队
420119=东湖大队
420128=长江大桥大队
420129=二桥大队
420130=高管大队
420132=白沙洲大桥大队

PropertiesUtil类中的getKeyValue()函数如下:

public static Set<Object> getKeyValue(String propertyname) {
        String url = Thread.currentThread().getContextClassLoader().getResource("").toString();
        url = url.substring(url.indexOf("/") + 1);
        url = url.replaceAll("%20", " ");
        Properties properties = new Properties();
        try {
            InputStreamReader insReader = new InputStreamReader(new FileInputStream(url + propertyname + ".properties"),
                    "UTF-8");
            properties.load(insReader);
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        Set<Object> keyValue = properties.keySet();
        return keyValue;
    }

上面函数的作用就是读取配置文件中的key,并以set集合的形式返回。

那么,为什么上面函数返回的set集合不能执行add方法呢?

我们看PropertiesUtil类里的getKeyValue方法,看到倒数第二行代码,properties.keySet()方法,该方法的源码如下(位于Hashtable类中):

 /**
     * Returns a {@link Set} view of the keys contained in this map.
     * The set is backed by the map, so changes to the map are
     * reflected in the set, and vice-versa.  If the map is modified
     * while an iteration over the set is in progress (except through
     * the iterator's own <tt>remove</tt> operation), the results of
     * the iteration are undefined.  The set supports element removal,
     * which removes the corresponding mapping from the map, via the
     * <tt>Iterator.remove</tt>, <tt>Set.remove</tt>,
     * <tt>removeAll</tt>, <tt>retainAll</tt>, and <tt>clear</tt>
     * operations.  It does not support the <tt>add</tt> or <tt>addAll</tt>
     * operations.
     *
     * @since 1.2
     */
    public Set<K> keySet() {
        if (keySet == null)
            keySet = Collections.synchronizedSet(new KeySet(), this);
        return keySet;
    }

根据上面的源码,可以通过注释看到,该方法返回的set集合不支持add,addAll操作。

因此会抛出异常。

那么怎么解决上面set集合不能添加元素的问题呢?

解决方法:

新建一个set集合,利用该集合的addAll方法,复制上面的PropertiesUtil.getKeyValue(“xzqhall”)返回的set集合,再利用新建的那个集合利用add方法,添加自己想要的元素。

Set<Object> set = PropertiesUtil.getKeyValue("xzqhall");
//      set.add("这个字符串加不进去");
Set<Object> set_copy = new HashSet<Object>();
set_copy.addAll(set);
set_copy.add("这条加得进去");

Set接口的知识点,需要知道的是:set集合继承自Collection接口,set集合里的元素不重复,如果添加一个已经存在的元素,会添加不成功。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值