Keep the order of the map as in the arrayList before
我正在尝试将行合并到一起,以便为我的程序创建数据。目前,
有没有办法像在
输出:
1 2 3 4 5 6 7 8 9 10 | 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] |
输出应如下所示:
1 2 3 4 5 6 7 8 9 10 | 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] |
代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | 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>(); for (String element : buffer) { PlaceTime part = new PlaceTime(element); list.add(part); } for (PlaceTime t : list) { if (map.containsKey(t.getPlace())) { // If the map already contains an entry for the place, add the // times to the array map.get(t.getPlace()).addAll(t.getTimes()); } else { // Map does not contain entry for place, create a new entry map.put(t.getPlace(), t.getTimes()); } } // Print out contents of map for (Entry<String, List<String>> entry : map.entrySet()) { System.out.println(entry.getKey()); System.out.println(entry.getValue()); } System.out.println("Test"); } |
PlaceTime类:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | public class PlaceTime { StringBuilder place = new StringBuilder(); List<String> times = new ArrayList<>();; public PlaceTime(String placeTime) { DateFormat dateFormat = new SimpleDateFormat("HH:mm"); for (String i:placeTime.split("")) { try { dateFormat.parse(i); //No exception, add as time times.add(i); } catch (Exception e) { //Exception, add as place name place.append(i).append(""); } } } public String getPlace() { return place.toString(); } public List<String> getTimes() { return this.times; } } |
使用
用于插入顺序的
Hash table and linked list implementation of the Map interface, with predictable iteration order. This linked list defines the iteration ordering, which is normally the order in which keys were inserted into the map (insertion-order).
用于自然或自定义排序的
A Red-Black tree based NavigableMap implementation. The map is sorted according to the natural ordering of its keys, or by a Comparator provided at map creation time, depending on which constructor is used.
而不是
This class makes no guarantees as to the order of the map; in particular, it does not guarantee that the order will remain constant over time.
它将根据需要按条目放入映射的顺序迭代。
查看文档