【问题标题】:Counting the same Strings in List计算列表中的相同字符串
【发布时间】:2017-09-20 14:59:24
【问题描述】:

我是 Android 和 Java 编程的新手。我对这种方法有疑问。 我正在尝试计算相同的字符串出现了多少次。

例如:输入列表l,输出列表m

列表l

  • 字符串1
  • 字符串1
  • 字符串2
  • 字符串2
  • 字符串2

列表m

  • 2x 字符串1
  • 3x 字符串2

我的清单:

    List<String> l = new ArrayList<>();
    List<String> m = new ArrayList<>();

我的方法:

public String Generuj() {
    String generator = "";
    int x = 0;
    int j = 0;


    if (l.size() > 0) {
        m.add(l.get(0));
        for (int i = 1; i < l.size(); i++) {
            while (j < l.size() && m.get(m.size() - 1).equals(l.get(j))) {
                x++;
                j++;
            }
            m.add("\n" + x + "x " + l.get(i));
        }
    }
    for (int i = 0; i < m.size(); i++) {
        generator = generator.concat(m.get(i));
    }
    return generator;
}

提前感谢您的帮助。

【问题讨论】:

标签: java list


【解决方案1】:

修改OP的解决方案:(不带HashMap

public String Generuj() {
    String generator = "";
    int x = 0;

    while(l.size() > 0) {
        String someString=l.remove(0);
        x=0;
        for (int i = 0; i < l.size();) {
            if(l.get(i).equals(someString)){
                x++;
                l.remove(i);
            }else{
                i++;
            }
        }
        m.add("\n" + x + "x " + someString);
    }
    for (int i = 0; i < m.size(); i++) {
        generator = generator.concat(m.get(i));
    }
    return generator;
}

【讨论】:

    【解决方案2】:

    正如 wanpanman 所说,您可以使用 hashmap :

     private void countEachString(List<String> strings) {
            HashMap<String, Integer> stringCountMap = new HashMap<>();
            for (String string : strings) {
                if (!stringCountMap.containsKey(string)) stringCountMap.put(string, 0);
                stringCountMap.put(string, stringCountMap.get(string) + 1);
            }
            print(stringCountMap);
        }
    
        private void print(HashMap<String, Integer> hashMap) {
            for (String string : hashMap.keySet()) {
                Log.d("print out", hashMap.get(string) + " x " + string);
            }
        }
    

    您可以根据需要更改打印内容

    【讨论】:

      【解决方案3】:

      我建议使用HashMap&lt; String, Integer &gt;,而不是简单地添加x。通过这样做:

      HashMap< String, Integer > countMap = new HashMap<>();
      for( String s : l )
      {
         if( countMap.containsKey( s ) )
         {
            // Add one to the string's occurrence.
            countMap.put( s, countMap.get( s ) + 1 );
         }
         else
         {
            // Set the string's first occurrence.
            countMap.put( s, 1 );
         }
      }
      

      您有输入列表中所有字符串的所有出现次数。如果您仍然需要打印结果,您可以遍历 HashMapEntrySet 并从那里连接:

      generator.concat( entry.getValue() + "x " + entry.getKey() );
      

      【讨论】:

        【解决方案4】:

        在 Java 8 中,您可以使用流轻松做到这一点:

        List<String> result = l.stream() // stream list l
            .collect(
                Collectors.groupingBy( // collect list elements to map grouping by keys and counting values
                    Function.identity(),
                    Collectors.counting()
                )
            )
            .entrySet() // get map's entry set
            .stream() // and stream it
            .map(entry -> entry.getValue() + "x " + entry.getKey()) // map each map entry to result string
            .collect(Collectors.toList()); // collect the results to new list
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2018-05-12
          • 2015-03-12
          • 1970-01-01
          • 1970-01-01
          • 2017-06-16
          • 2020-07-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多