有一个字符串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做到这一点?
发布于 2018-10-23 10:35:02
这个解决方案
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
https://stackoverflow.com/questions/-100002965
复制相似问题