java - Keep the order of the map as in the arrayList before -
this question has answer here:
i trying merge line toghether create data programm. curently merging part of string
s in arraylist
's buffer works when output content of map
getting of string
in arraylist
's buffer.
is there way keep order of string
s in arraylist
buffer?
output:
nathan marcus adler [04:43, 05:43, 12:11, 12:41, 19:11, 19:41] dukes of bedford [04:56, 05:56, 12:24, 12:54, 19:24, 19:54] prince albert [04:48, 05:48, 12:16, 12:46, 19:16, 19:46] joseph addison [04:41, 05:41, 12:08, 12:38, 19:08, 19:38] william baker [04:52, 05:52, 12:20, 12:50, 19:20, 19:50]
the ouput should looks this:
joseph addison [04:41, 05:41, 12:08, 12:38, 19:08, 19:38] nathan marcus adler [04:43, 05:43, 12:11, 12:41, 19:11, 19:41] prince albert [04:48, 05:48, 12:16, 12:46, 19:16, 19:46] william baker [04:52, 05:52, 12:20, 12:50, 19:20, 19:50] dukes of bedford [04:56, 05:56, 12:24, 12:54, 19:24, 19:54]
code:
public static void main(string[] args) { arraylist<string> buffer = new arraylist<string>(); buffer.add("joseph addison 04:41 05:41"); buffer.add("nathan marcus adler 04:43 05:43"); buffer.add("prince albert 04:48 05:48"); buffer.add("william baker 04:52 05:52"); buffer.add("dukes of bedford 04:56 05:56"); buffer.add("joseph addison 12:08 12:38 "); buffer.add("nathan marcus adler 12:11 12:41"); buffer.add("prince albert 12:16 12:46"); buffer.add("william baker 12:20 12:50"); buffer.add("dukes of bedford 12:24 12:54"); buffer.add("joseph addison 19:08 19:38"); buffer.add("nathan marcus adler 19:11 19:41"); buffer.add("prince albert 19:16 19:46"); buffer.add("william baker 19:20 19:50"); buffer.add("dukes of bedford 19:24 19:54"); map<string, list<string>> map = new hashmap<>(); arraylist<placetime> list = new arraylist<placetime>(); (string element : buffer) { placetime part = new placetime(element); list.add(part); } (placetime t : list) { if (map.containskey(t.getplace())) { // if map contains entry place, add // times array map.get(t.getplace()).addall(t.gettimes()); } else { // map not contain entry place, create new entry map.put(t.getplace(), t.gettimes()); } } // print out contents of map (entry<string, list<string>> entry : map.entryset()) { system.out.println(entry.getkey()); system.out.println(entry.getvalue()); } system.out.println("test"); }
placetime class:
public class placetime { stringbuilder place = new stringbuilder(); list<string> times = new arraylist<>();; public placetime(string placetime) { dateformat dateformat = new simpledateformat("hh:mm"); (string i:placetime.split(" ")) { try { dateformat.parse(i); //no exception, add time times.add(i); } catch (exception e) { //exception, add place name place.append(i).append(" "); } } } public string getplace() { return place.tostring(); } public list<string> gettimes() { return this.times; } }
use treemap
sort natural or customized or linkedhashmap
mantain insertion order:
linkedhashmap
insertion order
hash table , linked list implementation of map interface, predictable iteration order. linked list defines iteration ordering, order in keys inserted map (insertion-order).
treemap
natural or custom sorting
a red-black tree based navigablemap implementation. map sorted according natural ordering of keys, or comparator provided @ map creation time, depending on constructor used.
instead of hashmap
this class makes no guarantees order of map; in particular, not guarantee order remain constant on time.
Comments
Post a Comment