Java 如何根据元素将重复元素和不同值的字符串的部分排序到不同的数组中?
我目前正在尝试根据元素(如freq、mag等)将以下字符串转换为几个数组。该字符串最初是json格式的,但使用Gson,它被转换为我正在使用的另一个类的对象,然后转换为字符串,以便更容易排序(对我来说)我正在寻找关于如何将其转换为字符串数组的建议,或者是否有其他更简单的方法来准备多个数组Java 如何根据元素将重复元素和不同值的字符串的部分排序到不同的数组中?,java,arrays,string,list,object,Java,Arrays,String,List,Object,我目前正在尝试根据元素(如freq、mag等)将以下字符串转换为几个数组。该字符串最初是json格式的,但使用Gson,它被转换为我正在使用的另一个类的对象,然后转换为字符串,以便更容易排序(对我来说)我正在寻找关于如何将其转换为字符串数组的建议,或者是否有其他更简单的方法来准备多个数组 [freq: 17.97 mag: 8.22 bw: 0.44 snr: 2.47 med: 3.33, freq: 9.96 mag: 4.06 bw: 0.41 snr: 2.31 med:
[freq: 17.97 mag: 8.22 bw: 0.44 snr: 2.47 med: 3.33, freq: 9.96 mag: 4.06 bw: 0.41 snr: 2.31 med: 1.76, freq: 23.05 mag: 0.92 bw: 0.41 snr: 2.24 med: 0.41, freq: 12.99 mag: 1.07 bw: 0.38 snr: 2.19 med: 0.49, freq: 7.18 mag: 18.54 bw: 0.47 snr: 2.15 med: 8.61]
理想情况下,我最终会得到如下结果:
frequencyArray[0] = 17.97
frequencyArray[1] = 9.96
frequencyArray[x] = [x]
等等
我已经清理了一些字符串,比如在一些值之后删除了额外的“,”
我已经清理了一些字符串,比如在一些值之后删除了额外的
“,”
。您可以尝试将其放入这样的映射中
我假设您的格式非常规则,两个空格分隔两个键值对,逗号分隔数据行
key1: x.xx key2: x.xx key3: x.xx, key1: x.xx key2: x.xx key3: x.xx ...
是
假设输入是这样定义的(忽略换行符,它们只是为了格式化,因为代码不换行)
虽然键的顺序没有保留(如果需要,可以使用LinkedHashMap
),但里面的数字顺序是
使用流的另一种方法:
Map<String, List<Double>> res = Arrays
.stream(input.split("[, ] "))
.map(s -> s.split(": "))
.collect(Collectors.groupingBy(s -> s[0]))
.entrySet().stream()
.collect(Collectors.toMap(
e -> e.getKey(),
e -> e.getValue()
.stream()
.map(ns -> Double.parseDouble(ns[1]))
.collect(Collectors.toList())));
你可以试着把它放到这样的地图上 我假设您的格式非常规则,两个空格分隔两个键值对,逗号分隔数据行
key1: x.xx key2: x.xx key3: x.xx, key1: x.xx key2: x.xx key3: x.xx ...
是
假设输入是这样定义的(忽略换行符,它们只是为了格式化,因为代码不换行)
虽然键的顺序没有保留(如果需要,可以使用LinkedHashMap
),但里面的数字顺序是
使用流的另一种方法:
Map<String, List<Double>> res = Arrays
.stream(input.split("[, ] "))
.map(s -> s.split(": "))
.collect(Collectors.groupingBy(s -> s[0]))
.entrySet().stream()
.collect(Collectors.toMap(
e -> e.getKey(),
e -> e.getValue()
.stream()
.map(ns -> Double.parseDouble(ns[1]))
.collect(Collectors.toList())));
地图是你的朋友
String str = "[freq: 17.97 mag: 8.22 bw: 0.44 snr: 2.47 med: 3.33, freq: 9.96 mag: 4.06 bw: 0.41 snr: 2.31 med: 1.76," +
" freq: 23.05 mag: 0.92 bw: 0.41 snr: 2.24 med: 0.41, freq: 12.99 mag: 1.07 bw: 0.38 snr: 2.19 med: 0.49, freq: 7.18 " +
" mag: 18.54 bw: 0.47 snr: 2.15 med: 8.61]\n";
List<String> stringList = Arrays.asList(str.split(" |,")); // split into pair key : value
Map<String, List<String>> map = new HashMap<>();
stringList.forEach(s1 -> {
String[] splitedStrings = s1.split(": "); //split into key : value
String key = splitedStrings[0].replaceAll("[^A-Za-z0-9]",""); // remove non alphanumeric from key, like {
String value = splitedStrings[1];
if (map.get(key) == null) {
List<String> values = new ArrayList<>();
values.add(value);
map.put(key, values);
}else if (map.get(key) != null) {
map.get(key).add(value);
}
});
System.out.println(map);
地图是你的朋友
String str = "[freq: 17.97 mag: 8.22 bw: 0.44 snr: 2.47 med: 3.33, freq: 9.96 mag: 4.06 bw: 0.41 snr: 2.31 med: 1.76," +
" freq: 23.05 mag: 0.92 bw: 0.41 snr: 2.24 med: 0.41, freq: 12.99 mag: 1.07 bw: 0.38 snr: 2.19 med: 0.49, freq: 7.18 " +
" mag: 18.54 bw: 0.47 snr: 2.15 med: 8.61]\n";
List<String> stringList = Arrays.asList(str.split(" |,")); // split into pair key : value
Map<String, List<String>> map = new HashMap<>();
stringList.forEach(s1 -> {
String[] splitedStrings = s1.split(": "); //split into key : value
String key = splitedStrings[0].replaceAll("[^A-Za-z0-9]",""); // remove non alphanumeric from key, like {
String value = splitedStrings[1];
if (map.get(key) == null) {
List<String> values = new ArrayList<>();
values.add(value);
map.put(key, values);
}else if (map.get(key) != null) {
map.get(key).add(value);
}
});
System.out.println(map);
您可以使用Map
存储所有数组。首先按,
拆分,然后按
拆分。必须进行清理和筛选,并使用索引知道将值放在何处
Map<Integer, Double[]> mapOfArrays = new HashMap<>();
String[] cleanAndSplitByComma = str.replace("]", "").split(",");
int arrayIndex = 0;
for (String line : cleanAndSplitByComma) {
int partIndex = 0;
for (String value : line.split(" ")) {
if (!value.isBlank() && !value.contains(":")) {
mapOfArrays.computeIfAbsent(partIndex,
c -> new Double[cleanAndSplitByComma.length]
)[arrayIndex] = Double.valueOf(value);
partIndex++;
}
}
arrayIndex++;
}
您可以使用Map
存储所有数组。首先按,
拆分,然后按
拆分。必须进行清理和筛选,并使用索引知道将值放在何处
Map<Integer, Double[]> mapOfArrays = new HashMap<>();
String[] cleanAndSplitByComma = str.replace("]", "").split(",");
int arrayIndex = 0;
for (String line : cleanAndSplitByComma) {
int partIndex = 0;
for (String value : line.split(" ")) {
if (!value.isBlank() && !value.contains(":")) {
mapOfArrays.computeIfAbsent(partIndex,
c -> new Double[cleanAndSplitByComma.length]
)[arrayIndex] = Double.valueOf(value);
partIndex++;
}
}
arrayIndex++;
}
这是否回答了您的问题?您可以更改字符串吗?因为如果可以,您可以只使用
.split()
否则我建议使用更复杂的正则表达式。这是否回答了您的问题?您可以更改字符串吗?因为如果可以,您可以只使用.split()
否则,我建议使用更复杂的正则表达式,我认为逗号用于分隔行,并且significant@user,我相信它们就在那里,这样我们就可以维持秩序。我已经做到了。我们在不同的数据结构中有相同的输出。我相信逗号是用来分隔行的,并且significant@user,我相信他们就在那里o我们可以维持秩序。我已经这样做了。我们在不同的数据结构中有相同的输出。顺便说一句,您可以使用double[]
而不是double[]
,尽管我想如果OP以后需要将其放入另一个方法中,使用盒装版本可能更安全。顺便说一句,您可以使用double[]
而不是Double[]
,虽然我想如果OP以后需要将其放入另一个方法中,使用盒装版本可能会更安全。非常感谢。我一直在修补它,效果相当不错。我不太精通map,所以要访问特定值,我应该使用哪些方法?(get等)只需使用map.get(keyHere)so map.get(“mag”),它就会返回值列表。如果您觉得我的答案有帮助,请将其标记为已回答:)非常感谢。我一直在修补它,效果相当不错。我对map不太熟悉,所以要访问特定值,我应该使用哪些方法?(get等)只需使用map.get(keyHere)和map.get(“mag”)即可返回值列表。如果您觉得我的答案有用,请将其标记为已回答:)
{mag=[8.22, 4.06, 0.92, 1.07, 18.54],
bw=[0.44, 0.41, 0.41, 0.38, 0.47],
snr=[2.47, 2.31, 2.24, 2.19, 2.15],
freq=[17.97, 9.96, 23.05, 12.99, 7.18],
med=[3.33, 1.76, 0.41, 0.49, 8.61]}
String str = "[freq: 17.97 mag: 8.22 bw: 0.44 snr: 2.47 med: 3.33, freq: 9.96 mag: 4.06 bw: 0.41 snr: 2.31 med: 1.76," +
" freq: 23.05 mag: 0.92 bw: 0.41 snr: 2.24 med: 0.41, freq: 12.99 mag: 1.07 bw: 0.38 snr: 2.19 med: 0.49, freq: 7.18 " +
" mag: 18.54 bw: 0.47 snr: 2.15 med: 8.61]\n";
List<String> stringList = Arrays.asList(str.split(" |,")); // split into pair key : value
Map<String, List<String>> map = new HashMap<>();
stringList.forEach(s1 -> {
String[] splitedStrings = s1.split(": "); //split into key : value
String key = splitedStrings[0].replaceAll("[^A-Za-z0-9]",""); // remove non alphanumeric from key, like {
String value = splitedStrings[1];
if (map.get(key) == null) {
List<String> values = new ArrayList<>();
values.add(value);
map.put(key, values);
}else if (map.get(key) != null) {
map.get(key).add(value);
}
});
System.out.println(map);
{mag=[8.22, 4.06, 0.92, 1.07, 18.54],
bw=[0.44, 0.41, 0.41, 0.38, 0.47],
snr=[2.47, 2.31, 2.24, 2.19, 2.15],
freq=[17.97, 9.96, 23.05, 12.99, 7.18],
med=[3.33, 1.76, 0.41, 0.49, 8.61]
]}
Map<Integer, Double[]> mapOfArrays = new HashMap<>();
String[] cleanAndSplitByComma = str.replace("]", "").split(",");
int arrayIndex = 0;
for (String line : cleanAndSplitByComma) {
int partIndex = 0;
for (String value : line.split(" ")) {
if (!value.isBlank() && !value.contains(":")) {
mapOfArrays.computeIfAbsent(partIndex,
c -> new Double[cleanAndSplitByComma.length]
)[arrayIndex] = Double.valueOf(value);
partIndex++;
}
}
arrayIndex++;
}
Double[] frequencyArray = mapOfArrays.get(0);
mapOfArrays.values().stream()
.map(Arrays::asList)
.forEach(System.out::println);
[17.97, 9.96, 23.05, 12.99, 7.18]
[8.22, 4.06, 0.92, 1.07, 18.54]
[0.44, 0.41, 0.41, 0.38, 0.47]
[2.47, 2.31, 2.24, 2.19, 2.15]
[3.33, 1.76, 0.41, 0.49, 8.61]