前言
集合的学习
一、List、Set、Map
二、例子:
public class Movie {
private String name;
private String type;
private Integer donoy;
public Movie() {
}
public Movie(String name, String type, Integer donoy) {
this.name = name;
this.type = type;
this.donoy = donoy;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public Integer getDonoy() {
return donoy;
}
public void setDonoy(Integer donoy) {
this.donoy = donoy;
}
@Override
public String toString() {
//选中假数据,双引号++
return "电影名称:"+this.name+"\n电影类型:"+this.type+"\n电影人气:"+this.donoy+"\n";
}
}
import java.util.*;
public class MovieManager {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
//创建容器存储所有电影
List<Movie> movieList = new ArrayList<>();
//创建容器存储电影类型
Map<Integer,String> movieTypeMap = new HashMap<>();
//定义变量存储key值
int count = 1;
for (;;) {
System.out.println("欢迎使用电影观看指南");
System.out.println("1:添加电影");
System.out.println("2:查看电影列表");
System.out.println("3:分类管理");
System.out.println("4:显示最高人气电影");
System.out.println("5:退出");
Integer num = input.nextInt();
switch (num){
case 1:
System.out.println("开始添加电影");
System.out.println("请输入电影名称");
String name = input.next();
System.out.println("请输入电影类型");
String type = input.next();
System.out.println("请输入电影人气");
Integer donny = input.nextInt();
Movie movie = new Movie(name,type,donny);
//将电影装入电影列表
movieList.add(movie);
//将电影类型装入Map集合,并且重复类型只存储一次(利用判断)
//判断Map中是否存在类型开关flag
boolean flag = movieTypeMap.containsValue(type);
if(!flag){
movieTypeMap.put(count,type);
count++;
}
break;
case 2:
System.out.println("查看电影列表");
movieList.forEach(temp->{
System.out.print(temp);
});
System.out.println("");
break;
case 3:
System.out.println("电影分类查询结果如下");
movieTypeMap.forEach((k,v)->{
System.out.println(k+"、"+v);
});
System.out.println("请输入查询电影类型");
Integer typeNum = input.nextInt();
String movieType = movieTypeMap.get(typeNum);
//循环遍历movieList找出符合movieType的电影
for(Movie temp:movieList){
if(movieType.equals(temp.getType())){
System.out.println(temp);
}
}
break;
case 4:
System.out.println("最高电影人气如下");
//循环找出最大值
Movie Max = movieList.get(0);
for (int i = 1; i < movieList.size(); i++) {
Movie movie1 = movieList.get(i);
if(Max.getDonoy()<movie1.getDonoy()){
Max = movie1;
}
}
System.out.println(Max);
break;
case 5:
return;
default:
System.out.println("请重新选择");
break;
}
}
}
}