-1

I have a list of Triples, e.g.

import org.apache.commons.lang3.tuple.ImmutableTriple;

final HashMap<String, ImmutableTriple<String, String, Function<String, String>>> olMap = new HashMap();

and I want to add something like

olMap.put("start", new ImmutableTriple("str1", "str2", MyClass::minusOne));

I get the following error:

The constructor ImmutableTriple(String, String, MyClass::minusOne) is undefined

which is

private static String minusOne(String count) {
    String ret = count;
    if (count != null) {
        try {
            ret = (Integer.parseInt(count) - 1) + "";
        } catch (final Exception e) {
            // nothing to do cuz input wasn't a number...
        }
    }
    return ret;
}

but somehow I do not get the signatures properly. And last but not least how to call finally the method? i.e. is this the correct syntax?

ol.get("start").right.apply("100")

UPDATE:

I've found the correct syntax:

final HashMap<String, Triple<String, String, Function<String, String>>> olMap = new HashMap();
olMap.put("start", new Triple.of("str1", "str2", MyClass::minusOne));

Thx for the help and reassurance - otherwise I wouldn't have found it

Naman
  • 27,789
  • 26
  • 218
  • 353
LeO
  • 4,238
  • 4
  • 48
  • 88
  • 1
    What are the compilation errors are you getting and what is your actual question? If code compiles and returns a string then it is "correct" in a certain sense. From the look of it, it should be correct. One thing maybe: I would not expect a triple to have a "right" field, so check the accessor methods there, otherwise looks okaish (well, do not catch such a generic exception etc, but those are side notes). – Oleg Sklyar Jan 10 '19 at 12:09
  • Are you getting some compilation error? Please share it with us. – Eran Jan 10 '19 at 12:09
  • Updated with the Error message and the origin of the `ImmutableTriple`. – LeO Jan 10 '19 at 12:15
  • minusOne is private. Could this be a problem? – Ralf Renz Jan 10 '19 at 12:21
  • I've changed it to `public` and unfortunately not :-( – LeO Jan 10 '19 at 12:46
  • @LeO, look at my answer, the final syntax you included to the question isn't correct – Andrew Tobilko Jan 10 '19 at 13:20

1 Answers1

1

new Triple.of(...) can be a correct Java syntax.

You tried to pass MyClass::minusOne as an Object and, since it's not a functional interface, you got the compilation error.

Make you sure you don't have raw types:

ImmutableTriple t = new ImmutableTriple("str1", "str2", MyClass::minusOne);
HashMap m = new HashMap();

The correct options would be specifying a complete type parameter list:

Triple<String, String, Function<String, String>> t1 = 
    Triple.<String, String, Function<String, String>>of("str1", "str2", MyClass::minusOne);
Triple<String, String, Function<String, String>> t2 = 
    new ImmutableTriple<String, String, Function<String, String>>("str1", "str2", MyClass::minusOne);

or using <> to let it be resolved automatically:

Triple<String, String, Function<String, String>> t1 = 
    Triple.of("str1", "str2", MyClass::minusOne);
Triple<String, String, Function<String, String>> t2 = 
    new ImmutableTriple<>("str1", "str2", MyClass::minusOne);
Andrew Tobilko
  • 48,120
  • 14
  • 91
  • 142
  • 1
    Why does the `<>` resolve it immediately? What is the usage of `<>` ? – LeO Jan 10 '19 at 13:28
  • @LeO it's called the diamond operator. Read more here: https://stackoverflow.com/questions/4166966/what-is-the-point-of-the-diamond-operator-in-java-7 – Andrew Tobilko Jan 10 '19 at 13:30