> Java 集合数据结构
-- HashMap对象的key、value值均可为null。HahTable对象的key、value值均不可为null。
-- Vector & ArrayList
1) Vector的方法都是同步的(Synchronized),是线程安全的(thread-safe),而ArrayList的方法不是,由于线程的同步必然要影响性能,因此,ArrayList的性能比Vector好。
2) 当Vector或ArrayList中的元素超过它的初始大小时,Vector会将它的容量翻倍,而ArrayList只增加50%的大小,这样,ArrayList就有利于节约内存空间。
-- ArrayList的内部实现是基于内部数组Object[],所以从概念上讲,它更象数组,但LinkedList的内部实现是基于一组连接的记录,所以,它更象一个链表结构。
-- Vector & ArrayList & CopyOnWriteArrayList,这三个集合类都继承List接口
1、ArrayList是线程不安全的;
2、Vector是比较古老的线程安全的,但性能不行;
3、CopyOnWriteArrayList在兼顾了线程安全的同时,又提高了并发性,性能比Vector有不少提高
CopyOnWriteArrayList 比 Vector 高效,主要有以下 2 个原因:
1、Vector 中,读写操作都被加锁了,而 CopyOnWriteArrayList 中,只有写操作才被加锁,而读操作没有进行加锁。这样,当 读 线程数量远大于 写 线程数量的时候,CopyOnWriteArrayList 尤为高效。
2、Vector 中,使用的是 内置锁(内置锁参考:Java内置锁的简单认识),而 CopyOnWriteArrayList 中,使用的是 jdk 1.5 引入的 ReentrantLock ,相比于 内置锁,ReentrantLock 的性能还是有所提升的。
Copy-on-Write,也就是“写时复制”,当有 写类型的操作作用到 CopyOnWriteArrayList 对象的时候,它们都会先获取锁,然后复制一份当前数据作为副本,然后在当前的数据副本上做修改,最后把修改提交,然后释放锁。
Array-List,也就是底层是使用数组(顺序表)来进行数据存储。
CopyOnWriteArrayList是ArrayList的一个线程安全的变体,其中所有可变操作(add、set等等)都是通过对底层数组进行一次新的复制来实现的。CopyOnWriteArrayList适合使用在读操作远远大于写操作的场景里,比如缓存。发生修改时候做copy,新老版本分离,保证读的高性能,适用于以读为主的情况。
-- Stack,Stack 实现了一个 LIFO 的栈的功能,他是继承于 Vector 来实现的,所以内部也是数组。
-- 队列集合主要有ArrayBlockingQueue,LinkedBlockingQueue,PriorityBlockingQueue,SynchronousQueue,DelayQueue这个五个实现类。在这五个阻塞队列中,比较常用的是ArrayBlockingQueue,LinkedBlockingQueue
> Collection(Collections.sort)中的Values排序问题
list、set、map的排序,包括按照map的value进行排序。
list排序可以直接采用Collections的sort方法,也可以使用Arrays的sort方法,归根结底Collections就是调用Arrays的sort方法。set包括HashSet和TreeSet,HashSet是基于HashMap的,TreeSet是基于TreeMap的。
map包括HashMap和TreeMap,TreeMap是用红黑树实现,天然就具有排序功能,“天然就具有排序功能”是指它拥有升序、降序的迭代器。
Comparator是个接口,可重写compare()及equals()这两个方法,用于比价功能;
java中Collections.sort排序详解- http://blog.csdn.net/tjcyjd/article/details/6804690
java Collection中的排序问题- http://www.cnblogs.com/huangfox/archive/2012/07/05/2577306.html
Collections的sort方法 排序-http://blog.csdn.net/ydd326/article/details/6761447
List,Set,Map用法以及区别-http://blog.csdn.net/vstar283551454/article/details/8682655
List,Set,Map集合使用方法总结-http://blog.sina.com.cn/s/blog_624a352c0100nnll.html
> List集合排重/排序
1.集合排重HashSet;!list.contains(list.get(i))
2.集合排序使用Collections工具类,实现了Comparable接口的类传入里面调用一下Collections.sort() 方法
java中Collections.sort() 排序函数的用法- http://www.blogjava.net/landor2004/articles/sort.html
JAVA集合中常见的两种去重的两种方式- http://blog.csdn.net/code_my_dream/article/details/72818700
-- 排重
一、相对简单的做法,回需要额外的存储空间:
1、重写Bean的hashcode和equals两个方法
2、然后new HashSet(List<Bean>),这样HashSet中的元素就是去重的了
二、使用双重循环,使用前一个元素与后面的元素逐个比较,如果相同,则把后面相同的元素删除
import java.util.HashSet;
import java.util.Iterator;
public class Bean{
public static void main(String[] args){
String userId = "001";
String ip = "192.168.1.1";
String roomId = "ROOM-001";
Bean bean1_1 = new Bean(userId,"192.168.0.1","ROOM-011","uuid","name");
Bean bean1_2 = new Bean(userId,"192.168.0.2","ROOM-012","uuid","name");
Bean bean2_1 = new Bean("id21",ip,"ROOM-021","uuid","name");
Bean bean2_2 = new Bean("id22",ip,"ROOM-022","uuid","name");
Bean bean3_1 = new Bean("id31","192.168.0.3",roomId,"uuid","name");
Bean bean3_2 = new Bean("id32","192.168.0.4",roomId,"uuid","name");
HashSet<Bean> beans = new HashSet<>();
Bean.setCondition(USERID);
beans.add(bean1_1);
beans.add(bean1_2);
beans.add(bean2_1);
beans.add(bean2_2);
beans.add(bean3_1);
beans.add(bean3_2);
beans = printBeans(beans,IP);
beans = printBeans(beans,ROOMID);
printBeans(beans,ROOMID);
}
private static HashSet<Bean> printBeans(HashSet<Bean> beans,int condition){
Bean.setCondition(condition);
HashSet<Bean> result = new HashSet<>();
System.out.println("Beans size: " + beans.size());
System.out.println("----------contain-------------");
Iterator<Bean> iterator = beans.iterator();
Bean bean = null;
for(;iterator.hasNext();){
bean = iterator.next();
result.add(bean);
System.out.println(bean);
}
System.out.println("------------------------------");
return result;
}
public Bean(String userId,String ip,String roomId,String uuid,String name){
this.userId = userId;
this.ip = ip;
this.roomId = roomId;
this.uuid = uuid;
this.name = name;
}
@Override
public String toString(){
return String.format("Bean[userId:%s,ip:%s,roomId:%s,uuid:%s,name:%s]",userId,ip,roomId,uuid,name);
}
public static void setCondition(int value){
condition = value;
}
@Override
public int hashCode(){
switch(condition){
case USERID :
return userId.hashCode();
case IP :
return ip.hashCode();
case ROOMID :
return roomId.hashCode();
default :
return 0;
}
}
@Override
public boolean equals(Object obj){
if(!(obj instanceof Bean)){
return false;
}
Bean bean = (Bean)obj;
switch(condition){
case USERID :
return userId.equals(bean.userId);
case IP:
return ip.equals(bean.ip);
case ROOMID:
return roomId.equals(bean.roomId);
default :
return true;
}
}
private String userId = null;
private String ip = null;
private String roomId = null;
private String uuid = null;
private String name = null;
/*----------Constant-----------*/
public static final int USERID = 1;
public static final int IP = 2;
public static final int ROOMID = 3;
public static int condition = 0;
}