set的java.lang.UnsupportedOperationException异常的解决

set的UnsupportedOperationException异常的解决:


Map中的keySet是不支持addAll()方法的,在开发中用到的keySet的addAll()的时候就会报UnsupportedOperationException

,那么如何解决呢?其实很简单。

public class Test {
@SuppressWarnings("unchecked")
public static void main(String[] args) {
	@SuppressWarnings("rawtypes")
	Map map =new HashMap();
	map.put("js", "jskl");
	map.put("j1s", "jskl");
	map.put("j2s", "jskl");
	map.put("j3s", "jskl");
	Map map1 =new HashMap();
	map1.put("j4s", "jskl");
	Set set=new HashSet();
	set.addAll(map.keySet());
	//set=map.keySet();
	//set.addAll(map1.keySet());
	set.addAll(map1.keySet());
	System.out.println("打印输出");
	
}
}

 
以上代码中演示了什么情况下报异常,以及如何解决的。








猜你喜欢

转载自blog.csdn.net/mr2zhang/article/details/80351261