0

Forgive me if it seems simple but I couldn't figure it out easily. I thought about using a loop but was wondering if anybody knows an easier way: I have:

Map<String, Integer> map = new HashMap<>();

        map.put("ClubB", 1);
        map.put("ClubA", 2);
        map.put("ClubC", 2);
        map.put("ClubD", 2);
        map.put("ClubE", 3);
        map.put("ClubF", 2);
        map.put("ClubG", 2);

I need to get keys or values at specific index in my tests. For example I need to get the key and value at index 3 etc. The reason is that I use a comparator and sort the Map and would like to show that the value at a specific index has changed.

Thanks for any ideas.

UPDATE:

I used:

HashMap leagueTable = new HashMap();
Map<String, Integer> map = sortByValues(leagueTable);

 public <K extends Comparable<K>, V extends Comparable<V>> Map<K, V> sortByValues(final Map<K, V> map) {
        Comparator<K> valueComparator = new Comparator<K>() {
            public int compare(K k1, K k2) {
                int compare = map.get(k2).compareTo(map.get(k1));
                if (compare == 0) {
                    return k1.compareTo(k2); // <- To sort alphabetically
                } else {
                    return compare;
                }
            }
        };
        Map<K, V> sortedByValues = new TreeMap<K, V>(valueComparator);
        sortedByValues.putAll(map);
        return sortedByValues;
    }

I then used aloop to print out values:

for (Map.Entry<String, Integer> entry : map.entrySet()) {

                    System.out.println( entry.getKey() + ", " + entry.getValue() );
} 
Afshin Ghazi
  • 2,784
  • 4
  • 23
  • 37
  • What index are you talking about? There're only key and value when dealing with maps. – Maroun Jan 17 '16 at 16:08
  • Yes I know that. What I mean is that I want the value of for example the key/value in the first position or third position or fourth position etc without using a loop and counter. – Afshin Ghazi Jan 17 '16 at 16:10
  • The map is not ordered. Maybe [`LinkedHashMap`](http://docs.oracle.com/javase/8/docs/api/java/util/LinkedHashMap.html) us what you're looking for. – Maroun Jan 17 '16 at 16:11
  • I ordered the Map using a comparator. Just wanted to get key or value at specific index to show it has changed after ordering in JUnit tests. – Afshin Ghazi Jan 17 '16 at 16:17
  • 1
    @AfshinGhazi a `HashMap` has **no order**. You cannot have "_ordered the `Map` using a `Comparator`_". Please post the code to show what exactly you did. – Boris the Spider Jan 17 '16 at 16:17
  • Show us the JUnit test. It is possible you are do not need indexed access. – Miserable Variable Jan 17 '16 at 16:25
  • Perhaps I should have asked if there is a way to show that after sorting the Map after the values are sorted. The point is I don't know how to conduct JUnit tests on this scenario. – Afshin Ghazi Jan 17 '16 at 16:31
  • Seems like a common theme on SO. First come trolls that are searching for problems with your question so as to gain points. Second round is people asking or clarification and more code looking for points. Finally when you have added the extra code everybody disappears. New people coming don't try the problem since there seems to be too much code. Some great people but many people only looking for points. – Afshin Ghazi Jan 17 '16 at 16:57

2 Answers2

0

Ended up using a for loop and comparing with what I already had added to the Map:

HashMap<String, Integer> map = new HashMap<>();

        map.put("ClubD", 3);
        map.put("ClubB", 1);
        map.put("ClubA", 2);
        map.put("ClubC", 2);
        map.put("ClubE", 2);
        map.put("ClubF", 2);
        map.put("ClubG", 2);


        Map<String, Integer> mapResult = instance.sortByValues(map);
        String expectedResultKey = "ClubB";
        int expectedResultValue = 1;
        String resultKey = "";
        int resultValue = 0;
        for (Map.Entry<String, Integer> entry : map.entrySet()) {
        resultKey = entry.getKey();
        resultValue = entry.getValue();

        }

        assertSame(expectedResultKey, resultKey);
Afshin Ghazi
  • 2,784
  • 4
  • 23
  • 37
0

HashMap's don't have indices. They just store data in key-value pairs.

Instead of map being a Map, why don't you use a 2D array? (And give it a more appropriate name)

String[][] array = new String[3][3];
array[3] = new String[] { "ClubD" };
array[1] = new String[] { "ClubB" };
array[2] = new String[] { "ClubA", "ClubC", "ClubE", "ClubF", "ClubG" };

System.out.println(array[3][0]);

and then if you wanted to loop through that array, you would just do:

for (int a = 0; a < array.length; a++)
    for (int b = 0; b < array[a].length; b++)
        if (array[a][b] != null)
            System.out.println("array["+a+"]["+b+"] is: "+array[a][b]);
John S.
  • 626
  • 1
  • 4
  • 16