1. 程式人生 > >獲取List中的Key值,返回List

獲取List中的Key值,返回List

【需求】:獲取List<Map>資料集中每項的key值,如List<map>=[{測試1:測試一,測試2:測試二},{測試1:測試三,測試2:測試四},{測試1:測試五,測試2:測試六},{測試1:測試七,測試2:測試八}]

返回結果List<String>=測試1,測試2

【解決】:

public List<String> GetMapKey(List<Map> listResult)
  {
    if ((listResult != null) && (!listResult.isEmpty()))
    {
      List listKey = new ArrayList();

      Map mapResult = (Map)listResult.get(0);

      Set mapKeySet = mapResult.keySet();

      String listHead = "";

      Iterator iteratorKey = mapKeySet.iterator();
      while (iteratorKey.hasNext()) {
        listHead = iteratorKey.next();
        listKey.add(listHead);
      }

      return listKey;
    }
    return null;
  }