i have map<string,integer>
, it's sorted value this:
set = map.entryset(); list = new arraylist<map.entry<string, integer»(set); collections.sort( list, (o1, o2) -> (o2.getvalue()).compareto( o1.getvalue() ));
i have sorted integer list map:
word_used = new arraylist<integer>(map.values() .stream() .sorted() .collect(collectors.tolist())); collections.reverse(word_used);
but how can string list, sorted equal map (by value)?
i mean if have map items:
map.put("eggs",1500); map.put("echo",150); map.put("foo",320); map.put("smt",50);
and sort like:
eggs : 1500 foo : 320 echo : 150 smt : 50
i need 2 lists:
eggs foo echo smt
and
1500 320 150 50
you can add projection streams using map()
, this:
list<string> word_used = map.entryset() .stream() .sorted(comparator.comparing(map.entry<string,integer>::getvalue).reversed()) .map(map.entry<string,integer>::getkey) .collect(collectors.tolist()); list<integer> ints_used = map.entryset() .stream() .sorted(comparator.comparing(map.entry<string,integer>::getvalue).reversed()) .map(map.entry<string,integer>::getvalue) .collect(collectors.tolist());
note approach sorts twice. can capture entries once, , project temporary list, this:
list<map.entry<string,integer>> sortedlist = map .entryset() .stream() .sorted(comparator.comparing(map.entry<string,integer>::getvalue).reversed()) .collect(collectors.tolist()); list<string> word_used = sortedlist .stream() .map(map.entry<string,integer>::getkey) .collect(collectors.tolist()); list<integer> ints_used = sortedlist .stream() .map(map.entry<string,integer>::getvalue) .collect(collectors.tolist());
Comments
Post a Comment