基于键对树图进行排序,其中键是变量

发布于 2024-11-27 16:47:20 字数 94 浏览 0 评论 0原文

我想根据键对树图进行排序,其中键是变量,因此排序应该基于变量值,我们如何实现这一点?我想在内置排序方法中使用 Rathar 通过代码实现它,任何带有示例的回复都会有很大帮助。

I want to sort the tree map based on the key where key is a variable,so sorting should be based on variable value, How can we achieve this? I want use in built sort method rathar implementing it through code, any reply with example is of great help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

川水往事 2024-12-04 16:47:20

TreeMap (它实现 SortedMap) 自动将密钥存储在正确的位置order:

Map<Integer, String> map = new TreeMap<Integer, String>();
map.put(1, "one");
map.put(3, "three");
map.put(2, "two"); 
// prints one two three   
for(Integer key : map.keySet()) {
    System.out.println(map.get(key));
}

作为 Key-Type (在这种情况下 Integer),您可以使用任何实现 Comparable (或者您可以提供 Comparator 创建 TreeMap 时)

编辑:好的,这里是如何重新映射地图的建议。

Map<Integer, String> oldMap; // get oldMap from somewhere
// Prepare remapping
Map<Integer, String> newMap = new TreeMap<Integer, String>();
Map<Integer, Integer> keyMap = new HashMap<Integer, Integer>();
// Store a new key for each old key
keyMap.put(oldKey, newKey);
// fill the newMap
for(Integer oldKey : keyMap.keySet()) {
    newMap.put(keyMap.get(oldKey), oldMap.get(oldKey));
}
oldMap = newMap; // if needed

TreeMap (which implements SortedMap) stores automatically the keys in the correct order:

Map<Integer, String> map = new TreeMap<Integer, String>();
map.put(1, "one");
map.put(3, "three");
map.put(2, "two"); 
// prints one two three   
for(Integer key : map.keySet()) {
    System.out.println(map.get(key));
}

As Key-Type (in that case Integer) you can use any class which implements Comparable (or you can provide a Comparator when creating the TreeMap)

Edit: Okay, here is a suggestion how to re-map your map.

Map<Integer, String> oldMap; // get oldMap from somewhere
// Prepare remapping
Map<Integer, String> newMap = new TreeMap<Integer, String>();
Map<Integer, Integer> keyMap = new HashMap<Integer, Integer>();
// Store a new key for each old key
keyMap.put(oldKey, newKey);
// fill the newMap
for(Integer oldKey : keyMap.keySet()) {
    newMap.put(keyMap.get(oldKey), oldMap.get(oldKey));
}
oldMap = newMap; // if needed
烂柯人 2024-12-04 16:47:20

treemap 是一棵红黑树,这是一个平衡的二叉搜索树。换句话说,树已经排序(或者更确切地说,按照二叉搜索树规则排列),其高度平衡,因此树操作具有 O(lg n) 复杂度。但是,我认为您想要的是按排序顺序打印所有键。这就像在树形图上实现中序遍历一样简单,或者您可以使用 keySet() 方法来获取 Set 并迭代值。

例如中序遍历

void inorderTraversal( Node root ){
    if( root == null ) return;
    inorderTraversal( root.getLeft() );
    root.printValue();
    inorderTraversal( root.getRight() );
}

编辑

好的,我很确定这就是你想要的。您想按值排序:

        Map<String, Integer> map = new TreeMap<String, Integer>();
        map.put("one", 8);
        map.put("two", 10);
        map.put("three", 9);
        map.put("hundred", 1);
        System.out.println(map.values());

输出:

[1, 8, 9, 10]

所以这甚至适用于对字符串值进行排序:

    Map<Integer, String> map = new TreeMap<Integer, String>();
        map.put(8, "one");
        map.put(10, "two");
        map.put(9, "three");
        map.put(1, "hundred");
        System.out.println(map.values());

输出:

[hundred, one, three, two]

另外,sachin 请注意,拥有“变量键”和变量值是完全不同的事情。

A treemap is a Red-black tree, which is a balanced binary search tree. In other words, the tree is already sorted (or rather, arranged as per the binary search tree rules) with its height balanced so that tree operations have a O(lg n) complexity. However, I think what you want is to print all the keys in sorted order. This is as simple as implementing an inorder traversal on the treemap, or you could use the keySet() method to get a Set and iterate over the values.

e.g. of inorder traversal

void inorderTraversal( Node root ){
    if( root == null ) return;
    inorderTraversal( root.getLeft() );
    root.printValue();
    inorderTraversal( root.getRight() );
}

EDIT:

Okay, I'm pretty sure this is what you want. You want to sort by values:

        Map<String, Integer> map = new TreeMap<String, Integer>();
        map.put("one", 8);
        map.put("two", 10);
        map.put("three", 9);
        map.put("hundred", 1);
        System.out.println(map.values());

Output:

[1, 8, 9, 10]

So this works even for sorting string values:

    Map<Integer, String> map = new TreeMap<Integer, String>();
        map.put(8, "one");
        map.put(10, "two");
        map.put(9, "three");
        map.put(1, "hundred");
        System.out.println(map.values());

Output:

[hundred, one, three, two]

Also, sachin take note that having "variable keys" and variable values are completely different things.

春花秋月 2024-12-04 16:47:20

TreeMap 实现SortedMap接口并按其键排序,而无需您做任何事:

映射根据其键的自然顺序或按
比较器在地图创建时提供,具体取决于
使用构造函数。

TreeMap implements the SortedMap interface and is sorted by its key without you having to do anything:

The map is sorted according to the natural ordering of its keys, or by
a Comparator provided at map creation time, depending on which
constructor is used.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文