java学习之 【一道java题】识别出相同的组

来源:互联网 发布:酷我营销软件 编辑:程序博客网 时间:2024/05/12 07:34

String txet=“abc ac a abc a  cd ef e”

 

要求输出:abc  2

                 ac  1

                  a   2

 

我只是用容器中的HashMap做了一下,输出的结果形式上不一样,可能还有其他方法能正确实现。

 

package Map;/** * 探讨一道题目,使用容器中的HashMap做出来,比较有参考意义 *  */import java.util.*;public class HashMapTest {public static final int ONE = 1;public static final String text = "abc ac a ab cd  cd ef e";/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stub// split 对字符串进行截取,截取符合自己要求的子字符串,并将子字符串放入数组中String[] t = text.split(" ");Map m = new HashMap();for (int i = 0; i < t.length; i++) {System.out.print(t[i] + " ");}System.out.print("\n");System.out.println(t.length);for (int i = 0; i < t.length; i++) {int freq = (Integer) m.get(t[i]) == null ? ONE : (Integer) m.get(t[i]) + 1;m.put(t[i], freq);}System.out.println("HashMap 中共有" + m.size() + "对元素!");System.out.print(m);}}