简体   繁体   English

LinkedList、HashSet 和 HashMap 之间的主要区别是什么?

[英]What is the main difference between LinkedList, HashSet and HashMap?

Well, you don't mind I will write my "test" project;好吧,你不介意我写我的“测试”项目; First of all I had created class that implements AbstractMap.首先,我创建了实现 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: TestClass 具有作为参数的私有 LinkedList(它是另一个实现 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: Next 方法返回一个值,否则如果数组成员不存在,则返回 null:

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:这个方法形成了一组 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:好的,在 main 方法中创建新对象:

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?我有一个问题,如果它们以相同的方式工作,LinkedList 和 LinkedHashMap (HashMap) 之间有什么区别? Thank you very much!非常感谢!

LinkedList can contain the same element multiple times if the same element is added multiple times.如果多次添加相同的元素,LinkedList 可以多次包含相同的元素。

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.即使多次添加,HashSet 也只能包含同一个对象一次,但它不保留集合中的插入顺序。

LinkedHashSet can only contain the same object once even if you add it multiple times, but it also retains insertion order.即使多次添加,LinkedHashSet 也只能包含同一个对象一次,但它也保留了插入顺序。

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 将一个值映射到一个键,键存储在一个集合中(所以它只能在集合中一次)。 HashMap doesn't preserve insertion order for the key set, while LinkedHashMap does retain insertion order for the keys. HashMap 不保留键集的插入顺序,而 LinkedHashMap 确实保留键的插入顺序。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 HashSet 和 HashMap 的区别? - Difference between HashSet and HashMap? Hashset,Treeset和LinkedHashset,Hashmap之间的主要区别是什么?它在Java中如何工作? - What is the main difference between Hashset, Treeset and LinkedHashset, Hashmap and how does it work in Java? 对象的LinkedList和HashMap的LinkedList之间的区别? - Difference between LinkedList of Object and a LinkedList of a HashMap? 使用 LinkedList 与 HashMap 实现无向图有什么区别? 遍历 BFS/DFS 哪个更好? - What is the difference between implementing an undirected graph with LinkedList vs HashMap? What is better for traversing BFS/DFS? 在Java中,HashSet有什么区别 <Integer> =新的HashSet(2)和HashSet <Integer> =新的HashSet <Integer> (2)? - In Java, what's the difference between HashSet<Integer> = new HashSet(2) and HashSet<Integer> = new HashSet<Integer>(2)? 各种小HashSet和1个大HashSet之间的搜索区别是什么? - What is the Searching difference between various small HashSet and 1 large HashSet? 将集合类型转换为 HashSet 和使用集合初始化 HashSet 有什么区别? - What is the difference between type casting a set to HashSet and initializing a HashSet with a set? HashSet和Set之间有什么区别? - What's the difference between HashSet and Set? HashSet和LinkedHashSet有什么区别 - what's the difference between HashSet and LinkedHashSet HashMap 有什么区别<Long, String>和 HashMap&lt;&gt;? - What is the difference between HashMap<Long, String> and HashMap<>?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM