在句子的字符串数组中查找特定单词,并 return 特定单词在整个数组中的出现频率
Find a particular word in a string array of sentences and return the frequency of a particular word throughout the array
输入的字符串数组如下,
{"1112323 400 错误","1112323 400 错误","9988778 400 错误"}
我需要打印时间戳,即句子开头的数字及其在整个数组中的出现频率
到目前为止我只走了这么远。只有已知字符串才能找到它。
int count = 0;
for(int i=str1.length-1;i>=0;i--)
{
String[] ElementOfArray = str1[i].split(" ");
for(int j=0;j<ElementOfArray.length-1;j++)
{
if(ElementOfArray[j].equals("Hi"))
{
count++;
}
}
}
System.out.println(count);
一种方法是跟踪条目数并递增。
public static void main(String[] args)
{
String[] inp = {"1112323 400 error",
"1112323 400 error",
"9988778 400 error"};
Map<String,Integer> results = new HashMap<>();
for (String one : inp) {
String[] parts = one.split(" ");
String ts = parts[0];
int val = results.computeIfAbsent(ts, v-> 0);
results.put(ts, ++val);
}
System.out.println(results);
}
注意:还有其他方法可以处理地图递增。这只是一个例子。
示例输出:
{1112323=2, 9988778=1}
现在,如果将来可能想要执行其他操作,使用对象可能会很有趣。
所以 class 可能是:
private static class Entry
{
private final String ts;
private final String code;
private final String desc;
public Entry(String ts, String code, String desc)
{
// NOTE: error handling is needed
this.ts = ts;
this.code = code;
this.desc = desc;
}
public String getTs()
{
return ts;
}
public static Entry fromLine(String line)
{
Objects.requireNonNull(line, "Null line input");
// NOTE: other checks would be good
String[] parts = line.split(" ");
// NOTE: should verify the basic parts
return new Entry(parts[0], parts[1], parts[2]);
}
// other getter methods
}
然后可以做类似的事情:
List<Entry> entries = new ArrayList<>();
for (String one : inp) {
entries.add(Entry.fromLine(one));
}
Map<String,Integer> res2 = entries.stream()
.collect(Collectors.groupingBy(x->x.getTs(),
Collectors.summingInt(x -> 1)));
System.out.println(res2);
(目前相同的样本输出)。但是,如果需要扩展以计算 400 代码的数量或其他任何内容,更改流是微不足道的,因为对象具有数据。当然,这种方法还有更多的扩展。
您可以使用HashMap
来计算时间戳的频率。
import java.util.HashMap;
public class test {
public static void main(String[] args) {
// Create a HashMap object called timeFrequency
HashMap<String, Integer> timeFrequency = new HashMap<String, Integer>();
String []str1 = {"1112323 400 error","1112323 400 error","9988778 400 error"};
for(int i=0;i<str1.length;i++)
{
String[] ElementOfArray = str1[i].split(" ");
if(timeFrequency.containsKey(ElementOfArray[0])){
timeFrequency.put(ElementOfArray[0], timeFrequency.get(ElementOfArray[0]) + 1);
}else{
timeFrequency.put(ElementOfArray[0], 1);
}
}
System.out.println(timeFrequency);
}
}
Output:
{1112323=2, 9988778=1}
输入的字符串数组如下,
{"1112323 400 错误","1112323 400 错误","9988778 400 错误"}
我需要打印时间戳,即句子开头的数字及其在整个数组中的出现频率
到目前为止我只走了这么远。只有已知字符串才能找到它。
int count = 0;
for(int i=str1.length-1;i>=0;i--)
{
String[] ElementOfArray = str1[i].split(" ");
for(int j=0;j<ElementOfArray.length-1;j++)
{
if(ElementOfArray[j].equals("Hi"))
{
count++;
}
}
}
System.out.println(count);
一种方法是跟踪条目数并递增。
public static void main(String[] args)
{
String[] inp = {"1112323 400 error",
"1112323 400 error",
"9988778 400 error"};
Map<String,Integer> results = new HashMap<>();
for (String one : inp) {
String[] parts = one.split(" ");
String ts = parts[0];
int val = results.computeIfAbsent(ts, v-> 0);
results.put(ts, ++val);
}
System.out.println(results);
}
注意:还有其他方法可以处理地图递增。这只是一个例子。
示例输出:
{1112323=2, 9988778=1}
现在,如果将来可能想要执行其他操作,使用对象可能会很有趣。
所以 class 可能是:
private static class Entry
{
private final String ts;
private final String code;
private final String desc;
public Entry(String ts, String code, String desc)
{
// NOTE: error handling is needed
this.ts = ts;
this.code = code;
this.desc = desc;
}
public String getTs()
{
return ts;
}
public static Entry fromLine(String line)
{
Objects.requireNonNull(line, "Null line input");
// NOTE: other checks would be good
String[] parts = line.split(" ");
// NOTE: should verify the basic parts
return new Entry(parts[0], parts[1], parts[2]);
}
// other getter methods
}
然后可以做类似的事情:
List<Entry> entries = new ArrayList<>();
for (String one : inp) {
entries.add(Entry.fromLine(one));
}
Map<String,Integer> res2 = entries.stream()
.collect(Collectors.groupingBy(x->x.getTs(),
Collectors.summingInt(x -> 1)));
System.out.println(res2);
(目前相同的样本输出)。但是,如果需要扩展以计算 400 代码的数量或其他任何内容,更改流是微不足道的,因为对象具有数据。当然,这种方法还有更多的扩展。
您可以使用HashMap
来计算时间戳的频率。
import java.util.HashMap;
public class test {
public static void main(String[] args) {
// Create a HashMap object called timeFrequency
HashMap<String, Integer> timeFrequency = new HashMap<String, Integer>();
String []str1 = {"1112323 400 error","1112323 400 error","9988778 400 error"};
for(int i=0;i<str1.length;i++)
{
String[] ElementOfArray = str1[i].split(" ");
if(timeFrequency.containsKey(ElementOfArray[0])){
timeFrequency.put(ElementOfArray[0], timeFrequency.get(ElementOfArray[0]) + 1);
}else{
timeFrequency.put(ElementOfArray[0], 1);
}
}
System.out.println(timeFrequency);
}
}
Output:
{1112323=2, 9988778=1}