首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何为java中的字符串队列分配字符串?

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

Stack Overflow用户
提问于 2018-10-23 01:17:13
回答 1查看 0关注 0票数 0

有一个字符串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做到这一点?

EN

回答 1

Stack Overflow用户

发布于 2018-10-23 10:35:02

这个解决方案

代码语言:javascript
复制
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());
    }
}

生成此输出:

代码语言:javascript
复制
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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/-100002965

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档