java - 将以相同名称开头的字符串连接在一起

标签 java

我正在尝试将具有相同名称的字符串合并在一起。

在当前状态下,我得到以下输出。

我尝试将 map 的值更改为列表,但后来我得到:

The method put(String, List) in the type Map is not applicable for the arguments (String, String)

输出

Ravensbusch 11:38 18:38 23:38
apple 11:54 18:54 23:53
table 11:50 18:50 23:49
markt haus 11:46 18:46 23:45
Rathausmarkt 11:41 18:41 23:40

输出应该是这样的;与缓冲区 arrayList 中的顺序一样:

Ravensbusch 04:41 ..... 23:38
Rathausmarkt 04:43 .... 23:40
markt haus 04:48 .... 18:46 23:45
table 04:52 .... 23:49
apple 04:56 ..... 23:53

代码:

 private static final Pattern pattern = 
        Pattern.compile("^ *(.+?)( +(?:[01][0-9]|2[0-3]):[0-5][0-9])+ *$");

public static void main(String[] args) {

    ArrayList<String> buffer = new ArrayList<String>();

    buffer.add("Ravensbusch street 04:41 05:41 06:09 06:38 07:08 07:38 08:08 08:38 09:08 09:38 10:08 10:38 11:08 11:38");
    buffer.add("Rathausmarkt 04:43 05:43 06:11 06:41 07:11 07:41 08:11 08:41 09:11 09:41 10:11 10:41 11:11 11:41");
    buffer.add("markt haus 04:48 05:48 06:16 06:46 07:16 07:46 08:16 08:46 09:16 09:46 10:16 10:46 11:16 11:46");
    buffer.add("table 04:52 05:52 06:20 06:50 07:20 07:50 08:20 08:50 09:20 09:50 10:20 10:50 11:20 11:50");
    buffer.add("apple 04:56 05:56 06:24 06:54 07:24 07:54 08:24 08:54 09:24 09:54 10:24 10:54 11:24 11:54");


    buffer.add("Ravensbusch street 12:08 12:38 13:08 13:38 14:08 14:38 15:08 15:38 16:08 16:38 17:08 17:38 18:08 18:38");
    buffer.add("Rathausmarkt 12:11 12:41 13:11 13:41 14:11 14:41 15:11 15:41 16:11 16:41 17:11 17:41 18:11 18:41");
    buffer.add("markt haus 12:16 12:46 13:16 13:46 14:16 14:46 15:16 15:46 16:16 16:46 17:16 17:46 18:16 18:46");
    buffer.add("table 12:20 12:50 13:20 13:50 14:20 14:50 15:20 15:50 16:20 16:50 17:20 17:50 18:20 18:50");
    buffer.add("apple 12:24 12:54 13:24 13:54 14:24 14:54 15:24 15:54 16:24 16:54 17:24 17:54 18:24 18:54");

    buffer.add("Ravensbusch street 19:08 19:38 20:08 20:38 21:38 22:38 23:38");
    buffer.add("Rathausmarkt 19:11 19:41 20:11 20:40 21:40 22:40 23:40");
    buffer.add("markt haus 19:16 19:46 20:16 20:45 21:45 22:45 23:45");
    buffer.add("table 19:20 19:50 20:20 20:49 21:49 22:49 23:49");
    buffer.add("apple 19:24 19:54 20:24 20:53 21:53 22:53 23:53");


    Map<String, String> stops = new HashMap<>(); 

     for (String line : buffer) {

            Matcher matcher = pattern.matcher(line);
            if (matcher.find()) {
                if (stops.containsKey(matcher.group(1))) {
                   stops.put(matcher.group(1), stops.get(matcher.group(1)) + matcher.group(2));
                } else {
                   stops.put(matcher.group(1), matcher.group(2).trim());
                }
            }                        
     }

     ArrayList<String> result = new ArrayList<>(stops.keySet().size());
     for (String key : stops.keySet()) {
         result.add(key + " " + stops.get(key));
     }

     for(String output: result){
         System.out.println(output);
     }                  
}

最佳答案

创建一个对象,该对象将包含地点和时间的字符串作为单个String。在构造函数中,拆分字符串并解析每个变量。如果 DateFormat.parse() 抛出异常,您就知道它是一个名称,因此,将其添加到名称变量中,反之亦然。下面是一个例子:

public class PlaceTime {
    StringBuilder place = new StringBuilder();
    Queue<String> times = new PriorityBlockingQueue<>();

    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 Queue<String> getTimes() {
        return this.times;
    }
}

现在,您需要做的就是为每个字符串创建 PlaceTime 对象,添加使用 getPlace 获取地点,并使用 getTimes 获取时间。将这些值添加到 map 中以避免重复,您最终会得到一个有时间的地方。下面是一个例子:

Map<String, Queue<String>> map = new LinkedHashMap<>();

List<PlaceTime> list = new ArrayList<>(
            Arrays.asList(
            new PlaceTime("Ravensbusch street 04:41 05:41 06:09 06:38 07:08 07:38 08:08 08:38 09:08 09:38 10:08 10:38 11:08 11:38"), 
            new PlaceTime("Ravensbusch street 04:41 05:41 06:09 06:38 07:08 07:38 08:08 08:38 09:08 09:38 10:08 10:38 11:08 11:38"),
            new PlaceTime("Ravensbusch street 19:08 19:38 20:08 20:38 21:38 22:38 23:38")));

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, Queue<String>> entry:map.entrySet()) {
    System.out.println(entry.getKey());
    System.out.println(entry.getValue());
}

此输出将是:

Ravensbusch street 
[04:41, 05:41, 06:09, 06:38, 07:08, 07:38, 08:08, 08:38, 09:08, 09:38, 10:08, 10:38, 11:08, 11:38, 12:08, 12:38, 13:08, 13:38, 14:08, 14:38, 15:08, 15:38, 16:08, 16:38, 17:08, 17:38, 18:08, 18:38, 19:08, 19:38, 20:08, 20:38, 21:38, 22:38, 23:38]

关于java - 将以相同名称开头的字符串连接在一起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30750765/

相关文章:

java - JComboBox运行3次后给出一个数组[jdbc]

java - Android SQLite : Using onUpgrade to update data?

java - 将声音文件链接到字符串值

java - 使用 Xpath 时按钮位置错误?

java - 用ant执行java类

java - AWS中的Kubernetes集群-哪些实例类型?

java - Netbeans 表示属性(property)值(value)不存在

Java Spring + ReactJs

java - Bean 配置 xml 文件头错误 -

java - 在嵌入式引用列表中对象化查询