在 Java 中对数组中的字符串进行分类和分组
Classifying and grouping strings in array in Java
我需要找出对字符串数组进行分类和分组的最佳方法。假设我有数组:
Resources[] = {tester1,tester2,solverC1,solverC2,solverS2,solverS1,tester3,tester4,system}
实际上它的字符串要多得多,但它就是这样的想法。
我需要得到答案,在这种情况下我有资源:
Resource: tester || Quantity: 4
Resource: system || Quantity: 1
Resource: solver || Quantity: 4
使用 Levenshtein 距离是最好的主意吗?
如果是,也许有些人有想法将数组分组并获取名称(没有数字或字母)并计算它们的数量?
您可以使用流进行映射:
String Resources[] =
new String[]{"tester1",
"tester2","solverC1","solverC2",
"solverS2","solverS1",
"tester3","tester4","system"};
Map<String, List<String>> groups = Arrays.asList(Resources)
.stream().collect(
Collectors.groupingBy(
t -> t.contains("tester") ? "tester" :
(t.contains("solver") ? "solver" : "system"),
Collectors.counting()));
这将 return 一张地图,结果如下:
{system=1, tester=4, solver=4}
可以使用以下方法使其更清洁:
private static String group(String t) {
return t.contains("tester") ? "tester" : (t.contains("solver") ? "solver" : "system");
}
因此可以使用以下方式构建收集器:
Collectors.groupingBy(Main::group, , Collectors.counting())
您可以使用 java 流来获得带有值的漂亮地图。例如:
List<String> res= Arrays.asList("tester1","tester2","solverC1","solverC2","solverS2","solverS1","tester1","tester4","system");
Map<String,Long> result=res.stream().collect( Collectors.groupingBy(s-> s.replaceAll("\d","").toString(), Collectors.counting()));
将保留以下值:system="1", tester="4", solverS="2", solverC="2"
我使用了仅从字符串中删除数字的 groupBy 函数,但您可以在此处定义您想要的任何规则:
Collectors.groupingBy(s-> s.replaceAll("\d","").toString()
取决于您是否希望 solverS 不同于 solverC
工作示例:
public class MainClass {
public static void main(String[] args) {
System.out.println(new Date()+": Let's start our Whosebug helper project!");
List<String> res= Arrays.asList("tester1","tester2","solverC1","solverC2","solverS2","solverS1","tester1","tester4","system");
Map<String,Long> reuslts=res.stream().collect( Collectors.groupingBy(s-> s.replaceAll("\d","").toString(), Collectors.counting()));
StringBuilder sb = new StringBuilder();
Iterator<Entry<String, Long>> iter = reuslts.entrySet().iterator();
while (iter.hasNext()) {
Entry<String, Long> entry = iter.next();
sb.append(entry.getKey());
sb.append('=').append('"');
sb.append(entry.getValue());
sb.append('"');
if (iter.hasNext()) {
sb.append(',').append(' ');
}
}
System.out.println(sb.toString());
}
}
// It should print system="1", tester="4", solverS="2", solverC="2"
我需要找出对字符串数组进行分类和分组的最佳方法。假设我有数组:
Resources[] = {tester1,tester2,solverC1,solverC2,solverS2,solverS1,tester3,tester4,system}
实际上它的字符串要多得多,但它就是这样的想法。 我需要得到答案,在这种情况下我有资源:
Resource: tester || Quantity: 4
Resource: system || Quantity: 1
Resource: solver || Quantity: 4
使用 Levenshtein 距离是最好的主意吗? 如果是,也许有些人有想法将数组分组并获取名称(没有数字或字母)并计算它们的数量?
您可以使用流进行映射:
String Resources[] =
new String[]{"tester1",
"tester2","solverC1","solverC2",
"solverS2","solverS1",
"tester3","tester4","system"};
Map<String, List<String>> groups = Arrays.asList(Resources)
.stream().collect(
Collectors.groupingBy(
t -> t.contains("tester") ? "tester" :
(t.contains("solver") ? "solver" : "system"),
Collectors.counting()));
这将 return 一张地图,结果如下:
{system=1, tester=4, solver=4}
可以使用以下方法使其更清洁:
private static String group(String t) {
return t.contains("tester") ? "tester" : (t.contains("solver") ? "solver" : "system");
}
因此可以使用以下方式构建收集器:
Collectors.groupingBy(Main::group, , Collectors.counting())
您可以使用 java 流来获得带有值的漂亮地图。例如:
List<String> res= Arrays.asList("tester1","tester2","solverC1","solverC2","solverS2","solverS1","tester1","tester4","system");
Map<String,Long> result=res.stream().collect( Collectors.groupingBy(s-> s.replaceAll("\d","").toString(), Collectors.counting()));
将保留以下值:system="1", tester="4", solverS="2", solverC="2"
我使用了仅从字符串中删除数字的 groupBy 函数,但您可以在此处定义您想要的任何规则:
Collectors.groupingBy(s-> s.replaceAll("\d","").toString()
取决于您是否希望 solverS 不同于 solverC
工作示例:
public class MainClass {
public static void main(String[] args) {
System.out.println(new Date()+": Let's start our Whosebug helper project!");
List<String> res= Arrays.asList("tester1","tester2","solverC1","solverC2","solverS2","solverS1","tester1","tester4","system");
Map<String,Long> reuslts=res.stream().collect( Collectors.groupingBy(s-> s.replaceAll("\d","").toString(), Collectors.counting()));
StringBuilder sb = new StringBuilder();
Iterator<Entry<String, Long>> iter = reuslts.entrySet().iterator();
while (iter.hasNext()) {
Entry<String, Long> entry = iter.next();
sb.append(entry.getKey());
sb.append('=').append('"');
sb.append(entry.getValue());
sb.append('"');
if (iter.hasNext()) {
sb.append(',').append(' ');
}
}
System.out.println(sb.toString());
}
}
// It should print system="1", tester="4", solverS="2", solverC="2"