我可以让某人验证我的作品集以进行SCJP考试吗?
我一直在学习SCJP,现在是Oracle认证专业Java SE程序员考试。
我有一段困难的时光,围绕着所有不同的收藏品以及何时使用它们。 我也喜欢闪存卡。 所以我尝试创建一组基本相同的类,除了它们使用的是哪个集合。 我将不得不确定输出的结果以及每个集合的主要“特征”。
不幸的是我不相信自己。 我希望有人确认所有信息都是准确的或者是否有遗漏。 然后,经过一些反馈/更正之后,我认为对于试图理解Java集合的其他人来说,这将是一个很好的练习。
覆盖的集合有:HashMap,Hashtable,TreeMap,LinkedHashMap,HashSet,TreeSet,LinkedHashSet,ArrayList,Vector,LinkedList,PriorityQueue。
我也将所有文件分离出来,他们可以在这里下载:http://www.allgo.com/personal/MyCollections.zip
提前致谢
import java.util.*; import java.lang.*; class MyItem implements Comparable{ private String name; MyItem(String n){ name = n; } public String toString(){return name;} public String getName(){return name;} public boolean equals(Object obj){ if(this==obj) return true; else if(obj==null) return false; else if(getName() != ((MyItem)obj).getName()) return false; else return true; } public int hashCode(){ return 5; } public int compareTo(MyItem b){return b.getName().compareTo(getName());} } public class MyCollections{ public static void main(String[] args){ MyHashMap.main(args); System.out.println("HashMap: Hash=Unsorted, Unordered. Map=key/value pairn##n"); MyHashtable.main(args); System.out.println("Hashtable: Thread Safe. Hash=Unsorted, Unordered. Map=key/value pairn##n"); MyTreeMap.main(args); System.out.println("TreeMap: Tree=sorted. Map=key/value.n##n"); MyLinkedHashMap.main(args); System.out.println("LinkedHashMap: Linked=Maintains Insertion Order. Hash=unsorted, unordered. Map=key/value pair.n##n"); MyHashSet.main(args); System.out.println("HashSet: Hash=Unsorted, Unordered. Set=Unique. Define=equals/hashCoden##n"); MyTreeSet.main(args); System.out.println("TreeSet: Tree=Sorted. Set=Unique. Define=Comparable/Comparatorn##n"); MyLinkedHashSet.main(args); System.out.println("LinkedHashSet: Liniked=Maintains Insertion Order. Hash=Unsorted. Set=Unique. Define=equals/hashCoden##n"); MyArrayList.main(args); System.out.println("ArrayList: List=Queue. Maintains insertion order, Allowed duplicatesn##n"); MyVector.main(args); System.out.println("Vector: Thread Safe. ArrayList. Maintains Insertion Order, Allows duplicatesn##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/comparatorn##n"); } } class MyHashMap{ public static void main(String[] args){ HashMap c = new HashMap(); MyItem Eight = new MyItem("Eight"); c.put(5, new MyItem("Five")); c.put(1, new MyItem("One")); c.put(8, Eight); c.put(3, new MyItem("Three")); c.put(4, new MyItem("Four")); c.put(1, new MyItem("1")); c.put(8, Eight); c.put(9, new MyItem("Nine")); c.remove(3); c.put(7, new MyItem("Seven")); System.out.println(c);//output? } } class MyHashtable{ public static void main(String[] args){ Hashtable c = new Hashtable(); MyItem Eight = new MyItem("Eight"); c.put(5, new MyItem("Five")); c.put(1, new MyItem("One")); c.put(8, Eight); c.put(3, new MyItem("Three")); c.put(4, new MyItem("Four")); c.put(1, new MyItem("1")); c.put(8, Eight); c.put(9, new MyItem("Nine")); c.remove(3); c.put(7, new MyItem("Seven")); System.out.println(c);//output? } } class MyTreeMap{ public static void main(String[] args){ TreeMap c = new TreeMap(); MyItem Eight = new MyItem("Eight"); c.put(5, new MyItem("Five")); c.put(1, new MyItem("One")); c.put(8, Eight); c.put(3, new MyItem("Three")); c.put(4, new MyItem("Four")); c.put(1, new MyItem("1")); c.put(8, Eight); c.put(9, new MyItem("Nine")); c.remove(3); c.put(7, new MyItem("Seven")); System.out.println(c);//output? } } class MyLinkedHashMap{ public static void main(String[] args){ LinkedHashMap c = new LinkedHashMap(); MyItem Eight = new MyItem("Eight"); c.put(5, new MyItem("Five")); c.put(1, new MyItem("One")); c.put(8, Eight); c.put(3, new MyItem("Three")); c.put(4, new MyItem("Four")); c.put(1, new MyItem("1")); c.put(8, Eight); c.put(9, new MyItem("Nine")); c.remove(3); c.put(7, new MyItem("Seven")); System.out.println(c);//output? } } class MyHashSet{ public static void main(String[] args){ HashSet c = new HashSet(); MyItem Eight = new MyItem("Eight"); c.add(new MyItem("Five")); c.add(new MyItem("One")); c.add(Eight); c.add(new MyItem("Three")); c.add(new MyItem("Four")); c.add(new MyItem("One")); c.add(Eight); c.add(new MyItem("Nine")); c.remove(3); c.add(new MyItem("Seven")); System.out.println(c);//output? } } class MyTreeSet{ public static void main(String[] args){ TreeSet c = new TreeSet(); MyItem Eight = new MyItem("Eight"); c.add(new MyItem("Five")); c.add(new MyItem("One")); c.add(Eight); c.add(new MyItem("Three")); c.add(new MyItem("Four")); c.add(new MyItem("One")); c.add(Eight); c.add(new MyItem("Nine")); c.remove(Eight); c.add(new MyItem("Seven")); System.out.println(c);//output? } } class MyLinkedHashSet{ public static void main(String[] args){ LinkedHashSet c = new LinkedHashSet(); MyItem Eight = new MyItem("Eight"); c.add(new MyItem("Five")); c.add(new MyItem("One")); c.add(Eight); c.add(new MyItem("Three")); c.add(new MyItem("Four")); c.add(new MyItem("One")); c.add(Eight); c.add(new MyItem("Nine")); c.remove(3); c.add(new MyItem("Seven")); System.out.println(c);//output? } } class MyArrayList{ public static void main(String[] args){ ArrayList c = new ArrayList(); MyItem Eight = new MyItem("Eight"); c.add(new MyItem("Five")); c.add(new MyItem("One")); c.add(Eight); c.add(new MyItem("Three")); c.add(new MyItem("Four")); c.add(new MyItem("One")); c.add(Eight); c.add(new MyItem("Nine")); c.remove(3); c.add(new MyItem("Seven")); System.out.println(c);//output? } } class MyVector{ public static void main(String[] args){ Vector c = new Vector(); MyItem Eight = new MyItem("Eight"); c.add(new MyItem("Five")); c.add(new MyItem("One")); c.add(Eight); c.add(new MyItem("Three")); c.add(new MyItem("Four")); c.add(new MyItem("One")); c.add(Eight); c.add(new MyItem("Nine")); c.remove(3); c.add(new MyItem("Seven")); System.out.println(c);//output? } } class MyLinkedList{ public static void main(String[] args){ LinkedList c = new LinkedList(); MyItem Eight = new MyItem("Eight"); c.add(new MyItem("Five")); c.add(new MyItem("One")); c.add(Eight); c.add(new MyItem("Three")); c.add(new MyItem("Four")); c.add(new MyItem("One")); c.add(Eight); c.add(new MyItem("Nine")); c.remove(3); c.add(new MyItem("Seven")); System.out.println(c);//output? } } class MyPriorityQueue{ public static void main(String[] args){ PriorityQueue c = new PriorityQueue(); MyItem Eight = new MyItem("Eight"); c.offer(new MyItem("Five")); c.offer(new MyItem("One")); c.offer(Eight); c.offer(new MyItem("Three")); c.offer(new MyItem("Four")); c.offer(new MyItem("One")); c.offer(Eight); c.offer(new MyItem("Nine")); System.out.println(c.peek()); System.out.println(c.poll()); c.offer(new MyItem("Seven")); System.out.println(c);//output? } }
对于初学者,你应该重构你的代码。 基本上,无论你用什么“复制粘贴”,都不要。
创建一个像这样的方法:
private static void fill(Collection c) {
MyItem Eight = new MyItem("Eight");
c.add(new MyItem("Five")); c.add(new MyItem("One")); c.add(Eight); c.add(new MyItem("Three"));
c.add(new MyItem("Four")); c.add(new MyItem("One")); c.add(Eight); c.add(new MyItem("Nine"));
c.remove(3); c.add(new MyItem("Seven"));
System.out.println(c);//output?
}
然后,而不是你有的方法,做到这一点:
class MyVector{
public static void main(String[] args){
Vector c = new Vector();
fill(c);
}
}
并为所有的收藏品做到这一点。
接下来,为你的地图做一个类似的事情:
private static void fill(Map<?,?> map) {
MyItem Eight = new MyItem("Eight");
map.put(5, new MyItem("Five")); map.put(1, new MyItem("One")); map.put(8, Eight); map.put(3, new MyItem("Three"));
map.put(4, new MyItem("Four")); map.put(1, new MyItem("1")); map.put(8, Eight); map.put(9, new MyItem("Nine"));
map.remove(3); map.put(7, new MyItem("Seven"));
System.out.println(map);//output?
}
你的代码会缩小,易读,甚至有一天会变得可用。
链接地址: http://www.djcxy.com/p/66567.html上一篇: Can I have someone verify my collections for the SCJP Exam
下一篇: Java Applet