java - Sort a hash map that contains double and string values -



java - Sort a hash map that contains double and string values -

i have been given text file winter olympic event. contains team, name of competitor , score.

framae berenice meite 455.455 chnkexin zhang 454.584 ukrnatalia popova 453.443 gernathalie weinzierl 452.162 rusevgeny plyushchenko 191.399 canpatrick chan 189.718 chnhan yan 185.527 chncheng & hao 271.018 itastefania & ondrej 270.317 usamarissa & simon 264.256 germaylin & daniel 260.825 fraflorent amodio 179.936 gerpeter liebers 179.615 ect....

the digit maters lastly digit in number. each team, need sum total points , display top 5 teams.

code far:

public class project2 { public static void main(string[] args) throws ioexception { // todo auto-generated method stub string[] array = new string[41]; string[] info = new string [41]; string[] stats = new string[41]; string[] team = new string[41]; //.txt file location fileinput filein = new fileinput(); filein.openfile("c:\\users\\o\\desktop\\turn in\\team.txt"); //txt file array int i=0; string line = filein.readline(); array[i] = line; i++; while (line != null) { line = filein.readline(); array[i] = line; i++; } system.out.println(array[1]); //splitting team/competitor name/score (int j =0; j< 40; j++){ team[j] = array[j].substring (0, 3).trim (); info[j] = array[j].substring (3, 30).trim (); stats[j] = array[j].substring (36).trim (); } //random outputs ive been using fore testing. system.out.println(team[1]); system.out.println(info[1]); system.out.println(stats[1]); myobject ob = new myobject(); ob.settext(info[0]); ob.setnumber(7, 23); ob.setnumber(3, 456); system.out.println("text " + ob.gettext() + " , number 3 " + ob.getnumber(7)); (int j =0; j< 40; j++){ team[j] = array[j].substring (0, 3).trim (); info[j] = array[j].substring (3, 30).trim (); stats[j] = array[j].substring (36).trim (); } //team , score in hashmap double[] statsdub = new double[40]; (int k =1; k < 40; k++){ statsdub[k] = double.parsedouble(stats[k]); } map<string,double> totalscore = new hashmap<>(); (int j =0; j< 40; j++){ double tmp = totalscore.get (team[j]); if (tmp != null) { totalscore.put(team[j], statsdub[j]+tmp); } else totalscore.put(team[j], statsdub[j]); } // set of entries set set = totalscore.entryset(); // iterator iterator i1 = set.iterator(); // display elements while(i1.hasnext()) { map.entry me = (map.entry)i1.next(); system.out.print(me.getkey() + ": "); system.out.println(me.getvalue()); }}} //if (totalscore.containskey("countryname")) // totalscore.put("countryname", totalscore.get("countryname") + playerscore); //else // totalscore.put("countryname",0);

i output:

ger: 17.0 usa: 27.0 ita: 23.0 rus: 37.0 chn: 20.0 jpn: 24.0 fra: 17.0 can: 32.0 ukr: 10.0 gbr: 8.0

how go having top 5 teams displayed in descending order?

you have create method sorts hashmap.

there few examples methods in post: sorting hashmap values

java string sorting hashmap order

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -