zoukankan      html  css  js  c++  java
  • 10w数组去重,排序,找最多出现次数(精华)

    package cn.tedu.javaweb.test;

    import java.util.*;

    /*
    * @author XueWeiWei
    * @date 2019/6/11 8:19
    */
    @SuppressWarnings("SingleStatementInBlock")
    public class Ten {
    public static void main(String[] args) {
    int n = 100000;
    Integer[] arrays = new Integer[n];
    Map<Integer,Integer> map = new HashMap<Integer, Integer>();
    /**
    * 将10万个数放到map集合中:key是数,value是他的出现次数
    */
    for (int i = 0; i <n ; i++) {
    arrays[i] = new Random().nextInt(n);
    int num = 1;
    if (map.isEmpty()){
    map.put(arrays[i],num);
    }else if(map.containsKey(arrays[i])){
    num = map.get(arrays[i]);
    map.put(arrays[i],++num);
    }else{
    map.put(arrays[i],num);
    }
    }
    /**
    * 将value进行排序,找到最大值,也就是出现次数最大的那个随机数出现了多少次
    */
    Collection<Integer> collection = map.values();
    Object[] o = collection.toArray();
    Arrays.sort(o);
    System.out.println("出现次数最多的那个数出现了多少次: " + o[o.length-1] + "次");

    /**
    * 找到出现次数最多的随机数
    */
    System.out.println("找到出现次数最多的数:");
    for (Integer i:map.keySet()
    ) {
    if (map.get(i).equals(o[o.length-1])){
    System.out.print(i + " ");
    }
    }
    System.out.println();

    /**
    * 将随机数数组排序输出
    */
    System.out.println("将数组随机数排序输出");
    for (Integer i:map.keySet()
    ) {
    for (int j = 0; j < map.get(i); j++) {
    System.out.print(i + " ");
    }
    }

    }

    }
  • 相关阅读:
    FastMM、FastCode、FastMove的使用(图文并茂)
    12种JavaScript MVC框架之比较
    十款最佳Node.js MVC框架
    Couchbase 服务器
    C#程序员阅读的书籍
    ORM的实现
    Linux内核策略介绍
    ASP.NET MVC + EF 利用存储过程读取大数据
    面向.Net程序员的dump分析
    动态加载与插件化
  • 原文地址:https://www.cnblogs.com/xww115/p/11001956.html
Copyright © 2011-2022 走看看