面试题 01

259 阅读1分钟

/** * 计算一个数组data[] 数字中的数字在0,9999之间,计算数组中的数字落在[0,9],[10,19],[20,29].....[9990,9999]区间,每个区间的落多少个数 * @param arrayList * @return */

public static HashMap demo(ArrayList<Integer> arrayList) {
    HashMap<Integer, Integer> hashMap = new HashMap(Math.pow(2,14));
    for (int i = 0; i < 1000; i++) {
        hashMap.put(i, 0);
    }

    arrayList.forEach((a) -> {
        if (a.toString().length() == 1) {
            hashMap.put(0, hashMap.get(0) + 1);
        }
        if (a.toString().length() > 1) {
            int temp = Integer.valueOf(a.toString().substring(0, a.toString().length() - 1));
            hashMap.put(temp, hashMap.get(temp) + 1);
        }
    });
    hashMap.forEach((a, b) -> System.out.println("区间为[" + (a * 10 + 0) + ":" + (a * 10 + 9) + "],value is " + b));
    return hashMap;
}