List在调用add、remove方法后报java.lang.UnsupportedOperationException

今天工作中碰到List对象调用add、remove方法竟然报错,用了好几年了尽然一直没发现,不知道之前写了多少bug。

在使用Arrays.asList()后调用add,remove这些method时出现java.lang.UnsupportedOperationException异常。这是由于Arrays.asList() 返回java.util.Arrays$ArrayList,而不是ArrayList。Arrays$ArrayList和ArrayList都是继承AbstractList,remove,add等method在AbstractList中是默认throw UnsupportedOperationException而且不作任何操作。ArrayList override这些method来对list进行操作,但是Arrays$ArrayList没有override remove(),add()等,所以throw UnsupportedOperationException。


Arrays源码分析:

    public static  List asList(T... a) {
        return new ArrayList<>(a);
    }
Arrays.asList()方法返回的是Arrays.ArrayList对象,而不是java.util.ArrayList对象。Arrays.ArrayList和java.util.ArrayList类都继承AbstractList抽象类,Arrays.ArrayList类没有重写add、remove等方法,java.util.ArrayList类重写了add、remove等方法。

    private static class ArrayList extends AbstractList
        implements RandomAccess, java.io.Serializable

使用Arrays.asList()方法返回的对象去调用add、remove方法时,实际调用的是AbstractList抽象类的,该类的实现如下:

public void add(int index, E element) {
    throw new UnsupportedOperationException();
}
public E remove(int index) {
    throw new UnsupportedOperationException();
}


所以,使用Arrays.asList()返回对象调用add、remove方法时直接报错。


代码如下:

public void updateUserTodayHasNoReadWord(String openId, String word, int flag) {
	String dateStr = TimeUtils.formatDateToStr(new Date(),"yyyyMMdd");
	String key = Constants.POCKET_YUEDU_USER_NO_READ_WORD_KEY + openId + "_" + dateStr;
	List noReadWordList = new ArrayList<>();
	String noReadWord = redisTemplate.get(key);// 今日未读单词
	if (org.apache.commons.lang.StringUtils.isNotBlank(noReadWord)) {
		noReadWordList = Arrays.asList(noReadWord.split(","));
		noReadWordList.remove(word);
		noReadWord = String.join(",", noReadWordList);
		redisTemplate.setex(key, Constants.REDIS_TIME_OUT_ONE_DAY, noReadWord);
	}
}
运行代码后,在调用remove()方法处报错,java.lang.UnsupportedOperationException异常。

解决方法:

public void updateUserTodayHasNoReadWord(String openId, String word, int flag) {
	String dateStr = TimeUtils.formatDateToStr(new Date(),"yyyyMMdd");
	String key = Constants.POCKET_YUEDU_USER_NO_READ_WORD_KEY + openId + "_" + dateStr;
	List noReadWordList = new ArrayList<>();
	String noReadWord = redisTemplate.get(key);// 今日未读单词
	if (org.apache.commons.lang.StringUtils.isNotBlank(noReadWord)) {
		noReadWordList = new ArrayList<>(Arrays.asList(noReadWord.split(",")));
		noReadWordList.remove(word);
		noReadWord = String.join(",", noReadWordList);
		redisTemplate.setex(key, Constants.REDIS_TIME_OUT_ONE_DAY, noReadWord);
	}
}

参考
When you call Arrays.asList it does not return a java.util.ArrayList. It returns a java.util.Arrays$ArrayList which is an immutable list. You cannot add to it and you cannot remove from it.


If you want a mutable list built from your array you will have to loop over the array yourself and add each element into the list in turn.


Even then your code won't work because you'll get an IndexOutOfBoundsException as you remove the elements from the list in the for loop. There are two options: use an Iterator which allows you to remove from the list as you iterate over it (my recommendation as it makes the code easier to maintain) or loop backwards over the loop removing from the last one downwards (harder to read). 


You are using AbstractList. ArrayList and Arrays$ArrayList are both types of AbstractList. That's why you get UnsupportedOperationException: Arrays$ArrayList does not override remove(int) so the method is called on the superclass, AbstractList, which is what is throwing the exception because this method is not implemented on that class (the reason being to allow you to build immutable subclasses).


另外:List转换成字符串并加入分隔符,可以StringUtils的join方法
public String listToString(List list, char separator) {  
   org.apache.commons.lang.StringUtils.join(list.toArray(),separator);  
}


转载自:http://blog.csdn.net/fygkchina/article/details/32329293

你可能感兴趣的:(java)