时间:2015-06-10 13:20:02

标签: java

我正在尝试合并line toghether来为我的程序创建数据。当然,String 缓冲区ArrayList的合并部分运行良好,但当我输出Map的内容时,我正在获取String 缓冲区ArrayList的另一个String

有没有办法保持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;
    }
}

PlaceTime类:

>>> a = [3, 4, 54, 8, 96, 2]
>>> a[:2] + a[3:]
[3, 4, 8, 96, 2]

2 个答案:

答案 0 :(得分:3)

使用TreeMap对自然或自定义或LinkedHashMap进行排序以保留插入顺序:

LinkedHashMap用于广告订单

  

Map接口的哈希表和链表实现,具有可预测的迭代顺序。此链接列表定义迭代排序,通常是将键插入到地图(插入顺序)中的顺序。

Treemap用于自然或自定义排序

  

基于红黑树的NavigableMap实现。地图根据其键的自然顺序进行排序,或者根据使用的构造函数在地图创建时提供的比较器进行排序。

而不是HashMap

  

此课程不保证地图的顺序;特别是,它不保证订单会随着时间的推移保持不变。

答案 1 :(得分:1)

HashMap不会通过defenition来休闲迭代。当添加新元素以便不保留顺序时,它甚至会完全改变,而是使用LinkedHashMap

它将根据需要按照条目放入地图的顺序进行迭代。

查看documentation