Cannot resolve method Character::hashCode in a Java stream

Jan Bodnar :

In my example, I try to create an ASCII table from a sequence of characters. I managed to do it with a List of strings but failed with an array of chars.

I get an error that Character::hashCode cannot be resolved in Collectors.toMap().

Error:(26, 17) java: method collect in interface cannot be applied to given types;
  required: java.util.function.Supplier<R>,java.util.function.ObjIntConsumer<R>,java.util.function.BiConsumer<R,R>
  found:<java.lang.Object,capture#1 of ?,java.util.Map<java.lang.Object,java.lang.Object>>
  reason: cannot infer type-variable(s) R
    (actual and formal argument lists differ in length)
Error:(26, 42) java: incompatible types: cannot infer type-variable(s) T,K,U,T
    (argument mismatch; invalid method reference
      incompatible types: java.lang.Object cannot be converted to char)

Is there a way to do it?

public class JavaCollectToMapEx2 {

    public static void main(String[] args) {
        // list of ASCII characters
        var chars = List.of("a", "b", "c", "d", "e", "f",
                "g", "h", "i", "j", "k", "l", "m", "n",
                "o", "p", "q", "r", "s", "t", "u", "v",
                "w", "x", "y", "z");

//      CharSequence chars2 = "abcdefghijklmnopqrstuvwxyz";
        char[] letters = "abcdefghijklmnopqrstuvwxyz".toCharArray();

        // Map to represent ASCII character table
        Map<Integer, String> asciiMap =
           .collect(Collectors.toMap(String::hashCode, Function.identity()));

        Map<Integer, Character> asciiMap2 = CharBuffer.wrap(letters).chars() 
            .collect(Collectors.toMap(Character::hashCode, Function.identity()));

nickb :

.chars() is giving you an IntStream, which is a stream of primitive int, and not a stream of characters (more info). This is why no method references on Character will work.

To achieve what you're looking for, you'll need a Stream<Character> first:

Map<Integer, Character> asciiMap2 = CharBuffer.wrap(letters)
        .mapToObj(e -> (char) e)
        .collect(Collectors.toMap(e -> e.hashCode(), Function.identity()));

Now, you still have the issue of using a method reference for getting the hash code. You can't use Character::hashCode because it's ambiguous as to which method you want, as there are two that are possible:

  1. The override of Object#hashCode,
  2. The static method int hashCode(char value)

You can see from this code, that both satisfy the first argument to toMap():

Function<Character, Integer> f1 = e -> Character.hashCode(e);
Function<Character, Integer> f2 = e -> e.hashCode();

To resolve this, you can use Object::hashCode for the non-static method call.

Guess you like