Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java LinkedList、HashSet和HashMap之间的主要区别是什么?_Java - Fatal编程技术网

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

Java LinkedList、HashSet和HashMap之间的主要区别是什么?,java,Java,好吧,你不介意我会写我的“测试”项目; 首先,我创建了一个实现AbstractMap的类 public class TestClass <K, V> extends AbstractMap <K, V> 公共类TestClass扩展了AbstractMap TestClass具有作为参数的私有LinkedList(它是实现Map.Entry的另一个类): private int size = 1000; private LinkedList <InfoClas

好吧,你不介意我会写我的“测试”项目; 首先,我创建了一个实现AbstractMap的类

public class TestClass <K, V> extends AbstractMap <K, V> 
公共类TestClass扩展了AbstractMap
TestClass具有作为参数的私有LinkedList(它是实现Map.Entry的另一个类):

private int size = 1000;
private LinkedList <InfoClass <K, V>> [] array = new LinkedList [size];
private int size=1000;
私有LinkedList[]数组=新LinkedList[size];
之后,我创建了检查和替换重复项的方法:

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;
}
public V put(K键,V值){//Void不起作用,因此我们需要返回任何值;
V温度=零;
布尔值=false;
int index=Math.abs(key.hashCode())%size;
if(数组[索引]==null)
array[index]=new LinkedList();//如果指定的数组成员为null,则创建新成员;
LinkedList=数组[索引];
信息类信息=新信息类(键、值);
ListIterator it=list.ListIterator();
while(it.hasNext()){
InfoClass temp2=it.next();//创建一个临时实例;
如果(temp2.getKey().equals(key)){//如果有重复的值,只需替换为新值即可;
发现=真;
temp=temp2.getValue();
it.set(info);
打破
}
}
如果(!found)//如果没有重复项,则添加新项;
数组[索引]。添加(信息);
返回温度;
}
下一个方法返回一个值,否则,如果数组的成员不存在,则返回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;
}
public V get(Object key){//参数K不起作用;
int index=Math.abs(key.hashCode())%size;
if(数组[索引]==null)
返回null;
其他的
for(信息类信息:数组[索引])
if(info.getKey().equals(key))
return info.getValue();
返回null;
}
此方法形成一组抽象映射:

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;
}
publicset入口集(){
Set Set=newhashset();
for(LinkedList temp1:array){
if(temp1==null)
继续;
否则{
for(信息类temp2:temp1)
集合。添加(temp2);
}
}
返回集;
}
确定,在主方法中创建新对象:

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);
}
publicstaticvoidmain(字符串[]args){
TestClass TC=新的TestClass();
TC.putAll(CollectionDataMap.newCollection(newgroup.Ints(),newgroup.Name(),10));
系统输出打印项次(TC);
System.out.println(TC.get(1));
TC.put(1,“你好,这个世界”);
系统输出打印项次(TC);
}
希望我的解释正确。我有一个问题,如果LinkedList和LinkedHashMap(HashMap)的工作方式相同,它们之间有什么区别?
非常感谢!如果多次添加同一元素,则LinkedList可以多次包含同一元素

HashSet只能包含同一个对象一次,即使您多次添加它,但它在集合中不保留插入顺序

LinkedHashSet只能包含同一对象一次,即使您多次添加它,但它也保留插入顺序


HashMap将一个值映射到一个键,键存储在一个集合中(因此只能在集合中存储一次)。HashMap不保留键集的插入顺序,而LinkedHashMap保留键的插入顺序。

如果多次添加同一元素,LinkedList可以多次包含同一元素

HashSet只能包含同一个对象一次,即使您多次添加它,但它在集合中不保留插入顺序

LinkedHashSet只能包含同一对象一次,即使您多次添加它,但它也保留插入顺序


HashMap将一个值映射到一个键,键存储在一个集合中(因此它只能在集合中存在一次).HashMap不保留密钥集的插入顺序,而LinkedHashMap保留密钥的插入顺序。

A
LinkedHashMap
基本上是一个
HashMap
,所有条目都有一个双链接列表。它有点像是列表和映射的混合体。发布这样的链接之前,您应该查阅JavaDoc广泛的问题。你看到了吗?一个
LinkedHashMap
基本上是一个
HashMap
,所有条目都有一个双链接列表。它是列表和地图的混合体。在发布这样一个广泛的问题之前,你应该查阅JavaDoc。你看到了吗?