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

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

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

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class Pair implements Serializable {
    private E1 first;
    private E2 second;

    public Pair() {
    }

    public Pair(E1 first, E2 second) {
        this.first = first;
        this.second = second;
    }

    public E1 first() {
        return this.first;
    }

    public E2 second() {
        return this.second;
    }

    public void setFirst(E1 first) {
        this.first = first;
    }

    public void setSecond(E2 second) {
        this.second = second;
    }

    public Pair copy() {
        return new Pair<>(this.first, this.second);
    }

    public Map toHashMap() {
        HashMap hashMap = new HashMap<>();
        hashMap.put(this.first, this.second);
        return hashMap;
    }

    public static  Pair of(E1 first, E2 second) {
        return new Pair<>(first, second);
    }

    public String toString() {
        return "Pair{" + "first=" + this.first +
                ", second=" + this.second +
                '}';
    }

    public boolean equals(Object o) {
        if (this == o) {
            return true;
        } else if (!(o instanceof Pair)) {
            return false;
        } else {
            Pair pair = (Pair)o;
            return Objects.equals(this.first, pair.first) && Objects.equals(this.second, pair.second);
        }
    }

    public int hashCode() {
        return Objects.hash(this.first, this.second);
    }

    @SafeVarargs
    public static  Map toMap(Pair... pairs) {
        return (Map)Stream.of(pairs).collect(Collectors.toMap(Pair::first, Pair::second));
    }
}





© 2015 - 2024 Weber Informatics LLC | Privacy Policy