Java程序性能优化之集合
集合
关于结合用的较多的List Map Set等常用数据结构
我们先来来看下List接口相关的类 有ArrayList Vector LinkedList 他们都实现了List接口并扩展自AbstractList
ArrayList 和Vector 内部均由数组实现 而LinkedList则是用双向链表实现,所以说如果对数据的查询操作比较频繁则使用ArrayList 如果对数据的删除插入操作比较频繁,则使用LinKedList.至于Vector 是一种线程安全的集合,但实际上ArrayList和Vector的功能特性相差无几。
接下来看下Map接口
Map基本分为两大派系
一种是实现了Map接口并扩展自AbstractMap的有HashMap <--LinkedHashMap TreeMap
一种是实现了Map接口并扩展自Dictonnary的有Hashtable ßPropertities
关于HashMap是一种线程不安全的结合,其大致是将hash算法直接映射到内存取到value
基本操作如下:
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
public class asdf {
public static void main(String[] args) {
//Map am = new HashMap();
Map am = new LinkedHashMap(16,0.75f,true);//这些参数代表开启按访问顺序排序功能这时先按插入顺序,再按访问顺序
am.put(2,"2");
am.put(1, "1");
am.put(4,"4");
am.put(3,"3");
am.get(3);
am.get(2);
//am.get(1);
am.get(4);
/*Iterator iterator=am.entrySet().iterator();
while(iterator.hasNext()) {
Map.Entry entry=(Map.Entry)iterator.next();
System.out.println(entry.getKey()+"-->"+entry.getValue());
}*/
/*Iterator iterator=am.keySet().iterator();
while(iterator.hasNext()) {
Object key=iterator.next();
//String name=key;
String value=(String)am.get(key);
System.out.println(key+"-->"+value);
}*/
for(Iterator iterator=am.keySet().iterator();iterator.hasNext();) {
//这里值得注意的是不能在这里使用remove() put() get()操作 这些都会将数据顺序该变
Object in=iterator.next();
System.out.println(in);
}
}
}
map实现的单例能否做到同时做到延迟加载和同步
貌似不能
LinkedHashMap 在HashMap的基础上增加了链表以维护数据的添加和访问顺序。
TreeMap是一种不同的Map实现,TreeMap比HashMap有更强大的功能,然而TreeMap却比HashMap的性能较低。TreeMap实现了sortedMap接口,可以实现排序。这种排序和LinkedHashMap的排序方式是不一样的,TreeMap是基于元数排序,而LinkedHashMap则是基于数据添加和访问的顺序进行排序。
下面我们来看一下TreeMap的具体代码使用
public class Student implements Comparable<Student>{
private String name;
private int score;
public Student(String name,int score) {
this.name=name;
this.score=score;
}
@Override
public int compareTo(Student o) {
// TODO Auto-generated method stub
if(o.score<this.score) {
return 1;
}else if(o.score>this.score) {
return -1;
}
return 0;
}
public String toString() {
StringBuffer sb = new StringBuffer();
sb.append("name:");
sb.append(name);
sb.append(" ");
sb.append("score");
sb.append(score);
return sb.toString();
}
public String getName() {
return name;
}
}
import java.util.Iterator;
import java.util.Map;
import java.util.TreeMap;
public class StudentDetailInfo {
Student s;
public StudentDetailInfo(Student s) {
this.s=s;
}
public String toString() {
return s.getName()+"'s detail information";
}
public static void main(String[] args) {
Map map=new TreeMap();
Student s1=new Student("li1",45);
Student s2=new Student("li2",55);
Student s3=new Student("li3",65);
Student s4=new Student("li4",75);
StudentDetailInfo sf1 = new StudentDetailInfo(s1);
StudentDetailInfo sf2 = new StudentDetailInfo(s2);
StudentDetailInfo sf3 = new StudentDetailInfo(s3);
StudentDetailInfo sf4 = new StudentDetailInfo(s4);
map.put(s1, sf1);
map.put(s2,sf2);
map.put(s3,sf3);
map.put(s4,sf4);
Map map1 = ((TreeMap)map).subMap(s1,s4);//[S1,s4)
Map map2 = ((TreeMap)map).headMap(s4);//<s4
Map map3 = ((TreeMap)map).tailMap(s3);//>=s3
for(Iterator it = map3.keySet().iterator();it.hasNext();) {
Student student=(Student)it.next();
System.out.println(map3.get(student));
}
}
}
使用Treemapde subMap headMap tailMap 可以有效实现排序查询
实际上TreeHap是基于红黑树的内部实现 所以说TreeMap的效率还是非常高的
在实际开发中遇到排序可以考虑TreeMap使用
实现Set接口的集合实际上是对实现Map接口的类的封装 实现set接口的类并没有在Colloection接口之上增加额外的操作
HashSet 与HashMap对应
LinkedHashSet与LinkedHashMap对应
treeSet与treeMap对应
相应输出也大致相同