-2

I want to sort the map. I have string as key and integer as value.

Example:

key      value
"1,3"     40
"1,5"     20 
"2,5"     10
"2,10"    30


Output:
"2,5"     10
"1,5"     20
"2,10"    30
"1,3"     40

I tried the below code but it is not working. Can you please help me out.

 treemap = treemap = new TreeMap<String, Integer>();
while((line = br.readLine()) !=null )
        {
            int sum=0;

              sum = //something
              String keys = xCordinate + "," + yCordinate; //getting it
              treemap.put(keys, sum);

        }
        testMap(treemap );
    }

    public static void testMap(Map <String,Integer> map)
    {
        for(Integer value1 : map.values())
        {
            String keys = map.get(value1).toString(); //error in this line
            System.out.println(keys + "  " + value1);
        }
    }

Here, I get the perfect values in treemap..When I debug and see, the key value and count are perfect, but when i go in for loop, theere comes an error in second line.

StackTrace

Exception in thread "main" java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer
    at java.lang.Integer.compareTo(Unknown Source)
    at java.util.TreeMap.getEntry(Unknown Source)
    at java.util.TreeMap.get(Unknown Source)
    at TestJavaServer.testMap(TestJavaServer.java:75)
    at TestJavaServer.readFromFile(TestJavaServer.java:68)
    at TestJavaServer.main(TestJavaServer.java:22)
user3365810
  • 35
  • 1
  • 5
  • What error? Can you please add the stack trace? – Giovanni Botta Feb 28 '14 at 17:09
  • @GiovanniBotta StackTrace added – user3365810 Feb 28 '14 at 17:11
  • 3
    And can you please correct the syntax and formatting? The line `for(Integer value1 : map.)` is wrong, the first line doesn't make sense and you have mismatched curly braces. – Giovanni Botta Feb 28 '14 at 17:13
  • Also, can you state more clearly what you're trying to do? Sort the map how? Can you give a simple input/expected output example? – Giovanni Botta Feb 28 '14 at 17:18
  • You don't get that for free unless your sorted map used the integer as the key (are the integers unique?). If you need the string to be the key, you will need to extract the pairs from the map and then sort them according to the value using a [`Collections.sort()`](http://docs.oracle.com/javase/7/docs/api/java/util/Collections.html). Try that and come back with more code. – Giovanni Botta Feb 28 '14 at 17:25
  • By the way, if that's the only goal, this question is a [duplicate](http://stackoverflow.com/questions/109383/how-to-sort-a-mapkey-value-on-the-values-in-java). – Giovanni Botta Feb 28 '14 at 17:29

3 Answers3

0

Asuming Java ,you could sort hashmap just like this :

public LinkedHashMap sortHashMapByValuesD(HashMap passedMap) {
   List mapKeys = new ArrayList(passedMap.keySet());
   List mapValues = new ArrayList(passedMap.values());
   Collections.sort(mapValues);
   Collections.sort(mapKeys);

   LinkedHashMap sortedMap = new LinkedHashMap();

  Iterator valueIt = mapValues.iterator();
  while (valueIt.hasNext()) {
   Object val = valueIt.next();
   Iterator keyIt = mapKeys.iterator();

   while (keyIt.hasNext()) {
       Object key = keyIt.next();
       String comp1 = passedMap.get(key).toString();
       String comp2 = val.toString();

       if (comp1.equals(comp2)){
           passedMap.remove(key);
           mapKeys.remove(key);
           sortedMap.put((String)key, (Double)val);
           break;
       }

   }

   }
  return sortedMap;

} Just a kick-off example , This way is more useful as it sorts the HashMap and keeps the duplicate values as well.

Sunil Singh Bora
  • 771
  • 9
  • 24
0

When you call map.get(something), that something must be of the same type as the map's key (String in your case), but you are passing in an Integer.

Giovanni Botta
  • 9,626
  • 5
  • 51
  • 94
  • For information: this behavior is not for all map implementations. HashMap returns null. Treemap (this case) ClassCastException – pL4Gu33 Feb 28 '14 at 17:27
  • 1
    That's because of the nature of the sorted map implementation. The key must be cast to the right type because the map uses the key's natural ordering to retrieve values. – Giovanni Botta Feb 28 '14 at 17:28
0

The code

public static void testMap(Map <String,Integer> map)

says that your map has keys of the type String, whereas here

String keys = map.get(value1).toString(); //error in this line
                        --

value1 is an integer, hence the exception.

Ankit Rustagi
  • 5,539
  • 12
  • 39
  • 70
  • This answer is misleading. Why would he want to convert the int to string? Do you know what he's trying to do? Please let's make the SO better before giving solutions. – Giovanni Botta Feb 28 '14 at 17:17
  • I hope my answer covers your doubts, dont expect somebody to solve it for you. – Ankit Rustagi Feb 28 '14 at 17:26