基于AVL实现的Map
public class AVLMap<K extends Comparable<K>, V> implements Map<K, V> {
private AVLTree<K, V> avl;
public AVLMap(){
avl = new AVLTree<>();
}
@Override
public int getSize(){
return avl.getSize();
}
@Override
public boolean isEmpty(){
return avl.isEmpty();
}
@Override
public void add(K key, V value){
avl.add(key, value);
}
@Override
public boolean contains(K key){
return avl.contains(key);
}
@Override
public V get(K key){
return avl.get(key);
}
@Override
public void set(K key, V newValue){
avl.set(key, newValue);
}
@Override
public V remove(K key){
return avl.remove(key);
}
}
不同实现的Map性能比较
public class TestMapMain {
private static double testMap(Map<String, Integer> map, String filename){
long startTime = System.nanoTime();
System.out.println(filename);
ArrayList<String> words = new ArrayList<>();
if(FileOperation.readFile(filename, words)) {
System.out.println("Total words: " + words.size());
for (String word : words){
if(map.contains(word))
map.set(word, map.get(word) + 1);
else
map.add(word, 1);
}
System.out.println("Total different words: " + map.getSize());
System.out.println("Frequency of PRIDE: " + map.get("pride"));
System.out.println("Frequency of PREJUDICE: " + map.get("prejudice"));
}
long endTime = System.nanoTime();
return (endTime - startTime) / 1000000000.0;
}
public static void main(String[] args) {
String filename = "pride-and-prejudice.txt";
BSTMap<String, Integer> bstMap = new BSTMap<>();
double time1 = testMap(bstMap, filename);
System.out.println("BST Map: " + time1 + " s");
System.out.println();
LinkedListMap<String, Integer> linkedListMap = new LinkedListMap<>();
double time2 = testMap(linkedListMap, filename);
System.out.println("Linked List Map: " + time2 + " s");
System.out.println();
AVLMap<String, Integer> avlMap = new AVLMap<>();
double time3 = testMap(avlMap, filename);
System.out.println("AVL Map: " + time3 + " s");
}
}
输出:
pride-and-prejudice.txt
Total words: 125901
Total different words: 6530
Frequency of PRIDE: 53
Frequency of PREJUDICE: 11
BST Map: 0.1964334 s
pride-and-prejudice.txt
Total words: 125901
Total different words: 6530
Frequency of PRIDE: 53
Frequency of PREJUDICE: 11
Linked List Map: 14.8544489 s
pride-and-prejudice.txt
Total words: 125901
Total different words: 6530
Frequency of PRIDE: 53
Frequency of PREJUDICE: 11
AVL Map: 0.1144626 s
基于AVL实现的Set
public class AVLSet<E extends Comparable<E>> implements Set<E> {
private AVLTree<E, Object> avl;
public AVLSet(){
avl = new AVLTree<>();
}
@Override
public int getSize(){
return avl.getSize();
}
@Override
public boolean isEmpty(){
return avl.isEmpty();
}
@Override
public void add(E e){
avl.add(e, null);
}
@Override
public boolean contains(E e){
return avl.contains(e);
}
@Override
public void remove(E e){
avl.remove(e);
}
}
不同实现的Set性能比较
public class TestSetMain {
private static double testSet(Set<String> set, String filename){
long startTime = System.nanoTime();
System.out.println(filename);
ArrayList<String> words = new ArrayList<>();
if(FileOperation.readFile(filename, words)) {
System.out.println("Total words: " + words.size());
for (String word : words)
set.add(word);
System.out.println("Total different words: " + set.getSize());
}
long endTime = System.nanoTime();
return (endTime - startTime) / 1000000000.0;
}
public static void main(String[] args) {
String filename = "pride-and-prejudice.txt";
BSTSet<String> bstSet = new BSTSet<>();
double time1 = testSet(bstSet, filename);
System.out.println("BST Set: " + time1 + " s");
System.out.println();
LinkedListSet<String> linkedListSet = new LinkedListSet<>();
double time2 = testSet(linkedListSet, filename);
System.out.println("Linked List Set: " + time2 + " s");
System.out.println();
AVLSet<String> avlSet = new AVLSet<>();
double time3 = testSet(avlSet, filename);
System.out.println("AVL Set: " + time3 + " s");
}
}
输出:
pride-and-prejudice.txt
Total words: 125901
Total different words: 6530
BST Set: 0.1711018 s
pride-and-prejudice.txt
Total words: 125901
Total different words: 6530
Linked List Set: 2.9127663 s
pride-and-prejudice.txt
Total words: 125901
Total different words: 6530
AVL Set: 0.072973 s