如何在Java中迭代 TreeMap?
给定一个 TreeMap,任务是在Java中迭代这个 TreeMap。 Java中的TreeMap用于实现Map接口和NavigableMap以及抽象类。我们不能直接使用迭代器来迭代 TreeMap,因为 TreeMap 不是一个集合。所以我们将不得不使用 TreeMap.entrySet() 方法。此方法返回包含在此树形图中的映射的集合视图(Set
示例 1:
Java
// Java program to iterate over a TreeMap
import java.util.Map;
import java.util.TreeMap;
class IterationDemo {
public static void main(String[] arg)
{
Map gfg
= new TreeMap();
// enter name/url pair
gfg.put("GFG", "geeksforgeeks.org");
gfg.put("Practice", "practice.geeksforgeeks.org");
gfg.put("Code", "code.geeksforgeeks.org");
gfg.put("Quiz", "quiz.geeksforgeeks.org");
// using for-each loop for
// iteration over TreeMap.entrySet()
for (Map.Entry
entry : gfg.entrySet())
System.out.println(
"[" + entry.getKey()
+ ", " + entry.getValue() + "]");
}
}
Java
// Java Program to Iterate Over Entries in a TreeMap
import java.util.*;
// Importing required
// Main class
class GFG {
// Main driver method
public static void main(String[] args)
{
// Creating a TreeMap class object
// Objects are of key-value pairs (integer and
// string type)
TreeMap tm
= new TreeMap();
// Customly adding elements
tm.put(1, "Geeks");
tm.put(2, "For");
tm.put(3, "Geeks");
// Get all entries using the entrySet() method
Set > entries
= tm.entrySet();
// Way 1
// Using for loops
for (Map.Entry entry : entries) {
System.out.println(entry.getKey() + "->"
+ entry.getValue());
}
// New line to differentiate differences in output
// between for loop and for each loop
System.out.println();
// Way 2 - getting code shorter and simpler
// For each loops
entries.forEach(entry -> {
System.out.println(entry.getKey() + "->"
+ entry.getValue());
});
// New line to differentiate differences in output
// between for each loop and iterator traversal
System.out.println();
// Way 3 - New way to
// Getting an iterator
Iterator > iterator
= entries.iterator();
// Additional step here
// To Initialize object holding for
// key-value pairs to null
Map.Entry entry = null;
// Holds true till there is no element remaining in
// the object using hasNExt() method
while (iterator.hasNext()) {
// Moving onto next pairs using next() method
entry = iterator.next();
// Printing the key-value pairs
// using getKet() and getValue() methods
System.out.println(entry.getKey() + "->"
+ entry.getValue());
}
}
}
输出:
[Code, code.geeksforgeeks.org]
[GFG, geeksforgeeks.org]
[Practice, practice.geeksforgeeks.org]
[Quiz, quiz.geeksforgeeks.org]
现在让我们看看遍历 TreeMap 对象中的条目。为了实现,我们正在考虑非常简单的地图元素关联性,其中我们有三个元素,它们分别是“Geeks”、“for”、“Geeks”,并且是键值 '1'、'2' 和 '3'整数类型。所以只有从这里我们才能得到我们需要创建一个 TreeMap 类的对象。
示例 2:
Java
// Java Program to Iterate Over Entries in a TreeMap
import java.util.*;
// Importing required
// Main class
class GFG {
// Main driver method
public static void main(String[] args)
{
// Creating a TreeMap class object
// Objects are of key-value pairs (integer and
// string type)
TreeMap tm
= new TreeMap();
// Customly adding elements
tm.put(1, "Geeks");
tm.put(2, "For");
tm.put(3, "Geeks");
// Get all entries using the entrySet() method
Set > entries
= tm.entrySet();
// Way 1
// Using for loops
for (Map.Entry entry : entries) {
System.out.println(entry.getKey() + "->"
+ entry.getValue());
}
// New line to differentiate differences in output
// between for loop and for each loop
System.out.println();
// Way 2 - getting code shorter and simpler
// For each loops
entries.forEach(entry -> {
System.out.println(entry.getKey() + "->"
+ entry.getValue());
});
// New line to differentiate differences in output
// between for each loop and iterator traversal
System.out.println();
// Way 3 - New way to
// Getting an iterator
Iterator > iterator
= entries.iterator();
// Additional step here
// To Initialize object holding for
// key-value pairs to null
Map.Entry entry = null;
// Holds true till there is no element remaining in
// the object using hasNExt() method
while (iterator.hasNext()) {
// Moving onto next pairs using next() method
entry = iterator.next();
// Printing the key-value pairs
// using getKet() and getValue() methods
System.out.println(entry.getKey() + "->"
+ entry.getValue());
}
}
}
输出
1->Geeks
2->For
3->Geeks
1->Geeks
2->For
3->Geeks
1->Geeks
2->For
3->Geeks