MapUtil.java 7.1 KB

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