在Kotlin中实现返回Collection的Java方法 [英] Implement Java method that returns Collection in Kotlin

查看:77
本文介绍了在Kotlin中实现返回Collection的Java方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将Kotlin与Spring Security结合使用.实施此方法时:

I'm using Kotlin with Spring Security. When implementing this method:

public interface UserDetails extends Serializable {
  Collection<? extends GrantedAuthority> getAuthorities();
  ...
}

我注意到Intellij生成了此Kotlin代码:

I noticed Intellij generates this Kotlin code:

override fun getAuthorities(): MutableCollection<out GrantedAuthority> { ... }

...这很有意义,因为java.util.Collection是可变的.

... which makes sense since java.util.Collection is mutable.

但是,如果我将MutableCollection替换为Collection(应该是kotlin.collections.Collection IIRC),代码仍然可以正常编译.这使我感到困惑,因为kotlin.collections.CollectionMutableCollection继承的只读接口.我可以为声明为返回可变集合的方法返回不可变集合.

However, if I replaced the MutableCollection with Collection (which should be kotlin.collections.Collection IIRC), the code still compiles fine. This confuses me because kotlin.collections.Collection is a read-only interface that MutableCollection inherits from. It doesn't make sense that I can return an immutable collection for a method that's declared to return a mutable collection.

所以我的问题是:

  1. 为什么使用kotlin.collections.Collection不会在这里引起错误?
  2. 等同于java.util.Collection的正确Kotlin返回类型是什么?
  1. Why does using kotlin.collections.Collection not cause an error here?
  2. What's the correct Kotlin return type that's equivalent to java.util.Collection?

推荐答案

  1. 为什么使用kotlin.collections.Collection不会在这里引起错误?
  1. Why does using kotlin.collections.Collection not cause an error here?

如果您不知道,那么Kotlin中的可变和非可变collection接口都会解析为Java中的一个接口.因此,Kotlin的CollectionMutableCollection在Java中都是java.util.Collection,Kotlin的ListMutableList在Java中都是java.util.List,依此类推.

If you're not aware, the mutable and non-mutable collection interfaces in Kotlin both resolve to one interface in Java. So Kotlin's Collection and MutableCollection are both java.util.Collection in Java, Kotlin's List and MutableList are both java.util.List in Java, and so on.

此外,科特林还使用声明站点差异. Kotlin Collection接口定义为Collection<out E>,这意味着该接口是类型E的生产者.结果,具有:

Also, Kotlin has what it terms declaration-site variance. The Kotlin Collection interface is defined as Collection<out E> which means the interface is a producer of type E. As a consequence, having:

val col: Collection<GrantedAuthority> = ...

几乎与相同:

val col: MutableCollection<out GrantedAuthority> = ...

我之所以这么说是因为,尽管MutableCollection<out ...>阻止任何人添加任何元素,但它并不能阻止一个人对集合进行完全变异.例如,您仍然可以在集合上调用clear().

I say nearly because, while MutableCollection<out ...> prevents anyone from adding any elements, it does not prevent one from mutating the collection entirely. For example, you could still call clear() on the collection.

  1. 等同于java.util.Collection的正确Kotlin返回类型是什么?
  1. What's the correct Kotlin return type that's equivalent to java.util.Collection?

Java没有声明站点差异.它还没有区分可变集合和非可变集合(至少在接口级别上没有).

Java does not have declaration-site variance. It also doesn't distinguish between mutable and non-mutable collections (at least not at the interface level).

从技术上讲,这意味着以下Java类型的最佳匹配:

This means that, technically, the best match of the following Java type:

Collection<? extends GrantedAuthority>

以下是Kotlin类型:

Is the following Kotlin type:

MutableCollection<out GrantedAuthority>

这两种类型都是可变的集合类型,并被定义为类型GrantedAuthority生产者(通过Java中的? extends和Kotlin中的out).两种类型都不能让您向集合中添加任何内容.

Both types are mutable collection types and are defined to be producers of type GrantedAuthority (via ? extends in Java and out in Kotlin). Neither type let's you add anything to the collection.

但是,如果您的getAuthorities()方法应该返回一个不可修改的集合(例如Java中的Collections.unmodifiableCollection(collection)),则从Java到Kotlin的更合适的转换将是使用:

However, if your getAuthorities() method is supposed to return an unmodifiable collection (e.g. Collections.unmodifiableCollection(collection) in Java) then the more appropriate conversion from Java to Kotlin would be to use:

kotlin.collections.Collection<GrantedAuthority>

这篇关于在Kotlin中实现返回Collection的Java方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆