javaandroidnullkeyvaluepair

How to pass null into Pair<String, String> in Java


I'm new to Java. I'm creating an app in android, and I created a method that takes a varargs of Pair<String, String>. But I can't seem to pass null in. Is this possible?

Method:

makeJson(Pair<String, String>... keyValuePairs) {
...
}

Caller:

makeJson(Pair.create("reference", null));

Solution

  • You can use just ordinary Pair's constructor:

    new Pair<String, String>("reference", null)
    

    And according link to the source code of Pair.java:

    public static <A, B> Pair<A, B> create(A a, B b) {
        return new Pair<A, B>(a, b);
    }
    

    Static method create just invokes constructor so it should work with nulls.