Collections和ImmutableCollections之间的关系是什么?

huangapple go评论104阅读模式
英文:

What is the relationship between Collections and ImmutableCollections

问题

我正在学习Java集合框架。
我在理解集合的层次结构方面遇到了困难。

由于ArrayList实现了List接口,我们可以使用

List<String> list = new ArrayList();

然而,在

List<String> names = List.of("Larry", "Kenny", "Sabrina");

中,List.of()方法返回的是 ImmutableCollections.List12 类。
我之前从未听说过在集合层次结构中有这个类,并且我发现它并没有实现List接口(那它是如何被转换为List的呢?)

另外,我认为很奇怪的是,我不能这样创建ImmutableList:

List<String> names = new ImmutableList("Larry", "Kenny", "Sabrina");

我了解一些关于Kotlin的知识,在可变和不可变之间存在一个层次结构。但是在Java中,我找不到它们之间的任何联系。
这里到底发生了什么?

英文:

I'm studying Java Collections FrameWork.
I'm having trouble understanding a hierarchy of Collections.

Since ArrayList implements List interface, we can use

List&lt;String&gt; list = new ArrayList();

However, In

List&lt;String&gt; names = List.of(&quot;Larry&quot;, &quot;Kenny&quot;, &quot;Sabrina&quot;);

,List.of()method returns ImmutableCollections.List12
I've never heard of that class in the Collection hierarchy and I found it is not implementing List interface(then how it is casted to List?)

in addition, I think It is weird I can't make ImmutalbeList by

List&lt;String&gt; names = new ImmutableList(&quot;Larry&quot;, &quot;Kenny&quot;, &quot;Sabrina&quot;);

I Know little about Kotlin and there was a hierarchy between mutable and immutable. But I can't find any connection between them in Java.
what is happening here?

答案1

得分: 0

Immutable Collection与普通集合相同,但在创建后无法修改(即只读)。您不能添加、删除或更新项目,如果尝试这样做,会抛出UnsupportedOperationException
您不能通过new ImmutableList()来实例化ImmutableList,因为它是一个抽象类。您可以使用ImmutableList.of()方法来获取不可变列表。
如果您想了解更多关于ImmutableList的信息,请参阅Geeksforgeeks - Java中的ImmutableList

英文:

Immutable Collection are same as Normal collection but cannot be modified after creation (i.e it is read only). you cannot add, delete or update items, if you try it throws unsupportetOperationException.
You cannot instantiate ImmutableList by new ImmutableList() because it is a abstract class. you can use ImmutableList.of() method to get immuatabel list.
If you want to know more about immutableList see Geeksforgeeks - ImmutableList in java

答案2

得分: 0

为了深入分析为什么会发生这种情况,让我们从对不可变性的定义开始:

'如果对象在构造后其状态不能更改,则被视为不可变对象。广泛接受的一种创建简单可靠代码的策略是最大程度地依赖不可变对象。

当我们看一个在Java中非常知名的类时,我们也可以确认在我们不能改变状态时。即字符串类。

public class Main {
    public static void main(String[] args) {
        String hello = "hello";
        hello.concat("world");
        System.out.println(hello);
    }
}

如果我们应用concat方法,这不会改变对象的状态,只会返回"hello"。然而,如果我们将结果初始化为一个新变量,它将具有适当的结果,就像这样:

public class Main {
    public static void main(String[] args) {
        String hello = "hello";
        String newHello = hello.concat("world");
        System.out.println(newHello);
    }
}

这个问题在于concat方法欺骗了我。它说它会连接某些内容,但实际上并没有,因为字符串是不可变的。因此,既然不可变对象不能改变其状态,我们也不应该有改变其状态的方法。

由于我们不能为不可变类型拥有这些方法,也不能仅实现接口的部分内容,我们需要创建另一个只包含那些可以从列表中读取但不改变对象状态的方法的接口,比如List.add()方法所做的那样。

英文:

In order to thoroughly analyse why this is happening lets start with a definition on immutability:

'An object is considered immutable if its state cannot change after it is constructed. Maximum reliance on immutable objects is widely accepted as a sound strategy for creating simple, reliable code.

We can also confirm we cannot change the state when we look at one of the well known classes in Java. De string class.

public class Main {
public static void main(String[] args) {
    String hello = &quot;hello&quot;;
    hello.concat(&quot;world&quot;);
    System.out.println(hello);
}

}

This will not change the objects state if we apply the concat method and will only return "hello". However if we initialize the result to a new variable then it will have the appropriate result like this

public class Main {
public static void main(String[] args) {
    String hello = &quot;hello&quot;;
    String newHello = hello.concat(&quot;world&quot;);
    System.out.println(newHello);
}

}

The problem with this is that the concat method is lying to me. It says it will concatenate something, but it actually doesn't because string is immutable. It therefore follows that since an immutable object cannot change its state we also shouldn't have methods that changes its state.

Since we cannot have these methods for an immutable type and we we cannot just implement part of the interface we need to create another interface that only contains methods that can read from the list but don't change the state of the object like for example List.add() does.

huangapple
  • 本文由 发表于 2020年8月16日 15:31:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/63434294.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定