Map习题

题目说明

// 数据说明
/*第一列: 编号   id第二列: 姓名   name第三列: 所属地  location第四列: 性别    sex第五列: 出生年   birth第六列: 去世年   death第七列: 武力值   strength
*/
String data = "1\t阿会喃\t云南\t男\t190\t225\t74\n" +"2\t伊籍\t江陵\t男\t162\t226\t24\n" +"3\t尹赏\t天水\t男\t194\t260\t44\n" +"4\t尹默\t梓潼\t男\t183\t239\t15\n" +"5\t于禁\t陈留\t男\t159\t221\t78\n" +"6\t卫瓘\t晋阳\t男\t220\t291\t46\n" +"7\t袁胤\t汝南\t男\t163\t199\t14\n" +"8\t袁熙\t汝南\t男\t176\t207\t51\n" +"9\t阎行\t长安\t男\t159\t222\t84\n" +"10\t阎柔\t北平\t男\t168\t227\t57\n" +"11\t袁术\t宛\t男\t155\t199\t65\n" +...+"830\t刘氏\t洛阳\t女\t164\t205\t8\n";

要求:

  1. 将数据封装至 Hero 对象, 多个 Hero 对象放入 List 集合
  2. 将集合中的数据按寿命高低排序(即death-birth), 找到寿命最高的武将
  3. 按集合中数据按武力值排序, 找到武力最高的武将
  4. 找到男性武力最低的武将
  5. 找到女性武力最高的武将
  6. 找到武力最高的前10名武将

可以使用传统集合求解,也可以使用 stream api 求解

题解

MainApp.java

import map.ex.Role;
import java.util.*;
public class MianApp {public static void main(String[] args) {String data = "1\t阿会喃\t云南\t男\t190\t225\t74\n" +"2\t伊籍\t江陵\t男\t162\t226\t24\n" +"3\t尹赏\t天水\t男\t194\t260\t44\n" +"4\t尹默\t梓潼\t男\t183\t239\t15\n" +"5\t于禁\t陈留\t男\t159\t221\t78\n" +"6\t卫瓘\t晋阳\t男\t220\t291\t46\n" +..."828\t卞氏\t许昌\t女\t160\t230\t24\n" +"829\t步练师\t\t女\t185\t238\t10\n" +"830\t刘氏\t洛阳\t女\t164\t205\t8\n";List<Role> rolesObject = setRolesObjects(data);Role deathBirth = findDeathBirth(rolesObject);System.out.println("寿命最高的武将为:" + deathBirth.getName() + "   寿命为:" + (Integer.parseInt(deathBirth.getDeath()) - Integer.parseInt(deathBirth.getBirth())) );Role strenght = findstrenght(rolesObject);System.out.println("武力值最高的武将为:" + strenght.getName() + "   武力值为:" + Integer.parseInt(strenght.getStrength()) );System.out.println("武力值最高的男武将信息为:");findManOrWomanStrenght(rolesObject, "男");System.out.println("武力值最高的女武将信息为:");findManOrWomanStrenght(rolesObject, "女");findFirstStrenghtNum(rolesObject, 10);}// 1. 将数据封装至 Hero 对象, 多个 Hero 对象放入 List 集合public static List<Role> setRolesObjects(String rolesString){List<String> role = Arrays.asList(rolesString.split("\n"));   // 数据拆分为各个角色信息List<List<String>> roles= new ArrayList<>();                     // 存入list<list>, 内为角色信息表for (String s : role) {roles.add(Arrays.asList(s.split("\t")));}List<Role> rolesObject = new ArrayList<>();for (List<String> rol : roles) {rolesObject.add(new Role(rol.get(0), rol.get(1), rol.get(2), rol.get(3), rol.get(4), rol.get(5), rol.get(6)));}return rolesObject;}// 2.将集合中的数据按寿命高低排序(即death-birth), 找到寿命最高的武将public static Role findDeathBirth(List<Role> rolesObject){Map<String, Integer> map = new HashMap<>();for (Role role : rolesObject) {map.put(role.getId(), Integer.parseInt(role.getDeath()) - Integer.parseInt(role.getBirth()));}Map<String, Integer> treemap = new TreeMap<>(new MyCompar1(map));treemap.putAll(map);// treemap.keySet().stream().findFirst().get(); 获取treemap中最大寿命的Role对象,也就是第一个对象//Optional<String> first = treemap.keySet().stream().findFirst();  first.get()可以得到Optional中的值// 得到角色对象,并返回return rolesObject.get(Integer.parseInt(treemap.keySet().stream().findFirst().get()) - 1);}// 3. 按集合中数据按武力值排序, 找到武力最高的武将public static Role findstrenght(List<Role> rolesObject){Map<String, Integer> map = new HashMap<>();for (Role role : rolesObject) {map.put(role.getId(), Integer.parseInt(role.getStrength()));}Map<String, Integer> treemap = new TreeMap<>(new MyCompar1(map));treemap.putAll(map);// 得到角色对象,并返回return rolesObject.get(Integer.parseInt(treemap.keySet().stream().findFirst().get()) - 1);}//4. 找到男/女性武力最低的武将public static void findManOrWomanStrenght(List<Role> rolesObject, String sex){Map<String, Integer> map = new HashMap<>();for (Role role : rolesObject) {if (role.getSex().equals(sex))map.put(role.getId(), Integer.parseInt(role.getStrength()));}Map<String, Integer> treemap = new TreeMap<>(new MyCompar1(map));treemap.putAll(map);// 得到角色对象,并返回for (Role role : rolesObject) {if (role.getId().equals(treemap.keySet().stream().findFirst().get())){System.out.println(role);}}//System.out.println(treemap.keySet().stream().findFirst().get());//return rolesObject.get(Integer.parseInt(treemap.keySet().stream().findFirst().get()) - 1);}// 6. 找到武力最高的前名武将public static  void findFirstStrenghtNum(List<Role> rolesObject, int num){Map<String, Integer> map = new HashMap<>();for (Role role : rolesObject) {map.put(role.getId(), Integer.parseInt(role.getStrength()));}Map<String, Integer> treemap = new TreeMap<>(new MyCompar1(map));treemap.putAll(map);System.out.println("武力值最高的前" + num + "人为:");int count = 0;for (Map.Entry<String, Integer> stringIntegerEntry : treemap.entrySet()) {if (count == num)break;System.out.println(rolesObject.get(Integer.parseInt(stringIntegerEntry.getKey()) -1));count++;}}/*** 比较器1 Value值比较* Value : Integer.parseInt(role.getDeath()) - Integer.parseInt(role.getBirth())*/static class MyCompar1 implements Comparator<String> {private Map<String, Integer> map;public MyCompar1(Map<String, Integer> map) {this.map = map;}@Overridepublic int compare(String key1, String key2) {if (map.get(key1).intValue() < map.get(key2).intValue()) {return 1;}if (map.get(key1).intValue() > map.get(key2).intValue()) {return -1;}return 0;}}
}

Role.java

public class Role {private String id;private String name;private String location;private String sex;private String birth;private String death;private String strength;public Role() {}public Role(String id, String name, String location, String sex, String birth, String death, String strength) {this.id = id;this.name = name;this.location = location;this.sex = sex;this.birth = birth;this.death = death;this.strength = strength;}public String getId() {return id;}public void setId(String id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getLocation() {return location;}public void setLocation(String location) {this.location = location;}public String getSex() {return sex;}public void setSex(String sex) {this.sex = sex;}public String getBirth() {return birth;}public void setBirth(String birth) {this.birth = birth;}public String getDeath() {return death;}public void setDeath(String death) {this.death = death;}public String getStrength() {return strength;}public void setStrength(String strength) {this.strength = strength;}@Overridepublic String toString() {return "Role{" +"id='" + id + '\'' +", name='" + name + '\'' +", location='" + location + '\'' +", sex='" + sex + '\'' +", birth='" + birth + '\'' +", death='" + death + '\'' +", strength='" + strength + '\'' +'}';}
}