如何在java中将列表解包到子列表
我通过以下代码获得了一个列表:如何在java中将列表解包到子列表,java,arraylist,Java,Arraylist,我通过以下代码获得了一个列表: ArrayList<String> single = new ArrayList<String>() - [Document{{packetsLost=0,id=ssrc_1848956494_recv, timestamp=2019-07-11T07:18:42.923Z}}, Document{{packetsLost=10, timestamp=2019-07-11T07:20:43.413Z}}] - [Document{{pac
ArrayList<String> single = new ArrayList<String>()
- [Document{{packetsLost=0,id=ssrc_1848956494_recv, timestamp=2019-07-11T07:18:42.923Z}}, Document{{packetsLost=10, timestamp=2019-07-11T07:20:43.413Z}}]
- [Document{{packetsLost=0, id=ssrc_1848956494_send, timestamp=2019-07-11T07:18:42.923Z}}, - Document{{packetsLost=10, timestamp=2019-07-11T07:20:43.413Z}}]
- [Document{{packetsLost=0,id=ssrc_929521404_recv, timestamp=2019-07-11T07:18:42.923Z}}, Document{{packetsLost=10, timestamp=2019-07-11T07:20:43.413Z}}]
- [Document{{packetsLost=0,id=ssrc_929521404_send, timestamp=2019-07-11T07:18:42.923Z}}, Document{{packetsLost=10, timestamp=2019-07-11T07:20:43.413Z}}]
我们可以利用此处的流生成两个列表:
List<String> packetsList = single.stream()
.map(x -> x.replaceAll(".*packetsLost=(\\d+).*", "$1"))
.collect(Collectors.toList());
List<String> timestampsList = single.stream()
.map(x -> x.replaceAll(".* timestamp=([^}]+).*", "$1"))
.collect(Collectors.toList());
List packetsList=single.stream()
.map(x->x.replaceAll(“.*packetsLost=(\\d+).*”,“$1”))
.collect(Collectors.toList());
List timestampsList=single.stream()
.map(x->x.replaceAll(“.*timestamp=([^}]+)*”,“$1”))
.collect(Collectors.toList());
此外,您的预期输出与输入不一致。这。。。似乎假定输入是字符串列表。我改变了我的问题,你可以帮我,但我帮不了你,因为你的新预期输出不能用字符串表示。你的问题忽略了我们需要再次帮助你的信息。
List<String> packetsList = single.stream()
.map(x -> x.replaceAll(".*packetsLost=(\\d+).*", "$1"))
.collect(Collectors.toList());
List<String> timestampsList = single.stream()
.map(x -> x.replaceAll(".* timestamp=([^}]+).*", "$1"))
.collect(Collectors.toList());