【问题标题】:Can I have someone verify my collections for the SCJP Exam我可以请人验证我的 SCJP 考试收藏吗
【发布时间】:2011-10-17 22:42:58
【问题描述】:

我一直在学习 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);//输出? } }

【问题讨论】:

    标签: java collections scjp


    【解决方案1】:

    对于初学者,您应该 refactor 您的代码。基本上,在你使用“复制粘贴”的任何地方,都不要。

    创建一个这样的方法:

    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?
    }
    

    您的代码会缩小,变得可读,甚至有一天会变得可用。

    【讨论】:

    • 谢谢,我同意重构是至关重要的。但是在这种情况下,这是非常刻意的。因为每个类都打算单独显示在闪存卡上,而不会泄露任何关于该集合的作用的线索。这有点棘手,因为它们确实非常不同。
    猜你喜欢
    • 1970-01-01
    • 2011-08-13
    • 2014-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多