1. 程式人生 > 實用技巧 >map資料按照list排序

map資料按照list排序

  • 簡介 (Introduction):

    • 背景
      需要對欄位進行排序,但是,一個欄位的結果存在一個map中,一個存在list中,需要讓map的陣列,按照list的順序進行排序

    • 結構圖
      list順序

       [
      {
      "fieldKey":"關鍵字",
      "alias":"ID",
      "owner":"mysql",
      "field":"ID",
      "fieldNumber":1
      },
      {
      "fieldKey":"名稱",
      "alias":"CHNAME",
      "owner":"mysql",
      "field":"NAME",
      "fieldNumber":2
      },
      {
      "fieldKey":"是否刪除",
      "alias":"DELETED",
      "owner":"mysql",
      "field":"ISDELETED",
      "fieldNumber":3
      },
      {
      "fieldKey":"序號",
      "alias":"ORDERNO",
      "owner":"mysql",
      "field":"ORDERNO",
      "fieldNumber":4
      }
      ]

      map順序

       {
      "ORDERNO":{
      "fieldKey":"序號",
      "alias":"ORDERNO",
      "owner":"mysql",
      "field":"ORDERNO",
      "fieldNumber":4
      },
      "NAME":{
      "fieldKey":"名稱",
      "alias":"CHNAME",
      "owner":"mysql",
      "field":"NAME",
      "fieldNumber":2
      },
      "ISDELETED":{
      "fieldKey":"是否刪除",
      "alias":"DELETED",
      "owner":"mysql",
      "field":"ISDELETED",
      "fieldNumber":3
      },
      "ID":{
      "fieldKey":"關鍵字",
      "alias":"ID",
      "owner":"mysql",
      "field":"ID",
      "fieldNumber":1
      }
      }
  • 核心

  • 快速上手(Getting Started)

模型

list模型:

 /**
* 生成資源陣列
*
* @return 結果集
*/
private static List<FieldDemo> getListSource() {
List<FieldDemo> fieldDemos = new ArrayList<>();
FieldDemo fieldDemo = new FieldDemo();
fieldDemo.setField("ID");
fieldDemo.setAlias("ID");
fieldDemo.setFieldKey("關鍵字");
fieldDemo.setOwner("mysql");
fieldDemo.setFieldNumber(1); FieldDemo fieldDemo2 = new FieldDemo();
fieldDemo2.setField("NAME");
fieldDemo2.setAlias("CHNAME");
fieldDemo2.setFieldKey("名稱");
fieldDemo2.setOwner("mysql");
fieldDemo2.setFieldNumber(2); FieldDemo fieldDemo3 = new FieldDemo();
fieldDemo3.setField("ISDELETED");
fieldDemo3.setAlias("DELETED");
fieldDemo3.setFieldKey("是否刪除");
fieldDemo3.setOwner("mysql");
fieldDemo3.setFieldNumber(3); FieldDemo fieldDemo4 = new FieldDemo();
fieldDemo4.setField("ORDERNO");
fieldDemo4.setAlias("ORDERNO");
fieldDemo4.setFieldKey("序號");
fieldDemo4.setOwner("mysql");
fieldDemo4.setFieldNumber(4); fieldDemos.add(fieldDemo);
fieldDemos.add(fieldDemo2);
fieldDemos.add(fieldDemo3);
fieldDemos.add(fieldDemo4); return fieldDemos;
}

map模型:

 /**
* 生成map
*
* @return map結果集
*/
private static Map<FieldDemo, FieldDemo> getMapSource() {
Map<FieldDemo, FieldDemo> map = new HashMap<>();
List<FieldDemo> listSource = getListSource();
List<FieldDemo> listSource2 = getListSource2();
for (FieldDemo fieldDemo : listSource) {
for (FieldDemo demo : listSource2) {
if (fieldDemo.getField().equalsIgnoreCase(demo.getField())) {
map.put(fieldDemo, demo);
}
}
}
return map;
} private static List<FieldDemo> getListSource2() {
List<FieldDemo> fieldDemos = new ArrayList<>();
FieldDemo fieldDemo = new FieldDemo();
fieldDemo.setField("ID");
fieldDemo.setAlias("ID");
fieldDemo.setFieldKey("關鍵字");
fieldDemo.setOwner("mysql");
fieldDemo.setFieldNumber(1); FieldDemo fieldDemo2 = new FieldDemo();
fieldDemo2.setField("NAME");
fieldDemo2.setAlias("CHNAME");
fieldDemo2.setFieldKey("名稱");
fieldDemo2.setOwner("mysql");
fieldDemo2.setFieldNumber(2); FieldDemo fieldDemo3 = new FieldDemo();
fieldDemo3.setField("ISDELETED");
fieldDemo3.setAlias("DELETED");
fieldDemo3.setFieldKey("是否刪除");
fieldDemo3.setOwner("mysql");
fieldDemo3.setFieldNumber(3); FieldDemo fieldDemo4 = new FieldDemo();
fieldDemo4.setField("ORDERNO");
fieldDemo4.setAlias("ORDERNO");
fieldDemo4.setFieldKey("序號");
fieldDemo4.setOwner("mysql");
fieldDemo4.setFieldNumber(4); fieldDemos.add(fieldDemo4);
fieldDemos.add(fieldDemo3);
fieldDemos.add(fieldDemo);
fieldDemos.add(fieldDemo2);
return fieldDemos;
}

業務

核心處理

  /**
* 讓複雜map按照list的順序排序
*
* @param list
* @param map
* @param newMap
*/
public static void sortMapFromList(List<FieldDemo> list, Map<FieldDemo, FieldDemo> map, Map<FieldDemo, FieldDemo> newMap) { Map<FieldDemo, Integer> middleMap = new HashMap<>(16); for (FieldDemo fieldDemo : list) {
for (Map.Entry<FieldDemo, FieldDemo> fieldDemoFieldDemoEntry : map.entrySet()) {
String field = fieldDemoFieldDemoEntry.getKey().getField(); if (field.equalsIgnoreCase(fieldDemo.getField())) {
// 生成中間儲存map,存入資料和對應的序號
middleMap.put(fieldDemoFieldDemoEntry.getKey(), fieldDemo.getFieldNumber());
}
}
} // 生成的結果集加入到list中
List<Map.Entry<FieldDemo, Integer>> entryList = new ArrayList(middleMap.entrySet()); // 根據value 進行排序
Collections.sort(entryList, new Comparator<Map.Entry<FieldDemo, Integer>>() {
@Override
public int compare(Map.Entry<FieldDemo, Integer> o1, Map.Entry<FieldDemo, Integer> o2) {
return o1.getValue().compareTo(o2.getValue());
}
}); System.out.println("entryList:" + new Gson().toJson(entryList)); for (Map.Entry<FieldDemo, Integer> fieldDemoFieldDemoEntry : entryList) { for (Map.Entry<FieldDemo, FieldDemo> demoFieldDemoEntry : map.entrySet()) { String field = demoFieldDemoEntry.getKey().getField(); if (field.equalsIgnoreCase(fieldDemoFieldDemoEntry.getKey().getField())) { System.out.println("fieldNumber:" + new Gson().toJson(fieldDemoFieldDemoEntry.getValue()));
System.out.println("fieldDemoFieldDemoEntry:" + new Gson().toJson(fieldDemoFieldDemoEntry.getKey()));
System.out.println("demoFieldDemoEntry:" + new Gson().toJson(demoFieldDemoEntry.getValue())); newMap.put(fieldDemoFieldDemoEntry.getKey(), demoFieldDemoEntry.getValue());
}
}
}
}

測試

 public static void main(String[] args) {

         FieldDemo fieldDemo = new FieldDemo();

         System.out.println("fieldDemo順序:" + new Gson().toJson(fieldDemo));

         // 讓listSource2跟listSource順序一致
Map<FieldDemo, FieldDemo> mapSource = getMapSource(); System.out.println("map順序:" + new Gson().toJson(mapSource)); List<FieldDemo> listSource = getListSource(); System.out.println("list順序:" + new Gson().toJson(listSource)); // mapSource 跟 listSource順序一致,去除不是listSource理的欄位
Map<FieldDemo, FieldDemo> newMap = new HashMap<>(); sortMapFromList(listSource, mapSource, newMap); System.out.println("排序後:" + newMap);
}

待開發中

  • 環境設定 (Prerequisite):

    • 環境
      JDK1.8
    • 配置

      IDEA 2019
    • 存在問題

      暫無

  • 進階篇 (Advanced):

    研究中 ......