CollectionUtil

import java.util.*;

public class CollectionUtil {

    /**
     * 对List进行分页处理
     * @param list 需要分页的List
     * @param pageSize 单页条数
     * @param pageNum 页码
     */
    public static <T> List<T> pageList(List<T> list, int pageSize, int pageNum) {
        if (list == null || list.isEmpty()) {
            return list;
        }
        int offset = (pageNum - 1) * pageSize;
        int size = Math.min(pageNum * pageSize, list.size());
        if (offset > size) {
            return Collections.emptyList();
        }
        return list.subList(offset, size);
    }

    /**
     * 匹配Map中是否存在key,忽略大小写
     * @param map 要遍历的Map
     * @param key 要匹配的Key
     * @return 命中Map中的Key
     */
    public static String matchKeyIgnoreCase(Map<String, ?> map, String key) {
        if (map == null || map.isEmpty() || key == null) {
            return null;
        }
        for (String mapKey : map.keySet()) {
            if (mapKey.equalsIgnoreCase(key)) {
                return mapKey;
            }
        }
        return null;
    }

    /**
     * 对List元素去重
     */
    public static <T> List<T> uniqueList(List<T> list) {
        if (list == null || list.isEmpty()) {
            return list;
        }
        Set<T> set = new HashSet<>(list);
        list.clear();
        list.addAll(set);
        return list;
    }

}

猜你喜欢

转载自my.oschina.net/u/812874/blog/863389