这个程序实现的是 查找出现次数最多的数:import java.util.*;public class data {
public static void main (String[] args) {
    int max; 
    HashMap<String,Integer> hm = new HashMap<String,Integer>();
    ArrayList<String> list = new ArrayList<String>();
    
    for (int i=0;i<args.length;i++) {
 String s = args[i];
     if(hm.containsKey(s)) {
      hm.put(s, hm.get(s) + 1);   //hm.get(s)++;编译器说有问题,为什么呢?
 } else {
  hm.put(s,1);
  list.add(s);
 }
    }
 max=hm.get(list.get(0));
    for(int i = 0 ; (i+1)<list.size() ; i++) {
  if(hm.get(list.get(i)) <= hm.get(list.get(i+1))) {
      max = hm.get(list.get(i+1));
  }
    }    for(int i = 0 ; i<list.size() ; i++ ) {
  if(hm.get(list.get(i)) == max)
  System.out.println(list.get(i) + "  " + max + "次");
    }  
      
}}求高手帮我简化一下代码,总觉得我绕了很多弯路,我同学用C++写的也是用map(),代码比我的少了很多很多,我看他可以直接向数组一样使用map[i];另外他也直接用了map里的求最大值得方法,而我的这个程序还得自己写。还得用一个ArrayList来存储Key
。java里有没有对集合求MAX的方法呢?有没有对collection里的数据进行修改的方法呢?我看了一下API,不仅是map,连set,list里都没有对数据进行修改的方法,只有插入和删除,遍历。求高手帮帮忙,这是小弟第一次独立完成的一个程序,有什么好的算法,思路,方法都可以谢谢 

解决方案 »

  1.   

    //hm.get(s)++;编译器说有问题,为什么呢?hm.get(s)并不是一个变量
      

  2.   

    Map <String,Integer> hm = new HashMap <String,Integer>();
    int max = 0;
    String ret;
    for (String s : args) {
        Integer c = hm.get(s);
        Integer count = (c == null ? 1 : (c + 1));
        hm.put(s, count); 
        if (count > max) {
             max = count;
             ret = s;
        }
    }
    System.out.println(ret + "  " + max + "次"); 
      

  3.   

    楼主 我感觉 这段  
     for(int i = 0 ; (i+1) <list.size() ; i++) { 
      if(hm.get(list.get(i)) <= hm.get(list.get(i+1))) { 
          max = hm.get(list.get(i+1)); 
      } 
        } 
    有错误啊  应该 用MAX 去做比较吧
      

  4.   


    你再循环Map,值等于max的时候输出就好了么
      

  5.   

    额,想弄简单点结果貌似弄更复杂了……………… public static void main(String args[]) {
    int max;
    HashMap<String, Integer> hm = new HashMap<String, Integer>();
    TreeSet<Entry<String, Integer>> ts = new TreeSet<Entry<String, Integer>>(
    new Comparator<Entry<String, Integer>>() {
    public int compare(Entry<String, Integer> o1,
    Entry<String, Integer> o2) {
    if (o2.getValue() != o1.getValue())
    return o2.getValue() - o1.getValue();
    else
    return o1.getKey().compareTo(o2.getKey());
    }
    }); for (int i = 0; i < args.length; i++) {
    String s = args[i];
    if (hm.containsKey(s))
    hm.put(s, hm.get(s) + 1);
    else
    hm.put(s, 1);
    }

    Set<Entry<String, Integer>> entry = hm.entrySet();
    ts.addAll(entry); max = ts.first().getValue();
    Iterator<Entry<String, Integer>> iter = ts.iterator();
    Entry<String, Integer> temp;
    while ((temp = iter.next()).getValue() == max)
    System.out.println(temp.getKey() + " " + temp.getValue() + "次");
    }
      

  6.   

    修改了一下,依然很复杂 public static void main(String args[]) {
    int max;
    HashMap<String, Integer> hm = new HashMap<String, Integer>();
    ArrayList<Entry<String, Integer>> al = new ArrayList<Entry<String, Integer>>(); for (int i = 0; i < args.length; i++) {
    String s = args[i];
    if (hm.containsKey(s))
    hm.put(s, hm.get(s) + 1);
    else
    hm.put(s, 1);
    }

    Set<Entry<String, Integer>> entry = hm.entrySet(); max = 0;
    Iterator<Entry<String, Integer>> iter = entry.iterator();
    while (iter.hasNext()) {
    Entry<String, Integer> temp = iter.next();
    if (max < temp.getValue()) {
    max = temp.getValue();
    al.clear();
    al.add(temp);
    }
    else if (max == temp.getValue())
    al.add(temp);
    }

    for (Entry<String, Integer> temp : al) {
    System.out.println(temp.getKey() + " " + temp.getValue() + "次");
    }
    }
      

  7.   

    代码没看,你说找出最多的数的话,你不直接就a[i]++不就好了么?a[i]初始为0,然后在0~n查找一次...
      

  8.   


    public static void main(String[] args)
    {
    String [] args1={"1","11","3","2","2","11","11"};
    int max = 0;
    String key="";
    HashMap<String, Integer> hm = new HashMap<String, Integer>();
    for (int i = 0; i < args1.length; i++)
    {
    String s = args1[i];
    if (hm.containsKey(s))
    {
    hm.put(s, hm.get(s)+1); // hm.get(s)++;编译器说有问题,为什么呢?
    // ++是变量的单目运算符。hm.get(s)是一个值。不是变量。
    if(max<hm.get(s))
    {
    max=hm.get(s);
    key=s;
    }
    }
    else
    {
    hm.put(s, 1);
    }
    }
    System.out.println("key:"+key+"\nmax:"+max);
    }
      

  9.   

    hm.put(s, hm.get(s) + 1);  //hm.get(s)++;编译器说有问题,为什么呢? 
    hm.get(s) 这个方法得到的是Integer对象,不是变量。
      

  10.   

    lou主 特别有才,我关注。
      

  11.   

    出现次数最多的字符串可能有多个的情况
    针对这个问题,可不可以先定义一个ArrayList用于存放出现次数最多的字符串,也就是键值,在往Hashmap中插入数据的时候,判断一下,假如说value值一样的,往ArrayList里add添入就行了;假如说大于得话,直接用clear()方法,再add就行了最后直接打印出ArrayList就行了不知道这个和我以前那个哪个开销更小些呢
      

  12.   

     hm.put(s, hm.get(s) + 1);  //hm.get(s)++;编译器说有问题,为什么呢? 
    先把hm.get(s) 转换成int 然后再计算
      

  13.   


    对集合球MAX?没明白啥意思。
    对Collection里的数据进行修改,例如
    HashMap<String, String> map = new HashMap<String, String>();
    map.add("key", "bbb");
    String value = map.get("key");
    value += "ccc";
    String value2 = map.get("key");那么value2中的值就会是"bbbccc",这是Java的引用类型的特点。。
      

  14.   


    这个就是我8楼的做法
    7楼的做法是把所有的entry扔进一个TreeSet按value降序排序,排序方法自己写好,然后输出和第一个value相同的那些元素
      

  15.   

    另外顶楼
    if(hm.get(list.get(i)) <= hm.get(list.get(i+1)))
    应该是
    if(max <= hm.get(list.get(i+1)))这里楼主的做法是错的
      

  16.   

    import java.util.HashMap;
    import java.util.Iterator;
    import java.util.Map;
    import java.util.Set;
    public class Main {    public static void main(String[] args)
        {
            Map<Integer,Integer> map = new HashMap<Integer, Integer>();
            int[] a = {1,2,3,3,5,5,5,5,6,6,7,9,11,11,11,13,13,13,13};
            int max = 0;
            int tmpCount = 0;
            for(int i = 0; i < a.length; i ++)
            {
                if(!map.containsKey(a[i]))
                {
                    map.put(a[i], 1);
                }
                else
                {   tmpCount = map.get(a[i]) + 1;
                    if(tmpCount > max)
                    {
                        max = tmpCount;
                    }
                    map.put(a[i], tmpCount);
                }
            }
            Set<Integer> keySet = map.keySet();
            Iterator<Integer> itr = keySet.iterator();
            while(itr.hasNext())
            {
                int tmp = itr.next();
                if(map.get(tmp) == max)
                {
                    System.out.println(tmp + "出现" + max + "次");
                }
            }
        }
        
        
    }
      

  17.   


    import java.util.*; public class data { 
    public static void main (String[] args) { 
        int max = 0; 
        int temp;
        HashMap<String, Integer> map = new HashMap<String, Integer>(); 
        
        for (int i=0;i <args.length;i++) { 
         String s = args[i]; 
         if(!map.containsKey(s)){
         map.put(s, 1);
         }else{
         temp =  (Integer)(map.get(s));
         map.put(s, temp+1);
         if(max<temp+1){
         max = temp+1;
         }
         }
        
        } 
        Iterator<String> list = map.keySet().iterator();
        while(list.hasNext()){
         String str = (String)list.next();
         if((map.get(str)).intValue() == max){
         System.out.println(str + ""+ max + "次");
         }
        }
    }
    } 总算写好啦!中间操作可绕死我了
      

  18.   

    这的确是个很经典的问题
    问题有如下2种:
    1.编写一个Java应用程序,使用RandomAccessFile流统计Hello.txt中的单词,要求如下: 
    (1)计算全文中共出现了多少个单词(重复的单词只计算一次); 
    (2)统计出有多少个单词只出现了一次; 
    (3)统计并显示出每个单词出现的频率,并将这些单词按出现频率高低顺序显示在一个TextArea中。 package test;import java.io.IOException;
    import java.io.RandomAccessFile;
    import java.util.ArrayList;
    import java.util.Collection;
    import java.util.Collections;
    import java.util.Comparator;
    import java.util.HashMap;
    import java.util.HashSet;
    import java.util.Iterator;
    import java.util.LinkedList;
    import java.util.List;
    import java.util.ListIterator;
    import java.util.Map;
    import java.util.Scanner;
    import java.util.Set;
    import java.util.SortedSet;
    import java.util.TreeMap;
    import java.util.TreeSet;
    import java.util.Map.Entry;public class usefulclass {    private static int singleCount = 0;//单独出现的次数
        public static void main(String[] args) throws IOException {
            RandomAccessFile raf = new RandomAccessFile("D:\\Hello.txt","r");
            Scanner scan = new Scanner(raf.getChannel());
            HashMap<String,Integer> cache = new HashMap<String,Integer>();
            while(scan.hasNext()){
                String word = scan.next();
                Integer count = cache.get(word);
                if(count==null)count=0;
                cache.put(word, count+1);
            }
            
            ArrayList<Map.Entry<String, Integer>> list = new ArrayList<Map.Entry<String, Integer>>(cache.entrySet());
            if(list.get(0).getValue()==1)singleCount++;
            Collections.sort(list, new Comparator<Map.Entry<String, Integer>>(){
                public int compare(Entry<String, Integer> o1, Entry<String, Integer> o2) {
                    if(o2.getValue()==1)singleCount++;
                    return -o1.getValue().compareTo(o2.getValue());
                }
            });
            System.out.println("一共出现了 "+list.size()+" 个单词");
            System.out.println("有 "+singleCount+" 个单词只出现了一次");
            for(Map.Entry<String, Integer> e : list){
                System.out.println(e.getKey()+"\t"+e.getValue());
            }
        }
    }
    2.4、 编写一个Java应用程序,使用RandomAccessFile流统计Hello.txt中的单词,要求如下: 
    (1)计算全文中共出现了多少个单词(重复的单词只计算一次); 
    (2)统计出有多少个单词只出现了一次; 
    (3)统计并显示出每个单词出现的频率,并将这些单词按出现频率高低顺序显示在一个TextArea中。 package test;import java.io.IOException;
    import java.io.RandomAccessFile;
    import java.util.ArrayList;
    import java.util.Collection;
    import java.util.Collections;
    import java.util.Comparator;
    import java.util.HashMap;
    import java.util.HashSet;
    import java.util.Iterator;
    import java.util.LinkedList;
    import java.util.List;
    import java.util.ListIterator;
    import java.util.Map;
    import java.util.Scanner;
    import java.util.Set;
    import java.util.SortedSet;
    import java.util.TreeMap;
    import java.util.TreeSet;
    import java.util.Map.Entry;public class usefulclass {    private static int singleCount = 0;//单独出现的次数
        public static void main(String[] args) throws IOException {
            RandomAccessFile raf = new RandomAccessFile("D:\\Hello.txt","r");
            Scanner scan = new Scanner(raf.getChannel());
            HashMap<String,Integer> cache = new HashMap<String,Integer>();
            while(scan.hasNext()){
                String word = scan.next();
                Integer count = cache.get(word);
                if(count==null)count=0;
                cache.put(word, count+1);
            }
            
            ArrayList<Map.Entry<String, Integer>> list = new ArrayList<Map.Entry<String, Integer>>(cache.entrySet());
            if(list.get(0).getValue()==1)singleCount++;
            Collections.sort(list, new Comparator<Map.Entry<String, Integer>>(){
                public int compare(Entry<String, Integer> o1, Entry<String, Integer> o2) {
                    if(o2.getValue()==1)singleCount++;
                    return -o1.getValue().compareTo(o2.getValue());
                }
            });
            System.out.println("一共出现了 "+list.size()+" 个单词");
            System.out.println("有 "+singleCount+" 个单词只出现了一次");
            for(Map.Entry<String, Integer> e : list){
                System.out.println(e.getKey()+"\t"+e.getValue());
            }
        }
    }
      

  19.   

      // hm.put(s, hm.get(s) + 1);  //hm.get(s)++;编译器说有问题,为什么呢? 上面可以分开写
      int i = hm.get(s).intValue() + 1;
      hm.put(s,Integer.valueOf(i));