模拟使用map进行学生信息(学号、姓名、性别、年龄、专业)存储,key值是学生的学号,value值是对应学生的信息,要求此map中保存的数据根据学号自动升序排序。

模拟使用map进行学生信息(学号、姓名、性别、年龄、专业)存储,key值是学生的学号,value值是对应学生的信息,要求此map中保存的数据根据学号自动升序排序。

package com.zut.Nove;

import java.util.*;

public class MapStudent {

int no;
String name;
String sex;
int age;
String profession;

public MapStudent(int no, String name, String sex, int age, String profession) {
    this.no = no;
    this.name = name;
    this.sex = sex;
    this.age = age;
    this.profession = profession;
}

@Override
public String toString() {
    return "MapStudent{" +
            "no=" + no +
            ", name='" + name + '\'' +
            ", sex='" + sex + '\'' +
            ", age=" + age +
            ", profession='" + profession + '\'' +
            '}';
}



public static void main(String[] args) {
    MapStudent stu1=new MapStudent(18,"张三","男",18,"数学");
    MapStudent stu2=new MapStudent(20,"李四","女",20,"语文");
    MapStudent stu3=new MapStudent(8,"王五","男",30,"英语");
    Map<Integer,MapStudent>map=new HashMap<Integer,MapStudent>();
    map.put(1,stu1);
    map.put(2,stu2);
    map.put(3,stu3);
    //遍历循环输出map中的数据
    map.forEach((integer, mapStudent) -> System.out.println(integer+" "+mapStudent));
    System.out.println("根据学号自动升序排序");

在t这里插入图片描述

   //List中不可直接写List <Map<Integer,MapStudent>>list=new <Map<Integer,MapStudent>> LinkedList();需要用entrySet方法
   
    List <Map.Entry<Integer,MapStudent>>list=new<Map.Entry<Integer,MapStudent>>LinkedList(map.entrySet());
    Collections.sort(list, new Comparator<Map.Entry<Integer, MapStudent>>() {
        @Override
        public int compare(Map.Entry<Integer, MapStudent> o1, Map.Entry<Integer, MapStudent> o2) {
            return o1.getValue().no-o2.getValue().no;
        }
    });
    //遍历输出数据
    list.forEach(integerMapStudentEntry -> System.out.println(integerMapStudentEntry));

}

}

  • 0
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值