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;
}
}

 

 

转载于:https://www.cnblogs.com/xww115/p/10537720.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值