亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

無法解析 Java 流中的方法 Character::hashCode

無法解析 Java 流中的方法 Character::hashCode

一只名叫tom的貓 2023-03-09 15:20:59
在我的示例中,我嘗試從一系列字符創建一個 ASCII 表。我設法用一個List字符串來做,但用一個字符數組失敗了。我收到Character::hashCode無法在Collectors.toMap().Error:(26, 17) java: method collect in interface java.util.stream.IntStream 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.util.stream.Collector<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)有辦法嗎?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 = chars.stream()           .collect(Collectors.toMap(String::hashCode, Function.identity()));        Map<Integer, Character> asciiMap2 = CharBuffer.wrap(letters).chars()             .collect(Collectors.toMap(Character::hashCode, Function.identity()));        System.out.println(asciiMap);        System.out.println(asciiMap2);    }}
查看完整描述

3 回答

?
Smart貓小萌

TA貢獻1911條經驗 獲得超7個贊

.chars()給你一個IntStream,它是原始流int,而不是字符流(更多信息)。這就是為什么沒有方法引用Character會起作用的原因。

要實現您的目標,您Stream<Character>首先需要:

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

現在,您仍然有使用方法引用獲取哈希碼的問題。您不能使用Character::hashCode,因為它對您想要的方法不明確,因為有兩種可能的方法:

  1. Object#hashCode 的覆蓋,

  2. 靜態方法int hashCode(char value)

從這段代碼中可以看出,兩者都滿足 的第一個參數toMap()

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

要解決此問題,您可以使用Object::hashCode非靜態方法調用。


查看完整回答
反對 回復 2023-03-09
?
慕碼人2483693

TA貢獻1860條經驗 獲得超9個贊

collect()由于您使用的是after CharBuffer::charswhich returns的方法IntStream,因此您可以使用的唯一收集方法是IntStream::collect(Supplier<R> supplier, ObjIntConsumer<R> accumulator,  BiConsumer<R,R> combiner)采用 3 個參數。

如果你想使用單參數收集方法,IntStream::boxed在它之前放置 return Stream<Integer>。然后該方法Character::hashCode變得不明確,無法使用 lambda 表達式:

為避免這種情況,只需使用更好的方法直接mapToObj轉換為char而不需要裝箱,然后使用Object::hashCode從 `Object 繼承:

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


查看完整回答
反對 回復 2023-03-09
?
有只小跳蛙

TA貢獻1824條經驗 獲得超8個贊

首先,您需要將 映射IntStream到 a Stream<Character>。但在那之后你不能使用方法引用,Character::hashCode因為它是不明確的(對象級別和類級別):


Map<Integer, Character> asciiMap2 = CharBuffer.wrap(letters).chars()

        .mapToObj(i -> (char) i)

        .collect(Collectors.toMap(i -> Character.hashCode(i), Function.identity()));

或者,您可以只使用Object::hashCodeinstead ofi -> Character.hashCode(i)因為該類使用以下方法Character覆蓋了它的方法:hashCode()Character.hashCode()


public final class Character ... {

    @Override

    public int hashCode() {

        return Character.hashCode(value);

    }

}

所以最后你可以使用這個:


Map<Integer, Character> asciiMap2 = CharBuffer.wrap(letters).chars()

        .mapToObj(i -> (char) i)

        .collect(Collectors.toMap(Object::hashCode, Function.identity()));


查看完整回答
反對 回復 2023-03-09
  • 3 回答
  • 0 關注
  • 159 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號