📜  如何在Java中通过索引从 LinkedHashMap 中获取值?

📅  最后修改于: 2022-05-13 01:54:38.492000             🧑  作者: Mango

如何在Java中通过索引从 LinkedHashMap 中获取值?

LinkedHashMap 是Java中的一个预定义类,它类似于HashMap,不同于HashMap 包含key 及其各自的值,在LinkedHashMap 中保留了插入顺序。任务是通过它们的索引从 LinkedHashMap 中获取值,换句话说,它们的插入顺序。作为 LinkedHashMap 的一个优势,我们知道它们的插入顺序是保留的,它们的顺序将与插入的顺序相同。

例子 :

Input : Key - 2 : Value - 5
    Key - 4 : Value - 3
    Key - 1 : Value - 10
    Key - 3 : Value - 12
    Key - 5 : Value - 6

Input Index ( assuming index from 1-N ) :

Index - 2

Output :  3 ( Value 3 is at Index 2 )

算法 :

1. Check whether the index in LinkedHashMap does exist or not. 
By using size of LinkedHashMap.

2. If exists a print, the value present there.

    else print " index does not exist ".

方法一(使用keys数组):

您可以使用 Keyset 方法将 LinkedHashMap 的所有键转换为集合,然后使用 toArray 方法将集合转换为数组,现在使用数组索引访问键并从 LinkedHashMap 获取值。

句法:

Object[] toArray()

参数:该方法不带任何参数。

返回值:该方法返回一个包含与 Set 类似的元素的数组。

例子

Java
// Java program to get a value from LinkedHashMap by index
// Using Array
import java.util.*;
import java.io.*;
  
public class GFG {
  
    public static void main(String[] args)
    {
  
        // create linked hash map instance
        LinkedHashMap lhm
            = new LinkedHashMap();
  
        // Add mappings
        lhm.put(2, 5);
        lhm.put(4, 3);
        lhm.put(1, 10);
        lhm.put(3, 12);
        lhm.put(5, 6);
  
        // get the key set
        Set keySet = lhm.keySet();
  
        Integer[] keyArray
            = keySet.toArray(new Integer[keySet.size()]);
  
        // taking input of index
        Integer index = 2;
        Integer key = keyArray[index - 1];
  
        // get value from the LinkedHashMap for the key
        System.out.println("Value at index " + index
                           + " is : " + lhm.get(key));
    }
}


Java
// Java program to get a value from LinkedHashMap by index
// Using ArrayList
  
import java.util.*;
import java.io.*;
  
public class GFG {
  
    public static void main(String[] args)
    {
  
        // create an instance of linked hash mao
        LinkedHashMap lhm
            = new LinkedHashMap();
  
        // Add mappings
        lhm.put(2, 5);
        lhm.put(4, 3);
        lhm.put(1, 10);
        lhm.put(3, 12);
        lhm.put(5, 6);
  
        // get the key set
        Set keySet = lhm.keySet();
  
        // Integer[] keyArray = keySet.toArray(new
        // Integer[keySet.size()]); replacing array with
        // ArrayList here.
        List listKeys
            = new ArrayList(keySet);
  
        Integer index = 2; // taking input of index
        Integer key = listKeys.get(index - 1);
  
        // get value from the LinkedHashMap for the key
        System.out.println("Value at index " + index
                           + " is : " + lhm.get(key));
    }
}


Java
// Java program to get a value from LinkedHashMap by index
// Using iterator
  
import java.util.*;
import java.io.*;
  
class GFG {
    public static void main(String[] args)
    {
  
        // create an instance of linked hashmap
        LinkedHashMap lhm
            = new LinkedHashMap();
  
        // Add mappings
        lhm.put(2, 5);
        lhm.put(4, 3);
        lhm.put(1, 10);
        lhm.put(3, 12);
        lhm.put(5, 6);
  
        // get all entries from the LinkedHashMap
        Set > entrySet
            = lhm.entrySet();
  
        // create an iterator
        Iterator > iterator
            = entrySet.iterator();
  
        int i = 0;
        int index = 1;
        int value = 0;
  
        while (iterator.hasNext()) {
  
            if (index - 1 == i) {
                value = iterator.next()
                            .getValue(); // index is found
                                         // get value
                break; // at that index and break
            }
  
            iterator.next();
            i++;
        }
        // print value
        System.out.println("Value at index " + index + " : "
                           + value);
    }
}


输出
Value at index 2 is : 3

方法二(使用列表):

该方法与第一种方法类似,可以将 Keys 转换为 Arraylist 或 LinkedList,而不是转换为数组。

例子

Java

// Java program to get a value from LinkedHashMap by index
// Using ArrayList
  
import java.util.*;
import java.io.*;
  
public class GFG {
  
    public static void main(String[] args)
    {
  
        // create an instance of linked hash mao
        LinkedHashMap lhm
            = new LinkedHashMap();
  
        // Add mappings
        lhm.put(2, 5);
        lhm.put(4, 3);
        lhm.put(1, 10);
        lhm.put(3, 12);
        lhm.put(5, 6);
  
        // get the key set
        Set keySet = lhm.keySet();
  
        // Integer[] keyArray = keySet.toArray(new
        // Integer[keySet.size()]); replacing array with
        // ArrayList here.
        List listKeys
            = new ArrayList(keySet);
  
        Integer index = 2; // taking input of index
        Integer key = listKeys.get(index - 1);
  
        // get value from the LinkedHashMap for the key
        System.out.println("Value at index " + index
                           + " is : " + lhm.get(key));
    }
}
输出
Value at index 2 is : 3

方法三(使用迭代器):

我们可以使用 entrySet() 方法获取 LinkedHashMap 的所有条目,并使用 For-each 循环遍历它们,获取计数,直到它等于索引,中断并打印该值。

例子

Java

// Java program to get a value from LinkedHashMap by index
// Using iterator
  
import java.util.*;
import java.io.*;
  
class GFG {
    public static void main(String[] args)
    {
  
        // create an instance of linked hashmap
        LinkedHashMap lhm
            = new LinkedHashMap();
  
        // Add mappings
        lhm.put(2, 5);
        lhm.put(4, 3);
        lhm.put(1, 10);
        lhm.put(3, 12);
        lhm.put(5, 6);
  
        // get all entries from the LinkedHashMap
        Set > entrySet
            = lhm.entrySet();
  
        // create an iterator
        Iterator > iterator
            = entrySet.iterator();
  
        int i = 0;
        int index = 1;
        int value = 0;
  
        while (iterator.hasNext()) {
  
            if (index - 1 == i) {
                value = iterator.next()
                            .getValue(); // index is found
                                         // get value
                break; // at that index and break
            }
  
            iterator.next();
            i++;
        }
        // print value
        System.out.println("Value at index " + index + " : "
                           + value);
    }
}
输出
Value at index 1 : 5

时间复杂度: O(n)

注意:方法一和方法二不推荐使用,因为它们需要分配一个新的数组或ArrayList来执行这个任务,需要更多的空间,而使用迭代器方法(直接方法),只需要迭代。