1 import java.util.*; 2 import java.util.Map.Entry; 3 4 public class S22_7 { 5 6 public static void main(String[] args) { 7 // TODO Auto-generated method stub 8 9 Map<String,Integer> myMap = new TreeMap<String,Integer>(); 10 11 System.out.println("input integers: "); 12 Scanner input = new Scanner(System.in); 13 while(input.hasNext()){ 14 15 int str = input.nextInt(); 16 if(str != 0) 17 { 18 if(myMap.get(String.valueOf(str)) == null) 19 myMap.put(String.valueOf(str), 1); 20 else { 21 int value = myMap.get(String.valueOf(str)).intValue(); 22 value++; 23 myMap.put(String.valueOf(str),value); 24 } 25 } 26 else break; 27 } 28 input.close(); 29 System.out.println("result out :"); 30 Set<Map.Entry<String, Integer>> entrySet = myMap.entrySet(); 31 //find the max value of the entryset 32 int maxValue = 0; 33 for(Map.Entry<String, Integer> entry:entrySet) 34 if(maxValue < entry.getValue()) 35 maxValue = entry.getValue(); 36 else ; 37 for(Map.Entry<String, Integer> entry:entrySet) 38 System.out.println(entry.getKey() + " " + entry.getValue()); 39 System.out.println("the biggest times is :"); 40 for(Map.Entry<String, Integer> entry:entrySet) 41 if(entry.getValue() == maxValue) 42 System.out.println(entry.getKey()+" "+entry.getValue()); 43 else ; 44 } 45 }
刚开始用input.next() == "0" ,总是无法匹配,后来才发现==是用来检测字符串是否指向同一个对象,而无法比较字符串的内容。
如果需要比较字符串内容,应该使用equals(String)函数。如此,才能够通过0来阻断数据的持续录入。