集合的应用
定义原始数组,里面放30个原始对象,属性有:年龄,性别,分数,学科,随机生成
- 1.通过自动判断把相同年龄放在同一个集合里面,然后在把这些数据放到一个集合里面
- 2.分数1.(90以上)2.(60-90)3.(60分以下)分别放在一个map中,再放在同一个集合里面
- 3.学科: 数学,英语,语文,分别放在3个map里面,分数从小到大。再放在同一个集合里面
public class CollectionExercises {
static Random random = new Random();
public static void main(String[] args){
// 存储数据的集合
List<StudentEntity> studentList = new ArrayList<>();
// 数组中随机生成30个对象,属性有:年龄、性别、分数、学科
for(int i=0;i<30;i++){
StudentEntity student = new StudentEntity();
student.setAge(getRandomAge(15, 19));
student.setSex(getRandomSex());
student.setScore(randomInt(101));
student.setSubject(getRandomSubject());
studentList.add(student);
}
System.out.println("======所有数据======");
System.out.println(studentList);
// 将年龄相同的对象放入同一个集合
Map<Integer, List<StudentEntity>> map = new HashMap<>();
for (StudentEntity stu : studentList){
// map是否包含此key,若已经包含则添加一个新的数字到对应value集合中
if (map.containsKey(stu.getAge())){
map.get(stu.getAge()).add(stu);
}else{
// map不包含此key,则重新创建一个新集合,并把这个数据添加进集合
// ,再把集合放到map中
List<StudentEntity> newStudentAgeList = new ArrayList<>();
newStudentAgeList.add(stu);
map.put(stu.getAge(), newStudentAgeList);
}
}
System.out.println("======年龄相同的数据======");
System.out.println(map);
// 分数1.(90以上)2.(60-90)3.(60分以下)分别放在一个map中(键是1,2,3,分别代表三个区间),再放在同一个集合里面
Map<String,Map<Object,StudentEntity>> scoreMap = new HashMap<>();
Map<Object,StudentEntity> score1 = new TreeMap<>(Collections.reverseOrder());
Map<Object,StudentEntity> score2 = new TreeMap<>(Collections.reverseOrder());
Map<Object,StudentEntity> score3 = new TreeMap<>(Collections.reverseOrder());
for(StudentEntity stu : studentList){
if(stu.getScore() >= 90){
score1.put(stu.getScore(),stu);
}
else if(stu.getScore() >= 60 && stu.getScore()<=90){
score2.put(stu.getScore(),stu);
}
else{
score3.put(stu.getScore(),stu);
}
}
scoreMap.put("90分以上",score1);
scoreMap.put("60-90分",score2);
scoreMap.put("60分以下",score3);
System.out.println("======分数1.(90以上)2.(60-90)3.(60分以下)======");
System.out.println(scoreMap);
// 学科: 数学,英语,语文,分别放在3个map里面,分数从小到大。再放在同一个集合里面
Map<String,Map<Double,StudentEntity>> subjectMap = new HashMap<>();
Map<Double,StudentEntity> chinese = new TreeMap<>();
Map<Double,StudentEntity> english = new TreeMap<>();
Map<Double,StudentEntity> math = new TreeMap<>();
for (StudentEntity stu : studentList){
if(stu.getSubject().equals("语文")){
chinese.put(stu.getScore(),stu);
}
else if(stu.getSubject().equals("英语")){
english.put(stu.getScore(),stu);
}
else if(stu.getSubject().equals("数学")){
math.put(stu.getScore(),stu);
}
}
subjectMap.put("语文",chinese);
subjectMap.put("英语",english);
subjectMap.put("数学",math);
System.out.println("======学科: 数学,英语,语文======");
System.out.println(subjectMap);
System.out.println(subjectMap.get("数学"));
}
/**
* 生成随机数
*/
public static int randomInt() {
return random.nextInt();
}
/**
* 生成随机数(最大值限制)
*/
public static int randomInt(int maxNum) {
return random.nextInt(maxNum);
}
/**
* 获取随机性别
*/
public static String getRandomSex(){
return randomInt(2)%2 == 0 ? "男" : "女";
}
/**
* 获取随机年龄
*/
public static int getRandomAge(int min, int max){
return min + random.nextInt(max-min);
}
/**
* 获取随机年龄(15-18)
*/
public static int getRandomAge(){
return getRandomAge(15, 19);
}
/**
* 获取随机学科
*/
public static String getRandomSubject(){
String[] subject = {"语文","数学","英语","物理","化学","生物","历史","地理","政治"};
return subject[randomInt(subject.length)];
}
}