我一直在为 SCJP 学习,现在是 Oracle Certified Professional Java SE Programmer 考试。
我很难理解所有不同的系列以及何时使用它们。我也喜欢闪存卡。因此,我尝试创建一组基本相同的类,除了它们使用的是哪个集合。我必须确定输出将如何出现以及每个集合的主要“特征”是什么。
不幸的是,我不相信自己。我希望有人确认所有信息都是准确的,或者是否缺少任何信息。然后在一些反馈/更正之后,我认为这对于任何试图理解 Java 集合的人来说都是一个很好的练习。
涵盖的集合有:HashMap、Hashtable、TreeMap、LinkedHashMap、HashSet、TreeSet、LinkedHashSet、ArrayList、Vector、LinkedList、PriorityQueue。
我也把所有的文件都分开了,可以在这里下载:http ://www.allgo.com/personal/MyCollections.zip
提前致谢
导入 java.util.*;
导入java.lang.*;
MyItem 类实现 Comparable{
私有字符串名称;
我的项目(字符串 n){ 名称 = n; }
public String toString(){返回名称;}
public String getName(){返回名称;}
公共布尔等于(对象 obj){
if(this==obj) 返回真;
否则如果(obj==null)返回假;
否则 if(getName() != ((MyItem)obj).getName()) 返回 false;
否则返回真;
}
public int hashCode(){ return 5; }
public int compareTo(MyItem b){return b.getName().compareTo(getName());}
}
公共类 MyCollections{
公共静态无效主要(字符串[]参数){
MyHashMap.main(args); System.out.println("HashMap: Hash=Unsorted, Unordered.Map=key/value pair\n##\n");
MyHashtable.main(args); System.out.println("哈希表:线程安全。哈希=未排序,未排序。映射=键/值对\n##\n");
MyTreeMap.main(args); System.out.println("TreeMap: Tree=sorted.Map=key/value.\n##\n");
MyLinkedHashMap.main(args); System.out.println("LinkedHashMap: Linked=维护插入顺序。Hash=未排序,无序。Map=键/值对。\n##\n");
MyHashSet.main(args); System.out.println("HashSet: Hash=Unsorted, Unordered.Set=Unique.Define=equals/hashCode\n##\n");
MyTreeSet.main(args); System.out.println("TreeSet: Tree=Sorted.Set=Unique.Define=Comparable/Comparator\n##\n");
MyLinkedHashSet.main(args); System.out.println("LinkedHashSet: Liniked=维护插入顺序。Hash=未排序。Set=Unique。Define=equals/hashCode\n##\n");
MyArrayList.main(args); System.out.println("ArrayList: List=Queue.维护插入顺序,允许重复\n##\n");
MyVector.main(args); System.out.println("向量:线程安全。ArrayList。维护插入顺序,允许重复\n##\n");
MyLinkedList.main(args); System.out.println("LinkedList: Linked=Maintaines Insertion Order.List=Queue.Advanced ArrayList with more methods.\n##\n");
MyPriorityQueue.main(args); System.out.println("PriorityQueue:Define=Comparable/比较器\n##\n");
}
}
类 MyHashMap{
公共静态无效主要(字符串[]参数){
哈希映射 c = 新哈希映射();
MyItem 八 = new MyItem("Eight");
c.put(5, new MyItem("五")); c.put(1, new MyItem("One")); c.put(8, 八); c.put(3, new MyItem("三"));
c.put(4, new MyItem("Four")); c.put(1, new MyItem("1")); c.put(8, 八); c.put(9, new MyItem("九"));
c.删除(3);c.put(7, new MyItem("七"));
System.out.println(c);//输出?
}
}
类 MyHashtable{
公共静态无效主要(字符串[]参数){
哈希表 c = 新哈希表();
MyItem 八 = new MyItem("Eight");
c.put(5, new MyItem("五")); c.put(1, new MyItem("One")); c.put(8, 八); c.put(3, new MyItem("三"));
c.put(4, new MyItem("Four")); c.put(1, new MyItem("1")); c.put(8, 八); c.put(9, new MyItem("九"));
c.删除(3);c.put(7, new MyItem("七"));
System.out.println(c);//输出?
}
}
类 MyTreeMap{
公共静态无效主要(字符串[]参数){
树图 c = 新树图();
MyItem 八 = new MyItem("Eight");
c.put(5, new MyItem("五")); c.put(1, new MyItem("One")); c.put(8, 八); c.put(3, new MyItem("三"));
c.put(4, new MyItem("Four")); c.put(1, new MyItem("1")); c.put(8, 八); c.put(9, new MyItem("九"));
c.删除(3);c.put(7, new MyItem("七"));
System.out.println(c);//输出?
}
}
类 MyLinkedHashMap{
公共静态无效主要(字符串[]参数){
LinkedHashMap c = new LinkedHashMap();
MyItem 八 = new MyItem("Eight");
c.put(5, new MyItem("五")); c.put(1, new MyItem("One")); c.put(8, 八); c.put(3, new MyItem("三"));
c.put(4, new MyItem("Four")); c.put(1, new MyItem("1")); c.put(8, 八); c.put(9, new MyItem("九"));
c.删除(3);c.put(7, new MyItem("七"));
System.out.println(c);//输出?
}
}
类 MyHashSet{
公共静态无效主要(字符串[]参数){
哈希集 c = 新哈希集();
MyItem 八 = new MyItem("Eight");
c.add(new MyItem("五")); c.add(new MyItem("One")); c.add(八); c.add(new MyItem("三"));
c.add(new MyItem("Four")); c.add(new MyItem("One")); c.add(八); c.add(new MyItem("九"));
c.删除(3);c.add(new MyItem("七"));
System.out.println(c);//输出?
}
}
类 MyTreeSet{
公共静态无效主要(字符串[]参数){
树集 c = 新树集();
MyItem 八 = new MyItem("Eight");
c.add(new MyItem("五")); c.add(new MyItem("One")); c.add(八); c.add(new MyItem("三"));
c.add(new MyItem("Four")); c.add(new MyItem("One")); c.add(八); c.add(new MyItem("九"));
c.remove(八); c.add(new MyItem("七"));
System.out.println(c);//输出?
}
}
类 MyLinkedHashSet{
公共静态无效主要(字符串[]参数){
LinkedHashSet c = new LinkedHashSet();
MyItem 八 = new MyItem("Eight");
c.add(new MyItem("五")); c.add(new MyItem("One")); c.add(八); c.add(new MyItem("三"));
c.add(new MyItem("Four")); c.add(new MyItem("One")); c.add(八); c.add(new MyItem("九"));
c.删除(3);c.add(new MyItem("七"));
System.out.println(c);//输出?
}
}
类 MyArrayList{
公共静态无效主要(字符串[]参数){
ArrayList c = new ArrayList();
MyItem 八 = new MyItem("Eight");
c.add(new MyItem("五")); c.add(new MyItem("One")); c.add(八); c.add(new MyItem("三"));
c.add(new MyItem("Four")); c.add(new MyItem("One")); c.add(八); c.add(new MyItem("九"));
c.删除(3);c.add(new MyItem("七"));
System.out.println(c);//输出?
}
}
类 MyVector{
公共静态无效主要(字符串[]参数){
向量 c = 新向量();
MyItem 八 = new MyItem("Eight");
c.add(new MyItem("五")); c.add(new MyItem("One")); c.add(八); c.add(new MyItem("三"));
c.add(new MyItem("Four")); c.add(new MyItem("One")); c.add(八); c.add(new MyItem("九"));
c.删除(3);c.add(new MyItem("七"));
System.out.println(c);//输出?
}
}
类 MyLinkedList{
公共静态无效主要(字符串[]参数){
链表 c = 新链表();
MyItem 八 = new MyItem("Eight");
c.add(new MyItem("五")); c.add(new MyItem("One")); c.add(八); c.add(new MyItem("三"));
c.add(new MyItem("Four")); c.add(new MyItem("One")); c.add(八); c.add(new MyItem("九"));
c.删除(3);c.add(new MyItem("七"));
System.out.println(c);//输出?
}
}
类 MyPriorityQueue{
公共静态无效主要(字符串[]参数){
PriorityQueue c = new PriorityQueue();
MyItem 八 = new MyItem("Eight");
c.offer(new MyItem("五")); c.offer(new MyItem("One")); c.offer(八); c.offer(new MyItem("三"));
c.offer(new MyItem("Four")); c.offer(new MyItem("One")); c.offer(八); c.offer(new MyItem("Nine"));
System.out.println(c.peek());
System.out.println(c.poll());
c.offer(new MyItem("Seven"));
System.out.println(c);//输出?
}
}