MapUtil.java 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. package com.keao.edu.util.collection;
  2. import org.apache.commons.lang3.StringUtils;
  3. import org.apache.poi.ss.formula.functions.T;
  4. import java.beans.BeanInfo;
  5. import java.beans.IntrospectionException;
  6. import java.beans.Introspector;
  7. import java.beans.PropertyDescriptor;
  8. import java.lang.reflect.InvocationTargetException;
  9. import java.lang.reflect.Method;
  10. import java.util.HashMap;
  11. import java.util.List;
  12. import java.util.Map;
  13. import java.util.Objects;
  14. public class MapUtil {
  15. private static final Object[] EMPTY_ARRAY = {};
  16. /**
  17. * 地球半径,单位:公里/千米
  18. */
  19. private static final double EARTH_RADIUS = 6378.137;
  20. /**
  21. * 将一个bean转换成map
  22. * @param map
  23. * @param bean
  24. * @return
  25. */
  26. public static Map<String, Object> populateMap(Map<String, Object> map, Object bean) {
  27. return populateMap(map, bean, null);
  28. }
  29. /**
  30. * 假设prefix=detail.,bean带有一个属性name,则map中将有一个项:
  31. * key=detail.name,value为bean的name属性值。
  32. */
  33. public static Map<String, Object> populateMap(Map<String, Object> map, Object bean, String prefix) {
  34. boolean withoutPrefix = StringUtils.isBlank(prefix);
  35. try {
  36. Method[] methods = bean.getClass().getMethods();
  37. for (int i = 0; i < methods.length; i++) {
  38. String methodName = methods[i].getName();
  39. Class<?>[] pts = methods[i].getParameterTypes();
  40. Class<?> rt = methods[i].getReturnType();
  41. if (methodName.startsWith("get") && pts.length == 0 && !Void.class.equals(rt)) {
  42. String propName = Character.toLowerCase(methodName.charAt(3)) + methodName.substring(4);
  43. if ("class".equals(propName)) {
  44. continue;
  45. }
  46. String key = withoutPrefix ? propName : prefix + propName;
  47. Object value = methods[i].invoke(bean, EMPTY_ARRAY);
  48. if (value != null) {
  49. map.put(key, value);
  50. }
  51. }
  52. }
  53. return map;
  54. } catch (Exception e) {
  55. throw new RuntimeException(e);
  56. }
  57. }
  58. /**
  59. * map to javabean
  60. * @param clazz
  61. * @param map
  62. * @return
  63. * @throws IllegalArgumentException
  64. * @throws IllegalAccessException
  65. * @throws IntrospectionException
  66. * @throws InstantiationException
  67. */
  68. @SuppressWarnings("rawtypes")
  69. public static <T> T mapToJavaBean(Class<T> clazz, Map map) throws IllegalAccessException, IllegalArgumentException, IntrospectionException,
  70. InstantiationException {
  71. T obj = null;
  72. BeanInfo beanInfo = Introspector.getBeanInfo(clazz);
  73. obj = clazz.newInstance();
  74. PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
  75. for (int i = 0; i < propertyDescriptors.length; i++) {
  76. PropertyDescriptor descriptor = propertyDescriptors[i];
  77. String propertyName = descriptor.getName();
  78. if (map.containsKey(propertyName)) {
  79. Object value = map.get(propertyName);
  80. if ("".equals(value)) {
  81. value = null;
  82. }
  83. Object[] args = new Object[1];
  84. args[0] = value;
  85. try {
  86. descriptor.getWriteMethod().invoke(obj, args);
  87. } catch (InvocationTargetException e) {
  88. System.out.println("params poxy failed");
  89. }
  90. }
  91. }
  92. return (T) obj;
  93. }
  94. /**
  95. * @Author: Joburgess
  96. * @Date: 2019/10/12
  97. * @params [maps]
  98. * @return java.util.Map
  99. * @describe mybatis返回结果转换为Map
  100. */
  101. public static <T,K> Map convertMybatisMap(List<Map<T,K>> maps){
  102. int size = 0;
  103. if(maps != null){
  104. size = maps.size();
  105. }
  106. Map result=new HashMap(size);
  107. maps.forEach(stringIntegerMap -> {
  108. if(Objects.isNull(stringIntegerMap)){
  109. return;
  110. }
  111. result.put(stringIntegerMap.get("key"),stringIntegerMap.get("value").toString());
  112. });
  113. return result;
  114. }
  115. /**
  116. * @Author: Joburgess
  117. * @Date: 2019/10/12
  118. * @params [maps]
  119. * @return java.util.Map
  120. * @describe mybatis返回结果转换为Map
  121. */
  122. public static Map convertIntegerMap(List<Map> maps){
  123. int size = 0;
  124. if(maps != null){
  125. size = maps.size();
  126. }
  127. Map result=new HashMap(size);
  128. maps.forEach(stringIntegerMap -> {
  129. if(Objects.isNull(stringIntegerMap)){
  130. return;
  131. }
  132. result.put(stringIntegerMap.get("key"),stringIntegerMap.get("value"));
  133. });
  134. return result;
  135. }
  136. /**
  137. * 经纬度转化成弧度
  138. * @param d
  139. * 经度/纬度
  140. * @return 经纬度转化成的弧度
  141. */
  142. private static double radian(double d) {
  143. return d * Math.PI / 180.0;
  144. }
  145. /**
  146. * 返回两个地理坐标之间的距离
  147. * @param firsLongitude
  148. * 第一个坐标的经度
  149. * @param firstLatitude
  150. * 第一个坐标的纬度
  151. * @param secondLongitude
  152. * 第二个坐标的经度
  153. * @param secondLatitude
  154. * 第二个坐标的纬度
  155. * @return 两个坐标之间的距离,单位:公里/千米
  156. */
  157. public static double distance(double firsLongitude, double firstLatitude,
  158. double secondLongitude, double secondLatitude) {
  159. double firstRadianLongitude = radian(firsLongitude);
  160. double firstRadianLatitude = radian(firstLatitude);
  161. double secondRadianLongitude = radian(secondLongitude);
  162. double secondRadianLatitude = radian(secondLatitude);
  163. double a = firstRadianLatitude - secondRadianLatitude;
  164. double b = firstRadianLongitude - secondRadianLongitude;
  165. double cal = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2)
  166. + Math.cos(firstRadianLatitude) * Math.cos(secondRadianLatitude)
  167. * Math.pow(Math.sin(b / 2), 2)));
  168. cal = cal * EARTH_RADIUS;
  169. return Math.round(cal * 10000d) / 10000d * 1000;
  170. }
  171. /**
  172. * 返回两个地理坐标之间的距离
  173. * @param firstPoint
  174. * 第一个坐标 例如:"23.100919, 113.279868"
  175. * @param secondPoint
  176. * 第二个坐标 例如:"23.149286, 113.347584"
  177. * @return 两个坐标之间的距离,单位:公里/千米
  178. */
  179. public static double distance(String firstPoint, String secondPoint) {
  180. String[] firstArray = firstPoint.split(",");
  181. String[] secondArray = secondPoint.split(",");
  182. double firstLatitude = Double.valueOf(firstArray[0].trim());
  183. double firstLongitude = Double.valueOf(firstArray[1].trim());
  184. double secondLatitude = Double.valueOf(secondArray[0].trim());
  185. double secondLongitude = Double.valueOf(secondArray[1].trim());
  186. return distance(firstLatitude, firstLongitude, secondLatitude, secondLongitude);
  187. }
  188. }