【问题标题】:Find a particular word in a string array of sentences and return the frequency of a particular word throughout the array在句子字符串数组中查找特定单词并返回特定单词在整个数组中的频率
【发布时间】:2021-06-22 15:24:37
【问题描述】:

输入为如下字符串数组,

{"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);

【问题讨论】:

  • 您如何跟踪 N 个时间戳的频率?在数据结构方面有什么限制?我会考虑将每个字符串解析为一个映射,并增加计数器。部分示例is found here

标签: java arrays string search frequency


【解决方案1】:

一种方法是跟踪条目的数量并增加。

    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}

现在,如果将来可能想要执行其他操作,那么使用对象可能会很有趣。

所以一个类可能是:

   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 个代码的数量或其他什么,则更改流是微不足道的,因为对象具有数据。当然,这种方法还有更多的扩展。

【讨论】:

    【解决方案2】:

    您可以使用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}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-30
      • 2022-11-17
      • 2021-12-11
      • 1970-01-01
      • 2023-03-15
      • 2019-03-14
      相关资源
      最近更新 更多