身高比较

美团的题,这一题是给了两组数据,一组身高,一组姓名,按照身高升序排列,如果身高相同就按照姓名的字典序进行排序。
调用工具包的思路就是,TreeMap+TreeSet

  1. // 输出B A C D
  2. int[] high = new int[]{176, 170, 176, 176};
  3. String[] name = new String[]{"A", "B", "C", "D"};
  4. Main main = new Main();
  5. main.pz(high, name);
  6. public void pz(int[] high, String[] name) {
  7. TreeMap<Integer, TreeSet<String>> map = new TreeMap<>();
  8. for (int i = 0; i< high.length; i++) {
  9. if (!map.containsKey(high[i])) {
  10. TreeSet<String> temp = new TreeSet<>();
  11. temp.add(name[i]);
  12. map.put(high[i], temp);
  13. }else {
  14. TreeSet<String> temp = map.get(high[i]);
  15. temp.add(name[i]);
  16. map.put(high[i], temp);
  17. }
  18. }
  19. for (Integer people : map.keySet()) {
  20. TreeSet<String> names = map.get(people);
  21. for (String s : names) {
  22. System.out.print(s+" ");
  23. }
  24. }
  25. }