-2

Given the following code:

static <U,V> List<V> map(Iterable<U> l, Function<U,V> f) {
    return null;
}

I need to pass an arraylist in the first parameter and a hash function in the second which takes a string and outputs a type int.

I'm trying the following but get the following error message.

map(names,<String,Integer> hashCode());

map(names,hash());

Error message:Not applicable for the arguments int

Need to pass a hash function so I can use this function inside the method.

smac89
  • 39,374
  • 15
  • 132
  • 179

1 Answers1

0

Here:

hashCode()

isn't a function! It is simply a method invocation. You would rather need a lambda, something like:

x -> x.hashCode()

or, as you are probably using the hashcode of the current object:

this::hashCode

But that actually depends on the object on which you intend to invoke the hashCode method. And of course, there is also difference regarding the interface you intend to "use", be it a Function or Supplier (that second method reference example would be a supplier for example).

GhostCat
  • 137,827
  • 25
  • 176
  • 248
  • 1
    `this::hashCode` would be a Supplier, not a Function. – shmosel Jan 17 '19 at 23:16
  • @shmosel Good observation, I updated the answer accordingly. – GhostCat Jan 18 '19 at 08:46
  • 1
    I don’t get why you insist on suggesting `this::hashCode`. The OP showed a reasonable method declaration like `map(Iterable l, Function f)` and even continued with the matching explanation “… and a hash function in the second which takes a string and outputs a type int”, clearly indicating that your first lambda expression, `x -> x.hashCode()`, or an equivalent `Object::hashCode` (or `String::hashCode`, to match the task literally), is the right way to go. – Holger Jan 18 '19 at 15:15