0

Well, you don't mind I will write my "test" project; First of all I had created class that implements AbstractMap.

public class TestClass <K, V> extends AbstractMap <K, V> 

TestClass has the private LinkedList that take as a parameter (It is another class that implements Map.Entry:

private int size = 1000;
private LinkedList <InfoClass <K, V>> [] array = new LinkedList [size];

After, I had created the method that checks and replaces duplicates:

public V put (K key, V value){ // Void doesn't work, therefore we need to return any value;
    V temp = null;
    boolean found = false;
    int index = Math.abs(key.hashCode()) % size;

    if (array[index] == null)
        array[index] = new LinkedList <InfoClass <K, V>> (); // If the specified member of array is null, create new member;

    LinkedList <InfoClass <K, V>> list = array[index];
    InfoClass <K, V> info = new InfoClass <K, V> (key, value);
    ListIterator <InfoClass <K, V>> it = list.listIterator();

    while (it.hasNext()){
        InfoClass<K, V> temp2 = it.next(); // Create a temp instance;
        if (temp2.getKey().equals(key)){ // If there is a duplicate of value, just replace by new one;
            found = true;
            temp = temp2.getValue();
            it.set(info);
            break;
        }
    }

    if (!found) // if there is not a duplicate, add new one;
        array[index].add(info);
    return temp;
}

Next method returns a value, otherwise this returns null if a member of array doesn't exist:

public V get (Object key){ // The parameter K doesn't work;
    int index = Math.abs(key.hashCode()) % size;
    if (array[index] == null)
        return null;
    else
        for (InfoClass <K, V> info : array[index])
            if (info.getKey().equals(key))
                return info.getValue();
    return null;
}

This method forms a set of AbstractMap:

public Set<java.util.Map.Entry<K, V>> entrySet() {
    Set <Map.Entry<K, V>> sets = new HashSet <Map.Entry<K, V>> ();

    for (LinkedList <InfoClass<K, V>> temp1 : array){
        if (temp1 == null)
            continue;
        else{
            for (InfoClass<K,V> temp2 : temp1)
                sets.add(temp2);
        }
    }

    return sets;
}

Ok, create new object in main method:

public static void main (String [] args){
    TestClass <Integer, String> TC = new TestClass <Integer, String> ();
    TC.putAll(CollectionDataMap.newCollection(new Group.Ints(), new Group.Name(), 10));
    System.out.println(TC);
    System.out.println(TC.get(1));
    TC.put(1, "Hello this world");
    System.out.println(TC);
}

Hope I explained correctly. I have a question, what is difference between LinkedList and LinkedHashMap (HashMap) if they work the same way? Thank you very much!

Gary
  • 4,426
  • 1
  • 22
  • 19
  • 1
    A `LinkedHashMap` is basically a `HashMap` with a doubly linked list running through all entries. It is sort of a hybrid between a list and a map. You should consult the JavaDoc before posting such a broad question. – Tim Biegeleisen Jun 18 '17 at 11:26
  • Did you see https://stackoverflow.com/questions/20217414/what-is-the-main-difference-between-hashset-treeset-and-linkedhashset-hashmap?rq=1? –  Jun 18 '17 at 11:28

1 Answers1

6

LinkedList can contain the same element multiple times if the same element is added multiple times.

HashSet can only contain the same object once even if you add it multiple times, but it does not retain insertion order in the set.

LinkedHashSet can only contain the same object once even if you add it multiple times, but it also retains insertion order.

HashMap maps a value to a key, and the keys are stored in a set (so it can be in the set only once). HashMap doesn't preserve insertion order for the key set, while LinkedHashMap does retain insertion order for the keys.

EpicPandaForce
  • 79,669
  • 27
  • 256
  • 428