如何为java中的字符串队列分配字符串?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (1)
  • 关注 (0)
  • 查看 (79)

有一个字符串str =“id = 5 time = 44,id = 6 time = 9,id = 3 time = 43,id = 1 time = 29,id = 7 time = 10,id = 6 time = 9,id = 2时间= 33,id = 6时间= 55“我需要将其保存为队列!我怎么能用Java做到这一点?

提问于
用户回答回答于

这个解决方案

import java.util.*;
import java.util.regex.*;

public class Queue
{
    public static void main(String argv[])
    {
        TreeMap<Integer, Integer>   tm  = new TreeMap<Integer, Integer>();
        String                      str = "id=5 time=44, id=6 time=9, id=3 time=43, id=1 time=29,id=7 time=10, id=6 time=9,id=2 time=33, id=6 time=55";
        Pattern                     P   = Pattern.compile("id=(\\d+) time=(\\d+)");
        Matcher                     m   = P.matcher(str);
        while (m.find())                    tm.put(new Integer(m.group(1)), new Integer(m.group(2)));
        for (Integer id : tm.keySet())      System.out.println("Key: " + id.intValue() + "\tTime:\t" + tm.get(id.intValue()).intValue());
    }
}

生成此输出:

rtorello75@cloudshell:~ (webpublish-177600)$ java Queue
Key: 1  Time:   29
Key: 2  Time:   33
Key: 3  Time:   43
Key: 5  Time:   44
Key: 6  Time:   55
Key: 7  Time:   10

扫码关注云+社区

领取腾讯云代金券