閱讀582 返回首頁    go 阿裏雲 go 技術社區[雲棲]


java 開發中常用方法

從List中拿出top指的條數數據:

/**
     * 取top x條產品類數據
     *
     * @param sourList  產品類集合
     * @param rowsCount 條數
     * @return List<ProductInfo>
     */
    public static List<ProductInfo> limitProductInfoList(List<ProductInfo> sourList,
                                                         int rowsCount) {

        List<ProductInfo> tempList = new ArrayList<>();
        if (sourList != null) {
            int sourListSize = sourList.size();
            if (rowsCount < sourListSize) {
                int subCount =
                        sourListSize % rowsCount == 0 ? sourListSize / rowsCount : sourListSize / rowsCount + 1;
                int startIndext = 0;
                int stopIndext = 0;
                for (int i = 0; i < subCount; i++) {
                    stopIndext =
                            (i == subCount - 1) ? stopIndext + sourListSize % rowsCount : stopIndext + rowsCount;
                    tempList = new ArrayList<ProductInfo>(sourList.subList(startIndext, stopIndext));
                    startIndext = stopIndext;
                    if (tempList.size() > 0) {
                        break;
                    }
                }
            } else {
                tempList = sourList;
            }
        }

        return tempList;
    }

比較字符串是否在數組中:

 private static String[] StarArray = new String[]{"DHTL", "IHTL", "GPKG", "GDIY", "GCRU"};

ArrayUtils.contains(StarArray, "DHTL")

//驗證對象為null

if (ObjectUtils.equals(sysRole, null)) {
    sysRole = this.getRole(systemCode, loginName);
}

//驗證List

if (CollectionUtils.isNotEmpty(sysRoleList)) {
    sysRole = sysRoleList.get(0);
}

//驗證字符串-驗證時候忽略空白

if (StringUtils.isBlank(formData.getPost())){
}

//驗證字符串非空

if (StringUtils.isNotEmpty(userCard.getUID()))
/**
     * 清除空白字符
     *
     * @param str
     * @return
     */
    public static String trimAllWhitespace(String str) {
        if (str != null) {
            int len = str.length();
            if (len > 0) {
                char[] dest = new char[len];
                int destPos = 0;
                for (int i = 0; i < len; ++i) {
                    char c = str.charAt(i);
                    if (!Character.isWhitespace(c)) {
                        dest[destPos++] = c;
                    }
                }
                return new String(dest, 0, destPos);
            }
        }
        return str;
    }


    /**
     *list轉換string
     * @param list  List<String>
     * @param separator 分隔符 逗號等
     * @return
     */
    public static String listToString(List<String> list, char separator) {
        return org.apache.commons.lang.StringUtils.join(list.toArray(), separator);
    }


    /**
     *string轉換list
     * @param str 分隔符字符串
     * @param separator  逗號等
     * @return  List<String>
     */
    public static List<String> stringToList(String str, String separator) {
        return java.util.Arrays.asList(str.split(separator));
    }

最後更新:2017-11-14 16:04:06

  上一篇:go  如何從雲市場中選擇適合自己企業的數據庫審計產品
  下一篇:go  mysql日誌監控