問題タブ [method-reference]
For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.
java - System.out::println に相当するラムダ式は何ですか?
のメソッド参照を使用している次の Java コードを見つけましたSystem.out.println
。
に相当するラムダ式はSystem.out::println
何ですか?
java - この場合、メソッド参照を使用する利点は何ですか?
次のように2つの変数があります。
List<Basket> bigBasket = new ArrayList<>();
Basket basket = new Basket();
bigBasket
と のアイテム間の関係を追加するために、次のコードを書きましたbasket
。
これで問題ありませんが、IntelliJはコードを検査し、メソッド参照を使用してコードを改善することを提案したので、次のようになりました:
上記は行数が少ないですが、実際の利点は何ですか? 私は Java 8 の機能の利点を完全に把握しているわけではないので、何が起こっているのか本当に理解していないのはおそらくそのためです。
私の意見では、「改善された」バージョンのコードは、Java 8 の機能についてあまり知識がないと仮定すると、標準の for ループに対して何が起こっているのかをすぐに理解できるという意味で、あまり読みやすくありません。
誰かが「改善された」コードと標準の for ループの利点を説明できますか?
編集: ラムダへの誤った参照を削除しました。コードはメソッド参照を使用しているだけです - 私の側の専門用語の間違いです!
java - Java 8 メソッド参照 - 一度だけ逆参照されますか?
メソッド参照と混同しています。次のスクリプトを検討してください。
したがって、次のようです。
- メソッド参照は多態的であるため、コンパイラはメソッド参照をインライン化できません。それらはコンパイル時には解決されませんが、実行時に解決されます。
- しかし、次
i::m
のように動作しませんi.m()
...
だから私の質問は:
メソッド参照はリフレクションを使用していますか? そして、なぜ彼らは一度だけするのですか?
java - Java 8 連鎖メソッド参照?
典型的な Java Bean があるとします。
そして、BiConsumer の助けを借りて、セッターを呼び出すより抽象的な方法を作成したいと思います。
(myBean, id) -> myBean.getList().add(id)
ラムダをチェーンされたメソッド参照、(myBean.getList())::add
またはmyBean::getList::add
何か他のものに置き換える方法はありますか?
java-8 - How do I use [TypeArguments] with a constructor reference in Java 8?
Section 15.13 of the Java Language Specification for Java 8 describes this form of the method reference syntax for creating a constructor reference:
For example:
That all works fine, but it seems that absolutely anything (excluding primitives) can be also supplied for the [TypeArguments] and everything still works:
Here's a silly example to prove the point:
A few questions arising:
[1] Since the String class doesn't even use generics, is it valid that the compiler allows the creation of that test2 constructor reference with those meaningless [TypeArguments]?
[2] What would be a meaningful example of using [TypeArguments] when creating a constructor reference?
[3] Under what conditions is it essential to specify [TypeArguments] when creating a constructor reference?