Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6215039
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T06:59:00+00:00 2026-05-24T06:59:00+00:00

I’ve been studying for the SCJP, now Oracle Certified Professional Java SE Programmer exam.

  • 0

I’ve been studying for the SCJP, now Oracle Certified Professional Java SE Programmer exam.

I’ve had a difficult time wrapping my head around all the different collections and when to use them. I’m also fond of flash cards. So I tried to create a set of classes that are essentially the same except which collection they’re using. I’ll have to identify how the output would come out and what the primary “features” are of each collection.

Unfortunately I don’t trust myself. I would like to have someone confirm that all the information is accurate or if any is missing. Then after some feedback/corrections I think it’ll make a great exercise for anyone else trying to understand Java collections.

The collections covered are:
HashMap, Hashtable, TreeMap, LinkedHashMap, HashSet, TreeSet, LinkedHashSet, ArrayList, Vector, LinkedList, PriorityQueue.

I also have all the files separated out, they can be downloaded here: http://www.allgo.com/personal/MyCollections.zip

Thanks in advance

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 pair\n##\n");
        MyHashtable.main(args);         System.out.println("Hashtable: Thread Safe. Hash=Unsorted, Unordered. Map=key/value pair\n##\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/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=Maintains Insertion Order. Hash=Unsorted. Set=Unique. Define=equals/hashCode\n##\n");
        MyArrayList.main(args);         System.out.println("ArrayList: List=Queue. Maintains insertion order, Allowed duplicates\n##\n");
        MyVector.main(args);            System.out.println("Vector: Thread Safe. ArrayList. Maintains Insertion Order, Allows duplicates\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/comparator\n##\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?
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T06:59:02+00:00Added an answer on May 24, 2026 at 6:59 am

    For starters, you should refactor your code. Basically, everywhere you used “copy paste”, don’t.

    Create a method like this:

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

    Then instead of the method you have, do this:

    class MyVector{
        public static void main(String[] args){
            Vector c = new Vector();
            fill(c);
        }
    }
    

    And do that for all the collections you have.

    Next, do a similar thing for your maps:

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

    Your code will shrink, be readable and may even one day become usable.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
this is what i have right now Drawing an RSS feed into the php,
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm looking for suggestions for debugging... If you view this site in Firefox or
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.