将List/Map中的key转换为驼峰规则

/**
   * 将Map中的key由下划线转换为驼峰
   *
   * @param map
   * @return
   */
  public static Map formatHumpName(Map map) {
    Map newMap = new HashMap();
    Iterator> it = map.entrySet().iterator();
    while (it.hasNext()) {
      Map.Entry entry = it.next();
      String key = entry.getKey();
      String newKey = toFormatCol(key);
      newMap.put(newKey, entry.getValue());
    }
    return newMap;
  }

  public static String toFormatCol(String colName) {
    StringBuilder sb = new StringBuilder();
    String[] str = colName.toLowerCase().split("_");
    int i = 0;
    for (String s : str) {
      if (s.length() == 1) {
        s = s.toUpperCase();
      }
      i++;
      if (i == 1) {
        sb.append(s);
        continue;
      }
      if (s.length() > 0) {
        sb.append(s.substring(0, 1).toUpperCase());
        sb.append(s.substring(1));
      }
    }
    return sb.toString();
  }

  /**
   * 将List中map的key值命名方式格式化为驼峰
   *
   * @param
   * @return
   */
  public static List> formatHumpNameForList(List> list) {
    List> newList = new ArrayList>();
    for (Map o : list) {
      newList.add(formatHumpName(o));
    }
    return newList;
  }

你可能感兴趣的:(Java,驼峰命名法,List,Map)