1

I need a set that allows me to order element by key. The keys are Integer, but they are not sequential. This is and example of what I have:

<3, Alessandro>
<12, Mario>
<1, Marco>

And I need to print those elements in this order:

Marco    
Alessandro
Mario

I tried using TreeMap, but I can't make a loop like this:

for(int i = 0; i < treeMap.size(); i++){    
    System.out.println(treeMap.get(i));         
}

because the key is not sequential and I would get an error when calling treeMap.get(), because I don't know the Key integer.

4

2 回答 2

4

get gets by key, not index position. Iterate over the entrySet or keySet.

Setup:

TreeMap<Integer, String> treeMap = new TreeMap<Integer, String>();

treeMap.put(3, "Alessandro");
treeMap.put(12, "Mario");
treeMap.put(1, "Marco");

Example using entrySet, in which case you get a Map.Entry<Integer, String> for each iteration:

for (Map.Entry<Integer, String> entry : treeMap.entrySet()) {
    System.out.println(entry.getValue());
}

Or if you prefer keySet, which gives you the keys (Integers):

for (Integer key : treeMap.keySet()) {
    System.out.println(treeMap.get(key));
}

Either way, the above results in:

Marco
Alessandro
Mario

E.g., ordered by the natural order of the keys.

于 2015-01-21T10:17:34.853 回答
2

If You really need to use set instead Map, You can use TreeSet and as a type used in set use object that wraps key and value and is compared by key. Sample below:

package com.stackoverflow;

public class KeyValue<K extends Number, V> implements Comparable<KeyValue<K, V>> {

    private final K key;

    private final V value;

    public KeyValue(K key, V value) {
        this.key = key;
        this.value = value;
    }

    public K getKey() {
        return key;
    }

    public V getValue() {
        return value;
    }

    @Override
    public int compareTo(KeyValue<K, V> o) {
        long keyValue = key.longValue();
        long argKeyValue = o.key.longValue();
        return (keyValue == argKeyValue) ? 0 : (keyValue < argKeyValue) ? -1 : 1;
    }

    @Override
    public String toString() {
        return key.toString() + ": " + value.toString();
    }
}

And sample usage:

package com.stackoverflow;

import java.util.TreeSet;

public class Main {

    public static void main(String[] args) {
        TreeSet<KeyValue<Integer, String>> keyValues = new TreeSet<KeyValue<Integer, String>>();

        KeyValue<Integer, String> keyValue1 = new KeyValue<Integer, String>(2, "asd");
        KeyValue<Integer, String> keyValue2 = new KeyValue<Integer, String>(102, "sfsaf");
        KeyValue<Integer, String> keyValue3 = new KeyValue<Integer, String>(12, "dgs");
        KeyValue<Integer, String> keyValue4 = new KeyValue<Integer, String>(222, "dsgds");
        KeyValue<Integer, String> keyValue5 = new KeyValue<Integer, String>(22, "aa");
        KeyValue<Integer, String> keyValue6 = new KeyValue<Integer, String>(1, "sfasaf");
        KeyValue<Integer, String> keyValue7 = new KeyValue<Integer, String>(3, "sfs");
        KeyValue<Integer, String> keyValue8 = new KeyValue<Integer, String>(4, "www");

        keyValues.add(keyValue1);
        keyValues.add(keyValue2);
        keyValues.add(keyValue3);
        keyValues.add(keyValue4);
        keyValues.add(keyValue5);
        keyValues.add(keyValue6);
        keyValues.add(keyValue7);
        keyValues.add(keyValue8);

        for (KeyValue<Integer, String> keyValue : keyValues) {
            System.out.println(keyValue);
        }
    }
}
于 2015-01-21T10:25:04.860 回答