标签:style 类型转换 oid 索引 分享 == amp art etc
================================
?Copyright 蕃薯耀 2021-07-02
https://www.cnblogs.com/fanshuyao/
import java.util.ArrayList; import java.util.Collection; import java.util.Date; import java.util.Iterator; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.Set; import org.apache.commons.lang.StringUtils; import org.apache.commons.lang3.ArrayUtils; import org.assertj.core.util.Arrays; /** * Map转二维数组 * */ public class MapArrayUtils { /** * Map转二维数组 * @param row Row * @return */ @SuppressWarnings("rawtypes") public static Object[][] getTwoArrayObject(Map map) { Object[][] object = null; if (map != null && !map.isEmpty()) { int size = map.size(); object = new Object[size][2]; Iterator iterator = map.entrySet().iterator(); for (int i = 0; i < size; i++) { Map.Entry entry = (Map.Entry) iterator.next(); Object key = entry.getKey(); Object value = entry.getValue(); object[i][0] = key; object[i][1] = value; } } return object; } /** * 根据指定的keys获取Map中的属性 * @param map * @param keys * @return */ @SuppressWarnings({ "rawtypes", "unchecked" }) public static Map<String, Object> getMapByExistKeys(Map map, String[] keys) { if(map == null || map.isEmpty()) { return null; } if(keys == null || keys.length < 1) { return null; } Map<String, Object> resultMap = new LinkedHashMap<String, Object>(keys.length); Set<String> set = map.keySet(); for (String key : set) { if(ArrayUtils.contains(keys, key)) { resultMap.put(key, map.get(key)); } } return resultMap; } /** * 排除指定的keys获取Map中的其它属性 * @param map * @param excludeKeys 排除的keys * @return */ @SuppressWarnings({ "rawtypes", "unchecked" }) public static Map<String, Object> getMapByExcludeKeys(Map map, String[] excludeKeys) { if(map == null || map.isEmpty()) { return null; } Map<String, Object> resultMap = new LinkedHashMap<String, Object>(); Set<String> set = map.keySet(); for (String key : set) { if(ArrayUtils.contains(excludeKeys, key)) { continue; } resultMap.put(key, map.get(key)); } return resultMap; } /** * 数据类型转换 * @param value Object * @param datePattern String * @return */ public static String valueCast(Object value, String datePattern) { String valueString = ""; //类型转换 if(value == null) { valueString = ""; }else if(value instanceof String) { valueString = (String)value; }else if(value instanceof Date) {//日期时间 Date date = (Date)value; if(StringUtils.isBlank(datePattern)) { valueString = DateUtils.formatDateTime(date); }else { valueString = DateUtils.format(date, datePattern); } }else if(Arrays.isArray(value) || value instanceof Collection) {//数组和集合 //判断是否为数组(建议优先使用Arrays): //1:Arrays.isArray(value) //2:value.getClass().isArray() valueString = JsonUtil.obj2String(value); }else { valueString = String.valueOf(value); } return valueString; } /** * Map转二维数组 * @param map Map参数 * @param keys 数组,当不为空时,只取keys里面的属性;当为空时,取出所有属性 * @param datePattern 时间转换格式 * @return */ @SuppressWarnings({ "unchecked", "rawtypes" }) public static String[][] getTwoArray(Map map, String[] keys, String datePattern) { if(map == null || map.isEmpty()) { return null; } if(keys != null && keys.length > 0) { map = getMapByExistKeys(map, keys); //需要再次判断map是否为空 if(map == null || map.isEmpty()) { return null; } } String[][] array = new String[map.size()][2]; Set<String> set = map.keySet(); //数组索引 int index = 0; for (String key : set) { if(keys != null && keys.length > 0) { if(!ArrayUtils.contains(keys, key)) { continue; } } Object value = map.get(key); array[index][0] = key; array[index][1] = valueCast(value, datePattern); index ++; } return array; } /** * Map转二维数组 * @param map Map参数 * @param datePattern 时间转换格式 * @return */ @SuppressWarnings("rawtypes") public static String[][] getTwoArray(Map map, String datePattern){ return getTwoArray(map, null, datePattern); } /** * Map转二维数组 * @param map Map参数 * @param keys 数组,当不为空时,只取keys里面的属性;当为空时,取出所有属性 * @return */ @SuppressWarnings("rawtypes") public static String[][] getTwoArray(Map map, String[] keys){ return getTwoArray(map, keys, null); } /** * Map转二维数组 * @param map Map参数 * @return */ @SuppressWarnings("rawtypes") public static String[][] getTwoArray(Map map){ return getTwoArray(map, null, null); } /** * Map转二维数组 * @param map Map参数 * @param keys 数组,当不为空时,只取keys里面的属性;当为空时,取出所有属性 * @param datePattern 时间转换格式 * @return */ @SuppressWarnings({ "unchecked", "rawtypes" }) public static String[][] getTwoArrayExclude(Map map, String[] excludeKeys, String datePattern) { if(map == null || map.isEmpty()) { return null; } if(excludeKeys != null && excludeKeys.length > 0) { map = getMapByExcludeKeys(map, excludeKeys); //需要再次判断map是否为空 if(map == null || map.isEmpty()) { return null; } } String[][] array = new String[map.size()][2]; Set<String> set = map.keySet(); //数组索引 int index = 0; for (String key : set) { Object value = map.get(key); array[index][0] = key; array[index][1] = valueCast(value, datePattern); index ++; } return array; } /** * Map转二维数组 * @param map Map参数 * @param excludeKeys 数组,当不为空时,取排除keys里面的其它属性;当为空时,取出所有属性 * @return */ @SuppressWarnings("rawtypes") public static String[][] getTwoArrayExclude(Map map, String[] excludeKeys){ return getTwoArrayExclude(map, excludeKeys, null); } public static void main(String[] args) { Map<String, Object> map = new LinkedHashMap<String, Object>(); map.put("aa", "11"); map.put("bb", 2); map.put("cc", true); map.put("dd", new Date()); map.put("ee", 6.333); map.put("ff", 66666666666L); map.put("gg", null); int[] a = new int[] {1,2,3}; map.put("ii", a); List<String> j = new ArrayList<String>(); j.add("a"); j.add("aa"); j.add("aaa"); map.put("jj", j); long start = System.currentTimeMillis(); System.out.println("start = " + start); String[][] array = getTwoArray(map, null, DateUtils.DATE); long end = System.currentTimeMillis(); System.out.println("end = " + end); System.out.println("end - start = " + (end - start)); if(array != null && array.length > 0) { for(int n = 0; n < array.length; n++) { System.out.println(array[n][0] + " = " + array[n][1]); } }else { System.out.println("array = " + array); } System.out.println("---------------------------------------------"); long start2 = System.currentTimeMillis(); System.out.println("start2 = " + start2); String[][] array2 = getTwoArray(map, new String[] {"dd", "cc", "ccc", "bb", "EE"}, DateUtils.DATE); long end2 = System.currentTimeMillis(); System.out.println("end2 = " + end2); System.out.println("end2 - start2 = " + (end2 - start2)); if(array2 != null && array2.length > 0) { for(int n = 0; n < array2.length; n++) { System.out.println(array2[n][0] + " = " + array2[n][1]); } }else { System.out.println("array2 = " + array2); } System.out.println("---------------------------------------------"); Map<String, Object> resultMap = getMapByExistKeys(map, new String[]{"aa", "BB", "cc"}); System.out.println("resultMap = " + resultMap); System.out.println("---------------------------------------------"); Map<String, Object> ExcludeMap = getMapByExcludeKeys(map, new String[]{"aa", "BB", "cc"}); System.out.println("ExcludeMap = " + ExcludeMap); System.out.println("---------------------------------------------"); long start3 = System.currentTimeMillis(); System.out.println("start3 = " + start3); String[][] array3 = getTwoArrayExclude(map, new String[]{"aa", "BB", "cc"}); long end3 = System.currentTimeMillis(); System.out.println("end3 = " + end3); System.out.println("end3 - start3 = " + (end3 - start3)); if(array3 != null && array3.length > 0) { for(int n = 0; n < array3.length; n++) { System.out.println(array3[n][0] + " = " + array3[n][1]); } }else { System.out.println("array3 = " + array3); } System.out.println("---------------------------------------------"); /* System.out.println("---------------------------------------------"); Object[][] array22 = getTwoArrayObject(map); if(array22 != null && array22.length > 0) { for(int n = 0; n < array22.length; n++) { System.out.println(array22[n][0] + " = " + array22[n][1]); } }else { System.out.println("array22 = " + array22); } */ } }
(时间宝贵,分享不易,捐赠回馈,^_^)
================================
?Copyright 蕃薯耀 2021-07-02
https://www.cnblogs.com/fanshuyao/
标签:style 类型转换 oid 索引 分享 == amp art etc
原文地址:https://www.cnblogs.com/fanshuyao/p/14962155.html