MapUtil.java 856 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. package cn.com.qmth.examcloud.commons.util;
  2. import java.util.Comparator;
  3. import java.util.Map;
  4. import java.util.TreeMap;
  5. /**
  6. * 集合工具
  7. *
  8. * @author WANGWEI
  9. * @date 2019年5月15日
  10. * @Copyright (c) 2018-? http://qmth.com.cn All Rights Reserved.
  11. */
  12. public class MapUtil {
  13. /**
  14. * map按key排序
  15. *
  16. * @author WANGWEI
  17. * @param map
  18. * @param asc
  19. * 升序
  20. * @return
  21. */
  22. public static <T> Map<String, T> sortMapByKey(Map<String, T> map, boolean asc) {
  23. if (map == null) {
  24. return null;
  25. }
  26. Map<String, T> sortMap = new TreeMap<String, T>(new Comparator<String>() {
  27. @Override
  28. public int compare(String o1, String o2) {
  29. if (asc) {
  30. return ((String) o1).compareTo((String) o2);
  31. } else {
  32. return ((String) o2).compareTo((String) o1);
  33. }
  34. }
  35. });
  36. sortMap.putAll(map);
  37. return sortMap;
  38. }
  39. }