-3

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));
user1422348
  • 345
  • 1
  • 7
  • 23

2 Answers2

2

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.

Michael Spitsin
  • 2,539
  • 2
  • 19
  • 29
  • Pair Class from util in spring data does not allow these null values: private Pair(@NonNull S first, @NonNull T second) {... – NicoESIEA Dec 28 '22 at 16:21
-1

Better way to handle this will be to use Optional,

Pair.of(Optional.of(?), Optional.of(Object B))

Meet Shah
  • 750
  • 7
  • 5