一、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变成具体类型的数组]
在程序中,往往得到一个List, 程序要求对应赋值给一个array,
可以这样写程序:
for example:
Long [] l = new Long[list.size()];
for(int i=0;i<list.size();i++)
l[i] = (Long) list.get(i);
要写这些code,似乎比较繁琐,
其实List提供了toArray()的方法,但是要使用不好,就会有ClassCastException
究竟这个是如何产生的,且看代码:
-----------------------------------------------------------------------------------
List list = new ArrayList();
list.add(new Long(1));list.add(new Long(2));
list.add(new Long(3));list.add(new Long(4));
Long[] l = (Long[])list.toArray();
for(int i=0; i<l.length; i++)
System.out.println(l[i].longValue());
-----------------------------------------------------------------------------------
红色代码会抛java.lang.ClassCastException。
List list = new ArrayList();
list.add(new Long(1));list.add(new Long(2));
list.add(new Long(3));list.add(new Long(4));
Long[] l = (Long[])list.toArray();
for(int i=0; i<l.length; i++)
System.out.println(l[i].longValue());
-----------------------------------------------------------------------------------
红色代码会抛java.lang.ClassCastException。
当然,为了读出值来,你可以这样code:
-----------------------------------------------------------------------------------
Object [] a = list.toArray();
for(int i=0;i<a.length;i++)
System.out.println(((Long)a[i]).longValue());
-----------------------------------------------------------------------------------
但是让数组丢失了类型信息,这个不是我们想要得。:(
-----------------------------------------------------------------------------------
Object [] a = list.toArray();
for(int i=0;i<a.length;i++)
System.out.println(((Long)a[i]).longValue());
-----------------------------------------------------------------------------------
但是让数组丢失了类型信息,这个不是我们想要得。:(
正确使用方式:
-----------------------------------------------------------------------------------
1. Long[] l = new Long[<total size>];
list.toArray(l);
list.toArray(l);
2. Long[] l = (Long []) list.toArray(new Long[0]); 【*力推这种】
3. Long [] a = new Long[<total size>];
Long [] l = (Long []) list.toArray(a);
-----------------------------------------------------------------------------------
Long [] l = (Long []) list.toArray(a);
-----------------------------------------------------------------------------------
java sdk doc 上讲:
public Object[] toArray(Object[] a)
a--the array into which the elements of this list are to be stored, if it is big enough; otherwise, a new array of the same runtime type is allocated for this purpose.
如果这个数组a足够大,就会把数据全放进去,返回的数组也是指向这个数组;
要是不够大,就申请一个跟参数同样类型的数组,把值放进去,然后返回。
注意的是:你要是传入的参数为9个大小,而list里面有5个object,那么其他的四个很可能是null ,
使用的时候要注意。