All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.housepower.misc.CollectionUtil Maven / Gradle / Ivy

/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.github.housepower.misc;

import javax.annotation.Nullable;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class CollectionUtil {

    public static  List concat(List first, List second) {
        return Stream.concat(first.stream(), second.stream()).collect(Collectors.toList());
    }

    @SafeVarargs
    public static  List concat(List originList, T... elements) {
        return Stream.concat(originList.stream(), Stream.of(elements)).collect(Collectors.toList());
    }

    public static  List repeat(int time, List origin) {
        assert time > 0;
        List result = origin;
        for (int i = 0; i < time - 1; i++) {
            result = concat(result, origin);
        }
        return result;
    }

    public static List filterIgnoreCase(List set, String keyword) {
        return set.stream()
                .filter(key -> key.equalsIgnoreCase(keyword))
                .collect(Collectors.toList());
    }

    public static Map filterKeyIgnoreCase(Properties properties, String keyword) {
        Map props = new HashMap<>();
        properties.forEach((k, v) -> props.put(k.toString(), v.toString()));
        return filterKeyIgnoreCase(props, keyword);
    }

    public static  Map filterKeyIgnoreCase(Map map, String keyword) {
        return map.entrySet().stream()
                .filter(entry -> entry.getKey().equalsIgnoreCase(keyword))
                .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
    }

    public static  Map mergeMapKeepFirst(Map one, Map other) {
        return Stream.concat(one.entrySet().stream(), other.entrySet().stream()).collect(
                Collectors.toMap(
                        Map.Entry::getKey,
                        Map.Entry::getValue,
                        (former, latter) -> former));
    }

    public static  Map mergeMapKeepLast(Map one, Map other) {
        return Stream.concat(one.entrySet().stream(), other.entrySet().stream()).collect(
                Collectors.toMap(
                        Map.Entry::getKey,
                        Map.Entry::getValue,
                        (former, latter) -> latter));
    }

    public static  void mergeMapInPlaceKeepFirst(Map one, @Nullable Map other) {
        if (other != null)
            other.forEach(one::putIfAbsent);
    }

    public static  void mergeMapInPlaceKeepLast(Map one, @Nullable Map other) {
        if (other != null)
            other.forEach(one::put);
    }

    public static boolean isNotEmpty(Collection collection) {
        return collection != null && collection.size() > 0;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy