一、map的用法
map1.putAll(map2)意思是将map2的元素(k,v)复制到map1中。map2有可能覆盖map1中同key的元素。
import java.util.HashMap; public class Map_putAllTest { public static void main(String[] args){ //两个map具有不同的key HashMap map1=new HashMap(); map1.put("1", "A"); HashMap map2 = new HashMap(); map2.put("2", "B"); map2.put("3", "C"); map1.putAll(map2); System.out.println(map1); //两个map具有重复的key HashMap map3=new HashMap(); map3.put("1", "A"); HashMap map4 = new HashMap(); map4.put("1", "B"); map4.put("3", "C"); map3.putAll(map4); System.out.println(map3); } }
/* 输出结果:
* {3=C, 2=B, 1=A}
* {3=C, 1=B}
* 结论:putAll可以合并两个MAP,只不过如果有相同的key那么用后面的覆盖前面的
*/
http://hi.baidu.com/nevenchen/blog/item/f3dc5dc3049724110ef477ba.html
二、Collections类
不要与名为Collection的接口混淆--在Java集合架构中担当着不同角色。Collections类导出的静态方法适用于所有集合。
public class CollectionsDemo { @SuppressWarnings({ "unchecked", "serial" }) public static void main(String[] args) { List list = new ArrayList(){ { add("3"); add("2"); add("6"); add("1"); } }; System.out.println(list); Collections.sort(list); Object obj = Collections.max(list); System.out.println(obj); Collections.reverse(list); System.out.println(list); Collections.swap(list, 0, 1); System.out.println(list); Collections.rotate(list, 2); System.out.println(list); Collections.shuffle(list,new Random(3)); System.out.println(list); List empList = Collections.EMPTY_LIST; System.out.println(empList+"\t"+empList.size()); Map empMap = Collections.EMPTY_MAP; System.out.println(empMap+""+empMap.size()); Set empSet = Collections.EMPTY_SET; System.out.println(empSet+"\t"+empSet.size()); } }
运行结果:
[3, 2, 6, 1] 6 [6, 3, 2, 1] [3, 6, 2, 1] [2, 1, 3, 6] [1, 2, 6, 3] [] 0 {} 0 [] 0
三、List
主要方法:add()、addAll(Collection e)、
get(i)、remove(i)、set(index,o)、clear()、siEmpty()、size()、contains(Object)、toArray(T[] a)
应用:[如何将一个List变成具体类型的数组]
public Object[] toArray(Object[] a)