📌  相关文章
📜  如何从Java LinkedHashMap 获取第一个或最后一个条目?

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

如何从Java LinkedHashMap 获取第一个或最后一个条目?

LinkedHashMap 是Java中的一个预定义类,类似于HashMap,不同于HashMap,包含key及其各自的值,在LinkedHashMap中保留插入顺序。任务是获取 LinkedHashMap 中存在的第一个和最后一个条目。迭代以获取最后一个和第一个值。 Map 中的第一个和最后一个条目是最先插入的条目和最后插入的条目,其中保留了插入顺序。

方法:

  1. 使用 for-each 循环在 Map 上迭代的幼稚方法。
  2. 将 LinkedHashMap 的键转换为整数数组。
  3. 将 LinkedHashMap 中的键转换为 List,如 ArrayList 到 LinkedList。

插图:

方法 1:使用 for-each 循环对 Map 进行迭代的幼稚方法。

构造函数getFirst()getLast()

  • getFirst()打印第一个条目
  • getLast()移到最后一个条目(索引等于 LinkedHashMap 的大小)

例子:

Java
// Java Program to get first or last entry
// from Java LinkedHashMap
 
// Importing all class of
// java.util package
import java.util.*;
// Importing java input/output classes
import java.io.*;
 
class GFG {
 
  // getLast() method
    public static void
    getLast(LinkedHashMap lhm)
    {
        int count = 1;
 
        for (Map.Entry it :
             lhm.entrySet()) {
           
            if (count == lhm.size()) {
               
                System.out.println("Last Key-> "+it.getKey());
              System.out.println("Last Value-> "+it.getValue());
                return;
            }
            count++;
        }
    }
 
  // getFirst() method to get first element from
  // java LinkedHashMap
    public static void
    getFirst(LinkedHashMap lhm)
    {
        int count = 1;
 
        for (Map.Entry it :
             lhm.entrySet()) {
            if (count == 1) {
              System.out.println("First Key-> "+it.getKey());
              System.out.println("First Value-> "+it.getValue());
                return;
            }
            count++;
        }
    }
 
  // Main driver method
    public static void main(String[] args)
    {
 
      // Creating(defining) a LinkedHashMap
        LinkedHashMap LHM
            = new LinkedHashMap<>();
 
      // Adding elements to above LinkedHashMap
        LHM.put(2, 5);
        LHM.put(14, 35);
        LHM.put(36, 20);
        LHM.put(34, 18);
        LHM.put(52, 6);
       
      // Calling getFirst() method in main()
        getFirst(LHM);
       
      // Calling getLast() method in main()
        getLast(LHM);
    }
}


Java
// Java Program to get first or last entry
// from Java LinkedHashMap
// By converting Map to integer array
 
// Importing all class of
// java.util package
import java.util.*;
// Importing java input/output classes
import java.io.*;
 
class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
        // Creating a LinkedHashMAp
        LinkedHashMap LHM
            = new LinkedHashMap<>();
 
        // Adding. elements to above LinkedHashMap
        // Custom inputs
        LHM.put(1, 8);
        LHM.put(2, 6);
        LHM.put(3, 7);
        LHM.put(4, 2);
        LHM.put(5, 5);
 
        // Getting all keys from the LinkedHashMap, and
        // converting it to an array
        Integer[] aKeys
            = LHM.keySet().toArray(new Integer[LHM.size()]);
 
        // Condition check
        // If array is having element
        // Print key and value
        if (aKeys.length > 0) {
 
            // Print first key and first value
            // From integer array
            System.out.println("First key-> " + aKeys[0]);
            System.out.println("First value-> "
                               + LHM.get(aKeys[0]));
 
            // Print first key from integer array
            System.out.println("Last key-> "
                               + aKeys[aKeys.length - 1]);
 
            // Print last value from integer array
            System.out.println(
                "Last value-> "
                + LHM.get(aKeys[aKeys.length - 1]));
        }
    }
}


Java
// Java Program to get first or last entry
// from Java LinkedHashMap
// By converting Map to List
 
// Importing all class of
// java.util package
import java.util.*;
// Importing java input/output classes
import java.io.*;
 
// Main class
class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
 
        // Creating a LinkedHashMapc
        LinkedHashMap LHM
            = new LinkedHashMap<>();
 
        // Adding elements to above LinkedHashMap
        // Custom inputs
        LHM.put(1, 8);
        LHM.put(2, 6);
        LHM.put(3, 7);
        LHM.put(4, 2);
        LHM.put(5, 5);
 
        // Creating a List
        List lKeys
            = new ArrayList(LHM.keySet());
 
        // Condition check
        // If there is single element in List
        // Print key and value
        if (lKeys.size() > 0) {
 
            // Print first key form List
            System.out.println("First key: "
                               + lKeys.get(0));
 
            // Print first value from List
            System.out.println("First value: "
                               + LHM.get(lKeys.get(0)));
 
            // Print last key from List
            System.out.println(
                "Last key: " + lKeys.get(lKeys.size() - 1));
 
            // Print last value from List
            System.out.println(
                "Last value: "
                + LHM.get(lKeys.get(lKeys.size() - 1)));
        }
    }
}


输出
First Key-> 2
First Value-> 5
Last Key-> 52
Last Value-> 6

时间复杂度:O(n)

方法二:将LinkedHashMap的key转换为整型数组。

算法:

  • 获取第一个和键对应的值。
  • 打印最后一个和键对应的值。
Pseudo Code :
Integer[] aKeys = LHM.keySet().toArray(new Integer[LHM.size()]);
// where LHM is name of LinkedHashMap created and aKeys of array to be converted.

例子:

Java

// Java Program to get first or last entry
// from Java LinkedHashMap
// By converting Map to integer array
 
// Importing all class of
// java.util package
import java.util.*;
// Importing java input/output classes
import java.io.*;
 
class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
        // Creating a LinkedHashMAp
        LinkedHashMap LHM
            = new LinkedHashMap<>();
 
        // Adding. elements to above LinkedHashMap
        // Custom inputs
        LHM.put(1, 8);
        LHM.put(2, 6);
        LHM.put(3, 7);
        LHM.put(4, 2);
        LHM.put(5, 5);
 
        // Getting all keys from the LinkedHashMap, and
        // converting it to an array
        Integer[] aKeys
            = LHM.keySet().toArray(new Integer[LHM.size()]);
 
        // Condition check
        // If array is having element
        // Print key and value
        if (aKeys.length > 0) {
 
            // Print first key and first value
            // From integer array
            System.out.println("First key-> " + aKeys[0]);
            System.out.println("First value-> "
                               + LHM.get(aKeys[0]));
 
            // Print first key from integer array
            System.out.println("Last key-> "
                               + aKeys[aKeys.length - 1]);
 
            // Print last value from integer array
            System.out.println(
                "Last value-> "
                + LHM.get(aKeys[aKeys.length - 1]));
        }
    }
}
输出
First key-> 1
First value-> 8
Last key-> 5
Last value-> 5

时间复杂度:O(1)

方法三:将LinkedHashMap中的key转成List,就像ArrayList转LinkedList一样。

算法

  • 获取first和key对应的值。
  • 打印最后一个和键对应的值。
Pseudo Code:
List lKeys = new ArrayList(LHM.keySet());
// where LHM is name of LinkedHashMap and 
         lKeys is name of List

例子

Java

// Java Program to get first or last entry
// from Java LinkedHashMap
// By converting Map to List
 
// Importing all class of
// java.util package
import java.util.*;
// Importing java input/output classes
import java.io.*;
 
// Main class
class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
 
        // Creating a LinkedHashMapc
        LinkedHashMap LHM
            = new LinkedHashMap<>();
 
        // Adding elements to above LinkedHashMap
        // Custom inputs
        LHM.put(1, 8);
        LHM.put(2, 6);
        LHM.put(3, 7);
        LHM.put(4, 2);
        LHM.put(5, 5);
 
        // Creating a List
        List lKeys
            = new ArrayList(LHM.keySet());
 
        // Condition check
        // If there is single element in List
        // Print key and value
        if (lKeys.size() > 0) {
 
            // Print first key form List
            System.out.println("First key: "
                               + lKeys.get(0));
 
            // Print first value from List
            System.out.println("First value: "
                               + LHM.get(lKeys.get(0)));
 
            // Print last key from List
            System.out.println(
                "Last key: " + lKeys.get(lKeys.size() - 1));
 
            // Print last value from List
            System.out.println(
                "Last value: "
                + LHM.get(lKeys.get(lKeys.size() - 1)));
        }
    }
}
输出
First key: 1
First value: 8
Last key: 5
Last value: 5

时间复杂度:O(1)