将对象作为HashMap中的键值并在jsp中使用

时间:2018-01-23 13:09:31

标签: java list jsp arraylist hashmap

@Entity
@Table (name = "lectureHall_details")
public class LectureHall {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY  )
private int id;
private String Name;
private String code;
private String description;
private int capacity;
.......
}

这就是我的LectureHall类的样子。

@Entity
@Table(name = "timeTable_details")
public class TimeTable {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY  )
private int id;
@ManyToOne
private LectureHall lectureHall;
@ManyToOne
private Department department;
.....
}

这就是我的TimeTable类的样子。我想将Map的键值作为LectureHall类属性Name。

@RequestMapping(value = {"view/{day}/lecturehalls"})
public ModelAndView viewLectureHalls(@PathVariable("day") String day) {

    List<TimeTable> lectureHalls = timeTableDao.getLectureHallsList(day);
    Map<LectureHall, List<TimeTable>> byHall = lectureHalls.stream()
                                                           .collect(Collectors.groupingBy(TimeTable::getLectureHall));

    ModelAndView mv = new ModelAndView("page");
    mv.addObject("title","TimeTable");
    mv.addObject("mondayTime",byHall);
    mv.addObject("userclickviewlecturehallarrangements",true);
    return mv;
}

我能为此做些什么?

1 个答案:

答案 0 :(得分:0)

如果我正确地解释了你的问题,你想知道如何从LectureHalls到Strings创建一个HashMap。这将允许您将大厅的名称存储为地图条目中的值。以下是如何声明和实例化此地图:

Map<LectureHall, String> nameMap = new HashMap();

这回答了你的问题吗?