用于计算数组条目的Java HashMap

时间:2014-04-14 05:06:36

标签: java arrays hashmap

如何使用HashMap计算此数组中唯一条目的数量:

import java.util.Scanner;

public class Entry {

    private Scanner scan = new Scanner(System.in);


public void userInput(){
Scanner scan = new Scanner(System.in);
System.out.println("Please tell me the amount of numbers you will be entering (Up to 50): ");
        int [] arr = new int[scan.nextInt()];      

        for(int i=0;i<arr.length; i++) {
            scan = new Scanner(System.in);
            System.out.println("Please enter a number: ");
            while(scan.hasNextInt()){
            int x = scan.nextInt();
            arr[i]= x;
            break;
            }
        }        

         System.out.println(" ");
         System.out.println("Results of Data Entry");
         System.out.println("_____________________");
         for(int i:arr){System.out.println(i + " occurs "); 
        }
} 
}

我想输出以显示输入的数字以及输入的次数。

所以,如果我输入1 5 6 6 7

会显示

1 occurs 1
5 occurs 1
6 occurs 2
7 occurs 1

3 个答案:

答案 0 :(得分:4)

List asList = Arrays.asList(array);
Set<String> mySet = new HashSet<String>(asList);
for(String s : mySet) {
    System.out.println(s + " " + Collections.frequency(asList, s));
}

答案 1 :(得分:1)

countOccurences的辅助函数如下:

public static Map<Integer, Integer> countOccurences (int[] arr) {
    int len = arr.length;
    Map<Integer, Integer> map = new HashMap<Integer, Integer>();

    for(int i = 0; i < len; i++) {
        int key = arr[i];
        if (map.containsKey(key)) {
            int value = map.get(key);
            map.put(key, value + 1);
        } else {
            map.put(key, 1);
        }
    }

    return map;
}

以上代码应该易于阅读,如果您需要解释,请告诉我。

完整的代码如下所示,请注意Hashmap的迭代方式:

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Scanner;

public class main {

    private Scanner scan = new Scanner(System.in);

    public static void main(String args[]) {
        userInput();
    }

    public static void userInput() {
        Scanner scan = new Scanner(System.in);
        System.out
                .println("Please tell me the amount of numbers you will be entering (Up to 50): ");
        int[] arr = new int[scan.nextInt()];

        for (int i = 0; i < arr.length; i++) {
            scan = new Scanner(System.in);
            System.out.println("Please enter a number: ");
            while (scan.hasNextInt()) {
                int x = scan.nextInt();
                arr[i] = x;
                break;
            }
        }

        System.out.println(" ");
        System.out.println("Results of Data Entry");
        System.out.println("_____________________");

        Map<Integer, Integer> counts = countOccurences (arr);

        Iterator itr = counts.entrySet().iterator();
        while (itr.hasNext()) {
            Map.Entry pairs = (Map.Entry)itr.next();
            System.out.println(pairs.getKey() + " occurs " + pairs.getValue());
        }
    }

    public static Map<Integer, Integer> countOccurences (int[] arr) {
        int len = arr.length;
        Map<Integer, Integer> map = new HashMap<Integer, Integer>();

        for(int i = 0; i < len; i++) {
            int key = arr[i];
            if (map.containsKey(key)) {
                int value = map.get(key);
                map.put(key, value + 1);
            } else {
                map.put(key, 1);
            }
        }

        return map;
    }
}

答案 2 :(得分:0)

试试这个..你会得到你想要的东西

'public static void main(String args[])
    {
        int cnt=1;
        Integer a;

        Integer arr[] = {1,2,2,3,1,4,5,3,3,3};
        Map hm = new HashMap<Integer, Integer>();
        for(int i=0;i<arr.length;i++)
        {
            a=(Integer) hm.put(arr[i], cnt);    
            if(a!=null)
            {
                hm.put(arr[i],a+1);
            }
        }
            Set set = hm.entrySet();
          // Get an iterator
            Iterator it = set.iterator();
          // Display elements
            while(it.hasNext()) {
             Map.Entry me = (Map.Entry)it.next();
             System.out.print(me.getKey() + " occurs ");
             System.out.println(me.getValue());
            }
        }'

Map接口的put(K,V)方法返回相同键的先前值,如果第一次设置了键,则返回null。