1.图论某班有 40 个学生,学号为 180201-180240, 全部参加 Java 集合阶段检测,给出所有同学的成绩 (可随机产生,范围为 50-100),请编写程序将本班 各位同学成绩从高往低排序打印输出, 注:成绩相同时学号较小的优先打印 要求:打印的信息包含学号、姓名(姓名统一为 “同学 J”[J=10,11,12。。。])和成绩 。
import java.util.*;
/**
* 描述:从大到小打印学生的:学号+姓名+成绩
*
* @Author administrator{GINO ZHANG}
* @Date2018/11/22
*/
public class JavaTest1 {
private static List data = new ArrayList();
static class Student{
private int id;
private String name;
private int score;
public Student(int id,String name,int score){
this.id = id;
this.name = name;
this.score = score;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public int getScore() {
return score;
}
public String toString() {
return "学号:" + id +
", 姓名:" + name +
", 成绩:" + score ;
}
}
public static void initData(){
int j = 10;
Random random = new Random();
for (int i = 0;i < 40;++i){
data.add(new Student(180201+i,"同学"+(i+j),random.nextInt(51)+50));
}
}
public static void adjust(){
PriorityQueue queue = new PriorityQueue(11, new Comparator() {
@Override
public int compare(Student o1, Student o2) {
if (o1.getScore() == o2.getScore()) {
return o1.getId() - o2.getId();
}
return o2.getScore() - o1.getScore();
}
});
Iterator iterator1 = data.iterator();
while (iterator1.hasNext()){
queue.add(iterator1.next());
}
for (int i = 0; i < 40; i++) {
System.out.println(queue.remove());
}
/**Iterator iterator2 = queue.iterator();
while (iterator2.hasNext()){
Student next1 = iterator2.next();
int id = next1.getId();
String name = next1.getName();
int score = next1.getScore();
System.out.println("学号:"+id+" 姓名:"+name+" 成绩:"+score);
}*/
}
public static void print(){
Iterator iterator = data.iterator();
while (iterator.hasNext()){
Student next1 = iterator.next();
int id = next1.getId();
String name = next1.getName();
int score = next1.getScore();
System.out.println("学号:"+id+" 姓名:"+name+" 成绩:"+score);
}
}
public static void main(String[] args) {
JavaTest javaTest = new JavaTest();
initData();
adjust();
}
}
打印结果:
C:\java\java7\jdk1.7.0_80\bin\java.exe -javaagent:D:\ideaIU-2018.1.5.win\lib\idea_rt.jar=61408:D:\ideaIU-2018.1.5.win\bin -
学号:180209, 姓名:同学18, 成绩:100
学号:180224, 姓名:同学33, 成绩:100
学号:180238, 姓名:同学47, 成绩:100
学号:180211, 姓名:同学20, 成绩:98
学号:180212, 姓名:同学21, 成绩:98
学号:180218, 姓名:同学27, 成绩:96
学号:180237, 姓名:同学46, 成绩:94
学号:180210, 姓名:同学19, 成绩:92
学号:180206, 姓名:同学15, 成绩:91
学号:180234, 姓名:同学43, 成绩:91
学号:180240, 姓名:同学49, 成绩:91
.
.
.
Process finished with exit code 0
2.有两个有序的集合,集合的每个元素都是一段范围, 求其交集,例如交集{[4,8],[9,13]}和{[6,12]}的交集是 {[6,8],[9,12]}
/**
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
/**
* 描述:就两个集合的交集
*
* @Author administrator{GINO ZHANG}
* @Date2018/11/21
*/
public class CollectsTest {
private static boolean isVaild(List> collects) {
if (collects.size() < 1) {
System.err.println("两集合无交集");
return false;
}
Iterator> iterator = collects.iterator();
while (iterator.hasNext()) {
List list = iterator.next();
if (list.size() != 2) {
System.err.println("集合区间不合法");
return false;
}
if (list.get(0) > list.get(1)) {
System.err.println("集合区间非有序");
return false;
}
}
return true;
}
public static void intersectionWithColletion(List> collects1, List> collects2) {
if (!(isVaild(collects1) && isVaild(collects2))) {
return;
}
Iterator > iterator1 = collects1.iterator();
while (iterator1.hasNext()) {
List list1 = iterator1.next();
Iterator > iterator2 = collects2.iterator();
while (iterator2.hasNext()) {
List list2 = iterator2.next();
intersection(list1, list2);
}
}
}
private static void intersection(List list1, List list2) {
Integer a1 = list1.get(0);
Integer a2 = list1.get(1);
Integer b1 = list2.get(0);
Integer b2 = list2.get(1);
if (a1 > b2 || b1 > a2) {
return;
}
if (a1 == b2) {
System.out.println("["+a1+"]");
}
if (a2 == b1) {
System.out.println("["+a2+"]");
}
if (a1 >= b1) {
System.out.print("[" + a1 + ",");
if (b2 < a2) {
System.out.println(b2 + "]");
} else {
System.out.println(a2 + "]");
}
}
if (b1 >= a1) {
System.out.print("["+b1+",");
if (b2 < a2) {
System.out.println(b2 + "]");
} else {
System.out.println(a2 + "]");
}
}
}
public static void main(String[] args) {
ArrayList> a = new ArrayList>();
ArrayList> b = new ArrayList>();
ArrayList arrayList1 = new ArrayList();
arrayList1.add(4);
arrayList1.add(8);
ArrayList arrayList2 = new ArrayList();
arrayList2.add(9);
arrayList2.add(13);
ArrayList arrayList3 = new ArrayList();
arrayList3.add(6);
arrayList3.add(12);
a.add(arrayList1);
a.add(arrayList2);
b.add(arrayList3);
intersectionWithColletion(a,b);
}
}
打印结果:
C:\java\java7\jdk1.7.0_80\bin\java.exe -javaagent:D:\ideaIU-2018.1.5.win\lib\idea_rt.jar=61449:D:\ideaIU-2018.1.5.win\bin -
[6,8]
[9,12]
Process finished with exit code 0
3.一个列表中记录了 1000 条数据[数据内容需 自己初始化产生]。数据内容为部分用户的银行卡 消费记录,消费记录信息包含([姓名] [消费金额]), 找出消费次数最多的用户,并且该用户单笔消费 最高的金额是多少?
例:列表内容格式
张三,666
李四,7893
张三,9999
获取结果则为:张三消费次数最多,最高消费
金额为:9999
import java.util.*;
/**
* 描述:从随机一千组数据中找出消费记录最多的人,和他的最高的消费额
*
* @Author administrator{GINO ZHANG}
* @Date2018/11/21
*/
public class JavaTest3 {
static class Person {
private String name;
private int money;
public Person(String name, int money) {
this.name = name;
this.money = money;
}
public String getName() {
return name;
}
public int getMoney() {
return money;
}
public void setMoney(int money) {
this.money = money;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", money=" + money +
'}';
}
}
private static ArrayList arrayList = new ArrayList<>();
public static void initData(List userList) {
Random random = new Random();
for (int i = 0; i < 1000; i++) {
arrayList.add(new Person(userList.get(random.nextInt(3)), random.nextInt(10000)));
}
}
public static void searchUser() {
HashMap hashMap = new HashMap<>();
Iterator iterator = arrayList.iterator();
while (iterator.hasNext()) {
Person person = iterator.next();
if (hashMap.containsKey(person.getName())) {
int value = hashMap.get(person.getName());
hashMap.put(person.getName(), value + 1);
} else {
hashMap.put(person.getName(), 1);
}
}
PriorityQueue> priorityQueue = new PriorityQueue<>(10, new Comparator>() {
@Override
public int compare(Map.Entry o1, Map.Entry o2) {
return o2.getValue() - o1.getValue();
}
});
Iterator> iterator1 = hashMap.entrySet().iterator();
while (iterator1.hasNext()) {
Map.Entry value = iterator1.next();
priorityQueue.add(value);
}
//消费次数最多的 最大一笔的金额
int maxMoney = 0;
Iterator iterator2 = arrayList.iterator();
while (iterator2.hasNext()) {
Person person = iterator2.next();
String name = priorityQueue.peek().getKey();
Integer money = person.getMoney();
while (person.getName().equals(name)) {
if (person.getMoney() > maxMoney) {
maxMoney = person.getMoney();
} else {
break;
}
}
}
System.out.println(priorityQueue.peek().getKey() + " 为消费次数最多的人" + ",最高消费金额为 :" + maxMoney);
}
public static void print() {
Iterator iterator = arrayList.iterator();
while (iterator.hasNext()) {
Person person = iterator.next();
System.out.println(person + " ");
}
}
public static void main(String[] args) {
ArrayList arrayList = new ArrayList<>();
arrayList.add("张三");
arrayList.add("李四");
arrayList.add("王五");
initData(arrayList);
print();
searchUser();
}
}
打印结果:
C:\java\java7\jdk1.7.0_80\bin\java.exe -javaagent:D:\ideaIU-2018.1.5.win\lib\idea_rt.jar=61476:D:\ideaIU-2018.1.5.win\bin -
Person{name='李四', money=8359}
Person{name='李四', money=1048}
Person{name='王五', money=3827}
Person{name='李四', money=2158}
Person{name='李四', money=553}
Person{name='王五', money=837}
Person{name='王五', money=2870}
Person{name='张三', money=9936}
.
.
.
张三 为消费次数最多的人,最高消费金额为 :9960