• 10w数组去重,排序,找最多出现次数


             配置在博客底部                                            

                                                                          主函数

    package ooDay11.zy13;

    import ooDay11.zy13.hanshu.GetKeyList;
    import ooDay11.zy13.hanshu.GetMaxValue;
    import ooDay11.zy13.hanshu.HashCun;
    import ooDay11.zy13.hanshu.Random_1000;

    import java.util.Arrays;
    import java.util.HashMap;
    import java.util.Iterator;
    import java.util.TreeSet;

    public class shuzu_0201HashMap {
    public static void main(String[] args){
    //随机生成100000个数
    Integer[] array = Random_1000.Random_1000(100000);
    //去重
    TreeSet<Integer> tset = new TreeSet<Integer>(Arrays.asList(array));
    Iterator i = tset.iterator();
    while (i.hasNext()){
    System.out.print(i.next() + " ");
    }
    System.out.println();
    System.out.println("------------------------------");
    System.out.println("最大次数");

    //将出现的数字和其出现的次数存入hashmap中
    HashMap<Integer,Integer> hashMap = HashCun.HashCun(array);
    //System.out.println(hashMap.entrySet());
    Object a = GetMaxValue.GetMaxValue(hashMap);
    System.out.println("其中数字出现次数最大的数的出现次数为"+a);
    int b = Integer.parseInt(String.valueOf(a));
    System.out.println("获取所有的最大出现次数的" + GetKeyList.GetKeyList(hashMap,b));

    }

    }



    得到出现次数最多的多个key值
    package ooDay11.zy13.hanshu;

    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    //这里用一个List列表来存储得到的key值,并返回改list列表
    //该函数里边传入的参数一个为hashmap,另外一个为最大的数(即出现次数最多的数的出现次数)
    //用for循环遍历(java5的新特性)
    public class GetKeyList {
    public static List<Integer> GetKeyList(HashMap<Integer,Integer> hashMap, int MaxValue){
    List<Integer> list = new ArrayList<>();
    for(Integer getKey : hashMap.keySet()){
    if (hashMap.get(getKey).equals(MaxValue)){
    list.add(getKey);
    }
    }
    return list;
    }
    }


    求出出现次数最多的出现次数
    package ooDay11.zy13.hanshu;

    import java.sql.Array;
    import java.util.Arrays;
    import java.util.Collection;
    import java.util.HashMap;
    import java.util.Map;
    //用collection来收集Map中的所有value值
    //接着将其转化为Object对象数组进行排序
    //最后返回最大的value
    public class GetMaxValue {
    public static Object GetMaxValue(Map<Integer,Integer> map){
    Collection<Integer> collection = map.values();
    Object[] array = collection.toArray();
    Arrays.sort(array);
    return array[array.length-1];
    }
    }

    将数组存入hashmap中
    package ooDay11.zy13.hanshu;

    import java.util.HashMap;
    //将数组存放在hashmap中,原因有两点:
    //1.hasMap具有两个值相当于二维数组
    //2.不用多次循环遍历查找,时间复杂度为1,节省时间
    public class HashCun {
    public static HashMap<Integer,Integer> HashCun(Integer[] array){
    HashMap<Integer,Integer> hashMap = new HashMap<>();
    int num = 1;
    for (int j = 0; j < array.length; j++) {
    if (hashMap.isEmpty()){
    hashMap.put(array[j],num);
    }else if (hashMap.containsKey(array[j])){
    int a = hashMap.get(array[j]);
    hashMap.replace(array[j],a+1);
    }else {
    hashMap.put(array[j],num);
    }
    }
    return hashMap;
    }
    }

    10万个数随机赋值
    package ooDay11.zy13.hanshu;
    //就是将一个数组随机赋值
    //并且其值在1-10000之间,用到了do-while循环
    //求求你让我过嘎嘎嘎嘎嘎过过过过过过过过过过过过过过过过过过过过过过过过过过过过过过过过过过过过过过过过过
    public class Random_1000 {
    public static Integer[] Random_1000(int n){
    Integer[] a = new Integer[n] ;
    for (int i = 0; i < n; i++) {
    do {
    a[i] = (int) (Math.random()*10000);
    }while (a[i] ==0);
    }

    return a;
    }
    }

    
    
  • 相关阅读:
    asp.net 、C#实现微信企业号OAuth2认证
    node event中 on emit off 的封装
    node
    Express中间件
    旋转的魔方
    通过gulp为requireJs引入的模块添加版本号
    css水平垂直居中(绝对定位居中)
    COLOR 与 COLORREF
    VMware Workstation 安装 vmware tools
    MMIV: starter level 1
  • 原文地址:https://www.cnblogs.com/xww115/p/10537720.html
Copyright © 2020-2023  润新知