Tuesday, 10 September 2013

How to leave max value in Map of Strings

How to leave max value in Map of Strings

Hi,
I have list of illogical strings, for example:
Scanner s = new Scanner(
"m29 523\n" +
"b34 827\n" +
"p42 235\n" +
"b34 294\n" +
"t78 421\n" +
"t78 673\n" +
"c93 173\n" +
"k46 925\n" +
"k46 322\n" +
"x21 644\n");
then I want to break string in two parts - before and after space,
eliminate duplicates and leave only max value(after space) among
duplicates(b34, t78, k46). I mean such output:
"m29 523"
"b34 827"
"p42 235"
"t78 673"
"c93 173"
"k46 925"
"x21 644"
As for breaking in two parts and eliminate duplicates I used such code,
it's ok for me:
Map<String, ArrayList<String>> list = new HashMap<String,
ArrayList<String>>();
while (s.hasNext()){
String key = s.next();
if(!list.containsKey(key));
list.put(key, new ArrayList<String>());
list.get(key).add(s.next());
}
System.out.println(list);
But don't understand how here I could implement comparison of the second
part of string to get max value? Integer.parseInt(list.get(key))?

No comments:

Post a Comment