📜  如何在Java中使用索引获取 TreeMap 键或值?

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

如何在Java中使用索引获取 TreeMap 键或值?

Java中的 TreeMap 用于实现 地图界面和NavigableMap以及 抽象地图类。 TreeMap 根据其键的自然顺序进行排序。 TreeMap 类是 Map 接口的红黑树实现,因此不公开任何我们可以使用它们的索引访问 TreeMap 键或值的方法。

Java中使用索引获取key或TreeMap值的简单方法有以下三种:

  1. 使用数组
  2. 使用列表
  3. 使用迭代

方法 1:使用数组

我们可以通过使用 Array 使用Java中的索引获取 TreeMap 键或 TreeMap 值。该过程分为三个步骤:

  1. 使用 TreeMap 类的entrySet()方法获取存储在 TreeMap 对象中的所有条目的 Set 视图。
  2. 使用toArray()方法将条目集转换为数组。
  3. 并在getKey()getValue()方法的帮助下使用索引获取 TreeMap 键或 TreeMap 值

句法:

Set> entrySet = treeMap.entrySet();

例子:

Java
// Java Program to get TreeMap key or TreeMap value
// using index
 
// Importing all classes of
// java.util package
import java.util.*;
 
// Class
public class GFG {
 
    // MAin driver method
    public static void main(String[] args)
    {
 
        // Creating a New TreeMap
        TreeMap treeMap
            = new TreeMap();
 
        // Add elements to TreeMap
        // Custom inputs
        treeMap.put(1, "Welcome");
        treeMap.put(2, "geeks");
        treeMap.put(3, "on");
        treeMap.put(4, "geeksforgeeks");
 
        // Get entry set of the TreeMap using entrySet
        // method
        Set > entrySet
            = treeMap.entrySet();
 
        // Convert entrySet to Array using toArray method
        Map.Entry[] entryArray
            = entrySet.toArray(
                new Map.Entry[entrySet.size()]);
 
        // For loop for iteration  and printing
        for (int i = 0; i < 4; i++)
        {
            // Get Key using index and print
            System.out.println("Key at " + i + ":"
                               + entryArray[i].getKey());
 
            // Get value using index and print
            System.out.println("Value at " + i + ":"
                               + entryArray[i].getValue());
        }
         
    }
}


Java
// Java Program to get TreeMap key or TreeMap value
// using index
 
// Importing all classes of
// java.util package
import java.util.*;
 
// Class
public class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
 
        // Creating a TreeMap
        TreeMap treeMap = new TreeMap<>();
 
        // Add elements to TreeMap
        // Custom inputs
        treeMap.put(1, "Welcome");
        treeMap.put(2, "geeks");
        treeMap.put(3, "on");
        treeMap.put(4, "geeksforgeeks");
 
        // Get entry set of the TreeMap
        // using entrySet method
        Set > entrySet
            = treeMap.entrySet();
 
        // Converting entrySet to ArrayList
        List > entryList
            = new ArrayList<>(entrySet);
 
        // For each loop for iteration
        for (int i = 0; i < 4; i++) {
 
            // Print Key and Values using index
 
            // Get Key using index
            System.out.println("Key at " + i + ":"
                               + entryList.get(i).getKey());
 
            // Get value using index
            System.out.println(
                "Value at " + i + ":"
                + entryList.get(i).getValue());
        }
    }
}


Java
// Java Program to get TreeMap key or TreeMap value using
// index
 
// Importing all classes of
// java.util package
import java.util.*;
 
// Class
public class GFG {
 
    // MAin driver method
    public static void main(String[] args)
    {
        // Creating a TreeMp
        TreeMap treeMap = new TreeMap<>();
 
        // Add elements to TreeMap
        // Custom inputs
        treeMap.put(1, "Welcome");
        treeMap.put(2, "geeks");
        treeMap.put(3, "on");
        treeMap.put(4, "geeksforgeeks");
 
        // Get entry set of the TreeMap
        // using entrySet method
        Set > entrySet
            = treeMap.entrySet();
 
        int index = 0;
 
        // For-each loop for iteration
        for (Map.Entry currentEntry :
             entrySet) {
 
            // Print Key and Values using index
 
            // Get Key using index
            System.out.println("Key at " + index + ":"
                               + currentEntry.getKey());
 
            // Get value using index
            System.out.println("Value at " + index + ":"
                               + currentEntry.getValue());
            index++;
        }
    }
}


输出:

1
Welcome
2
geeks
3
on
4
geeksforgeeks

方法 2:使用列表

我们可以通过使用列表而不是数组来使用Java中的索引获取 TreeMap 键或 TreeMap 值。该过程分为三个步骤:

  1. 使用 TreeMap 类的 entrySet() 方法获取存储在 TreeMap 对象中的所有条目的集合视图。
  2. 现在,使用 toArray() 方法将条目集转换为数组。
  3. 最后,借助 get()、getKey() 和 getValue() 方法使用索引获取 TreeMap 键或 TreeMap 值。

句法:

Set> entrySet = treeMap.entrySet();

例子:

Java

// Java Program to get TreeMap key or TreeMap value
// using index
 
// Importing all classes of
// java.util package
import java.util.*;
 
// Class
public class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
 
        // Creating a TreeMap
        TreeMap treeMap = new TreeMap<>();
 
        // Add elements to TreeMap
        // Custom inputs
        treeMap.put(1, "Welcome");
        treeMap.put(2, "geeks");
        treeMap.put(3, "on");
        treeMap.put(4, "geeksforgeeks");
 
        // Get entry set of the TreeMap
        // using entrySet method
        Set > entrySet
            = treeMap.entrySet();
 
        // Converting entrySet to ArrayList
        List > entryList
            = new ArrayList<>(entrySet);
 
        // For each loop for iteration
        for (int i = 0; i < 4; i++) {
 
            // Print Key and Values using index
 
            // Get Key using index
            System.out.println("Key at " + i + ":"
                               + entryList.get(i).getKey());
 
            // Get value using index
            System.out.println(
                "Value at " + i + ":"
                + entryList.get(i).getValue());
        }
    }
}


输出
Key at 0:1
Value at 0:Welcome
Key at 1:2
Value at 1:geeks
Key at 2:3
Value at 2:on
Key at 3:4
Value at 3:geeksforgeeks

方法三:使用 迭代

我们可以通过使用迭代在Java中使用索引获取 TreeMap 键或 TreeMap 值。该过程分为两个步骤:

  1. 使用 TreeMap 类的 entrySet() 方法获取存储在 TreeMap 对象中的所有条目的 Set 视图。
  2. 在 getKey() 和 getValue() 方法的帮助下,使用索引在 entrySet 上迭代以获取 TreeMap 键或 TreeMap 值。

句法:

Set> entrySet = treeMap.entrySet();

例子:

Java

// Java Program to get TreeMap key or TreeMap value using
// index
 
// Importing all classes of
// java.util package
import java.util.*;
 
// Class
public class GFG {
 
    // MAin driver method
    public static void main(String[] args)
    {
        // Creating a TreeMp
        TreeMap treeMap = new TreeMap<>();
 
        // Add elements to TreeMap
        // Custom inputs
        treeMap.put(1, "Welcome");
        treeMap.put(2, "geeks");
        treeMap.put(3, "on");
        treeMap.put(4, "geeksforgeeks");
 
        // Get entry set of the TreeMap
        // using entrySet method
        Set > entrySet
            = treeMap.entrySet();
 
        int index = 0;
 
        // For-each loop for iteration
        for (Map.Entry currentEntry :
             entrySet) {
 
            // Print Key and Values using index
 
            // Get Key using index
            System.out.println("Key at " + index + ":"
                               + currentEntry.getKey());
 
            // Get value using index
            System.out.println("Value at " + index + ":"
                               + currentEntry.getValue());
            index++;
        }
    }
}
输出
Key at 0:1
Value at 0:Welcome
Key at 1:2
Value at 1:geeks
Key at 2:3
Value at 2:on
Key at 3:4
Value at 3:geeksforgeeks