1. 程式人生 > 其它 >Java Map轉二維陣列,Map轉陣列

Java Map轉二維陣列,Map轉陣列

JavaMap轉二維陣列,Map轉陣列

================================

©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/