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

io.github.jiawade.tool.collectors.CollectionKit Maven / Gradle / Ivy

The newest version!
package io.github.jiawade.tool.collectors;

import com.google.common.collect.Lists;
import io.cucumber.java.eo.Do;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;
import java.util.function.DoubleSupplier;
import java.util.function.Function;
import java.util.stream.Collectors;

public class CollectionKit {

    private CollectionKit() {
    }

    public static  List findDuplicates(Collection list) {
        final Set seen = new HashSet<>();
        return list.stream()
                .filter(n -> !seen.add(n))
                .collect(Collectors.toCollection(ArrayList::new));
    }

    public static  List unionSet(Collection left, Collection right) {
        left.addAll(right);
        return new ArrayList<>(new HashSet<>(left));
    }

    public static  List interSet(Collection left, Collection right) {
        return left.stream().filter(right::contains).distinct().collect(Collectors.toList());
    }

    public static  List leftDifference(Collection left, Collection right) {
        List inter = interSet(left, right);
        return left.stream().filter(i -> !inter.contains(i)).collect(Collectors.toList());
    }

    public static  List rightDifference(Collection left, Collection right) {
        List inter = interSet(left, right);
        return right.stream().filter(i -> !inter.contains(i)).collect(Collectors.toList());
    }

    public static  List allDifference(Collection left, Collection right) {
        List all = new ArrayList<>(leftDifference(left, right));
        all.addAll(rightDifference(left, right));
        return all;
    }

    public static  List sort(Collection elements) {
        return elements.stream().sorted().collect(Collectors.toList());
    }

    public static  List sort(Collection elements, Comparator comparator) {
        return elements.stream().sorted(comparator).collect(Collectors.toList());
    }

    public static  Iterator toIterator(Enumeration enumeration) {
        return new EnumerationIterator(enumeration);
    }

    public static Double sum(Collection collection) {
        if (collection.isEmpty()) {
            return 0d;
        }
        return collection.stream()
                .map(Number::toString)
                .map(Double::parseDouble)
                .mapToDouble(Double::doubleValue)
                .filter(i -> !Double.isNaN(i))
                .sum();
    }

    public static Double average(List collection) {
        if (collection.isEmpty()) {
            return 0d;
        }
        return collection.stream()
                .map(Number::toString)
                .map(Double::parseDouble)
                .mapToDouble(Double::doubleValue)
                .filter(i -> !Double.isNaN(i))
                .average()
                .orElse(Double.NaN);
    }

    public static  Map groupNumber(Collection list) {
        return list.stream().collect(Collectors.groupingBy(Function.identity(), Collectors.mapping(Function.identity(), Collectors.counting())));
    }

    public static Double retainNFloat(double value) {
        BigDecimal bd = new BigDecimal(value);
        bd = bd.setScale(6, RoundingMode.HALF_UP);
        return Double.parseDouble(bd.toString());
    }

    private static class EnumerationIterator implements Iterator {

        private Enumeration enumeration;

        public EnumerationIterator(Enumeration enumeration) {
            this.enumeration = enumeration;
        }

        public boolean hasNext() {
            return this.enumeration.hasMoreElements();
        }

        public E next() {
            return this.enumeration.nextElement();
        }

        public void remove() throws UnsupportedOperationException {
            throw new UnsupportedOperationException("Not supported");
        }
    }


    public static void main(String[] args) {
        List l = Lists.newArrayList(1, 2, 4, 4, 8, 9, 1);
        List v = Lists.newArrayList(1d, 3d, 5d, Double.NaN);
        System.out.println(sum(l));
        System.out.println(average(v));


    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy