package com.dashu; import java.util.ArrayList; import java.util.Comparator; import java.util.List; import java.util.PriorityQueue; import java.util.Queue; import java.util.Random; public class DaShu { static Comparator<Integer> cmp = new Comparator<Integer>() { public int compare(Integer e1, Integer e2) { return e2 - e1; } }; static Queue<Integer> sortQueue = new PriorityQueue<Integer>(10,cmp); public static void main(String[] args) throws Exception { while(true){ Thread.sleep(1000); sortQueue.add(random()); if(sortQueue.size() == 10){ List<Integer> list = new ArrayList<Integer>(); for(int i = 0;i<sortQueue.size();i++){ if(i == 0){ System.out.println("Max :"+sortQueue.poll()); } list.add(sortQueue.poll()); } sortQueue.addAll(list); System.out.println("Min: "+list.get(list.size() - 1)); System.out.println("------------------------------------"); } } } public static int random(){ int max=10000; int min=1; Random random = new Random(); return random.nextInt(max)%(max-min+1) + min; } }
-
Sandbox
431 引用 • 1250 回帖 • 599 关注
如果帖子标签含有 Sandbox ,则该帖子会被视为“测试帖”,主要用于测试社区功能,排查 bug 等,该标签下内容不定期进行清理。
相关帖子
-
package com.dashu; import java.util.ArrayList; import java.util.Comparator; import java.util.List; import java.util.PriorityQueue; import java.util.Queue; import java.util.Random; public class DaShu { static Comparator<Integer> cmp = new Comparator<Integer>() { public int compare(Integer e1, Integer e2) { return e2 - e1; } }; static Queue<Integer> sortQueue = new PriorityQueue<Integer>(10,cmp); public static void main(String[] args) throws Exception { while(true){ Thread.sleep(1000); sortQueue.add(random()); if(sortQueue.size() == 10){ List<Integer> list = new ArrayList<Integer>(); for(int i = 0;i<sortQueue.size();i++){ if(i == 0){ System.out.println("Max :"+sortQueue.poll()); } list.add(sortQueue.poll()); } sortQueue.addAll(list); System.out.println("Min: "+list.get(list.size() - 1)); System.out.println("------------------------------------"); } } } public static int random(){ int max=10000; int min=1; Random random = new Random(); return random.nextInt(max)%(max-min+1) + min; } }
欢迎来到这里!
我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。
注册 关于