Java 保持映射的顺序与前面的arrayList中相同
我正在尝试将行合并到一起,以便为我的程序创建数据。目前,Java 保持映射的顺序与前面的arrayList中相同,java,Java,我正在尝试将行合并到一起,以便为我的程序创建数据。目前,数组列表的缓冲区中的字符串的合并部分工作正常,但是当我输出映射的内容时,我得到了另一个字符串,就像它们在数组列表的缓冲区中一样 有没有办法保持Strings在ArrayList缓冲区中的顺序 输出: 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:5
数组列表
的缓冲区中的字符串
的合并部分工作正常,但是当我输出映射
的内容时,我得到了另一个字符串
,就像它们在数组列表
的缓冲区中一样
有没有办法保持String
s在ArrayList
缓冲区中的顺序
输出:
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]
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]
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");
}
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;
}
}
输出应如下所示:
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]
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]
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");
}
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;
}
}
代码:
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]
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]
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");
}
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;
}
}
publicstaticvoidmain(字符串[]args){
ArrayList缓冲区=新的ArrayList();
buffer.add(“Joseph Addison 04:41 05:41”);
添加(“Nathan Marcus Adler 04:43 05:43”);
添加(“阿尔伯特王子04:48 05:48”);
添加(“William Baker 04:52 05:52”);
添加(“贝德福德公爵04:56 05:56”);
buffer.add(“Joseph Addison 12:08 12:38”);
添加(“Nathan Marcus Adler 12:11 12:41”);
加上(“阿尔伯特亲王12:16 12:46”);
添加(“William Baker 12:20 12:50”);
缓冲区。添加(“贝德福德公爵12:24 12:54”);
buffer.add(“Joseph Addison 19:08 19:38”);
添加(“Nathan Marcus Adler 19:11 19:41”);
加上(“阿尔伯特亲王19:16 19:46”);
添加(“William Baker 19:20 19:50”);
添加(“贝德福德公爵19:24 19:54”);
Map Map=newhashmap();
ArrayList=新建ArrayList();
for(字符串元素:缓冲区){
PlaceTime部分=新的PlaceTime(元素);
列表。添加(部分);
}
for(PlaceTime t:list){
if(map.containsKey(t.getPlace())){
//如果地图已包含该地点的条目,请添加
//到阵列的时间
get(t.getPlace()).addAll(t.getTimes());
}否则{
//地图不包含place的条目,请创建新条目
put(t.getPlace(),t.getTimes());
}
}
//打印地图的内容
for(条目:map.entrySet()){
System.out.println(entry.getKey());
System.out.println(entry.getValue());
}
系统输出打印(“测试”);
}
PlaceTime类:
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]
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]
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");
}
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;
}
}
公共类放置时间{
StringBuilder位置=新建StringBuilder();
列表时间=新建ArrayList();;
公共PlaceTime(字符串PlaceTime){
DateFormat DateFormat=新的SimpleDateFormat(“HH:mm”);
for(字符串i:placeTime.split(“”){
试一试{
dateFormat.parse(i);
//没有例外,添加为时间
增加(i);
}捕获(例外e){
//异常,添加为地名
位置。追加(一)。追加(“”);
}
}
}
公共字符串getPlace(){
返回place.toString();
}
公共列表getTimes(){
把这个还给我。次;
}
}
HashMap
不会按defention暂停迭代顺序。当添加新元素时,它甚至会完全改变,因此不会保留顺序,请改用LinkedHashMap
它将根据需要按照条目放入映射的顺序进行迭代
查看使用
TreeMap
对自然或定制进行排序,或LinkedHashMap
对插入顺序进行排序:
插入顺序
哈希表和链表实现的映射接口,具有可预测的迭代顺序。此链表定义了迭代顺序,通常是键插入地图的顺序(插入顺序)
用于自然或自定义排序
基于红黑树的NavigableMap实现。映射根据其键的自然顺序进行排序,或者由映射创建时提供的比较器进行排序,具体取决于使用的构造函数
而不是
此类不保证地图的顺序;特别是,它不能保证订单在一段时间内保持不变
当前输出和期望输出之间没有差异。重新表述问题使用
LinkedHashMap
代替,它维护插入顺序。TreeMap
将对插入进行排序,LinkedHashMap
是关键。它不适用于TreeMap,但适用于LinkedHashMap
@MrAsker检查我的编辑。。。。