📜  在Java中以相反的顺序迭代 TreeMap

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

在Java中以相反的顺序迭代 TreeMap

Java中的 TreeMap 与 AbstractMap 类一起用于实现 Map 接口和 NavigableMap。 TreeMap 根据其键的自然顺序进行排序。

在Java中以相反的顺序迭代 TreeMap 有三种简单的方法:

  1. 使用 reverseOrder() 方法
  2. 使用descendingKeySet() 方法
  3. 使用descendingMap() 方法

方法一:

Collections 类的reverseOrder()方法返回一个 Comparator,它强制对象的自然顺序相反。在 TreeMap 的构造函数中使用 this 来创建一个对象,该对象以键的相反顺序存储映射。

下面是实现:

Java
// Java Program to iterate TreeMap in Reverse Order in Java
 
import java.util.*;
 
public class GFG {
    public static void main(String[] args)
    {
 
        // Use reverseOrder() method in the constructor
        TreeMap treeMap
            = new TreeMap<>(Collections.reverseOrder());
 
        // Add elements to treeMap
        treeMap.put(1, "Hello");
        treeMap.put(2, "geeks");
        treeMap.put(3, "on");
        treeMap.put(4, "geeksforgeeks");
 
        // Print the TreeMap in reverse order of the keys
        System.out.println("TreeMap in reverse order: "
                           + treeMap);
    }
}


Java
// Java Program to iterate TreeMap in Reverse Order in Java
 
import java.util.*;
 
public class GFG {
    public static void main(String[] args)
    {
 
        // New TreeMap
        TreeMap treeMap = new TreeMap<>();
 
        // Add elements to treeMap
        treeMap.put(1, "Hello");
        treeMap.put(2, "geeks");
        treeMap.put(3, "on");
        treeMap.put(4, "geeksforgeeks");
 
        // Print the TreeMap
        System.out.println("TreeMap before reverse: "
                           + treeMap);
 
        // view set of the keys in reverseOrder
        Set keySet = treeMap.descendingKeySet();
 
        // After reverse
        System.out.println("TreeMap after reverse:");
 
        // Iterate view set of the keys
        // and get value of the key
        for (Integer key : keySet) {
            // Print key:value of the TreeMap
            System.out.println(key + " = "
                               + treeMap.get(key));
        }
    }
}


Java
// Java Program to iterate TreeMap in Reverse Order in Java
 
import java.util.*;
 
public class GFG {
    public static void main(String[] args)
    {
 
        // New TreeMap
        TreeMap treeMap = new TreeMap<>();
 
        // Add elements to treeMap
        treeMap.put(1, "Hello");
        treeMap.put(2, "geeks");
        treeMap.put(3, "on");
        treeMap.put(4, "geeksforgeeks");
 
        // Before reverse
        System.out.println("TreeMap before reverse:"
                           + treeMap);
 
        // view map containing reverse view of mapping
        Map reverseMap
            = treeMap.descendingMap();
 
        // After reverse
        System.out.println("TreeMap after reverse:"
                           + reverseMap);
    }
}



输出
TreeMap in reverse order: {4=geeksforgeeks, 3=on, 2=geeks, 1=Hello}

时间复杂度: O(N)

方法二:

DescingKeySet()方法返回键的逆序 Set 视图。因此迭代以降序返回 TreeMap 键的集合视图,并在 get() 方法的帮助下获取值。

注意: descendingKeySet() 方法返回的键集是一个视图,它由原始 TreeMap 对象支持。对此视图所做的任何更改都将反映在原始 TreeMap 对象中,反之亦然。

下面是实现:

Java

// Java Program to iterate TreeMap in Reverse Order in Java
 
import java.util.*;
 
public class GFG {
    public static void main(String[] args)
    {
 
        // New TreeMap
        TreeMap treeMap = new TreeMap<>();
 
        // Add elements to treeMap
        treeMap.put(1, "Hello");
        treeMap.put(2, "geeks");
        treeMap.put(3, "on");
        treeMap.put(4, "geeksforgeeks");
 
        // Print the TreeMap
        System.out.println("TreeMap before reverse: "
                           + treeMap);
 
        // view set of the keys in reverseOrder
        Set keySet = treeMap.descendingKeySet();
 
        // After reverse
        System.out.println("TreeMap after reverse:");
 
        // Iterate view set of the keys
        // and get value of the key
        for (Integer key : keySet) {
            // Print key:value of the TreeMap
            System.out.println(key + " = "
                               + treeMap.get(key));
        }
    }
}


输出
TreeMap before reverse: {1=Hello, 2=geeks, 3=on, 4=geeksforgeeks}
TreeMap after reverse:
4 = geeksforgeeks
3 = on
2 = geeks
1 = Hello

时间复杂度: O(N)

方法三:

TreeMap 类的descendingMap()方法返回一个包含映射的反向视图的映射。使用条目集的迭代器迭代映射。

注意: descendingMap() 方法返回的降序映射是一个视图,因此所做的任何更改都将反映在原始 TreeMap 中,反之亦然。

下面是实现:

Java

// Java Program to iterate TreeMap in Reverse Order in Java
 
import java.util.*;
 
public class GFG {
    public static void main(String[] args)
    {
 
        // New TreeMap
        TreeMap treeMap = new TreeMap<>();
 
        // Add elements to treeMap
        treeMap.put(1, "Hello");
        treeMap.put(2, "geeks");
        treeMap.put(3, "on");
        treeMap.put(4, "geeksforgeeks");
 
        // Before reverse
        System.out.println("TreeMap before reverse:"
                           + treeMap);
 
        // view map containing reverse view of mapping
        Map reverseMap
            = treeMap.descendingMap();
 
        // After reverse
        System.out.println("TreeMap after reverse:"
                           + reverseMap);
    }
}


输出
TreeMap before reverse:{1=Hello, 2=geeks, 3=on, 4=geeksforgeeks}
TreeMap after reverse:{4=geeksforgeeks, 3=on, 2=geeks, 1=Hello}

时间复杂度: O(N)