Java:对不可修改的列表进行排序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1797550/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java: Sort an unmodifiable list
提问by Lista
How would one do this?
怎么做呢?
I have tried creating a new, empty list, then copying unmodifiable list's elements to it, but I'm getting unsupported operation error.
我尝试创建一个新的空列表,然后将不可修改列表的元素复制到它,但我收到了不受支持的操作错误。
Any help is appreciated.
任何帮助表示赞赏。
采纳答案by u7867
Are you creating an empty list using Collections.emptyList()
? If so, that is an unmodifiable list too and that may be the source of your problem.
您是否正在使用创建一个空列表Collections.emptyList()
?如果是这样,那也是一个不可修改的列表,这可能是您问题的根源。
Create a new list using the constructor for your List implementation, such as new ArrayList()
. You can pass the original list into the constructor or else use the addAll()
method.
使用构造函数为您的 List 实现创建一个新列表,例如new ArrayList()
. 您可以将原始列表传递给构造函数,或者使用该addAll()
方法。
回答by Bozho
List unmodifiableList = Collections.unmodifiableList(list);
List newList = new ArrayList(unmodifiableList);
Collections.sort(newList);
The constructor of ArrayList
takes an existing list, reads its elements (without modifying them!), and adds them to the new List.
的构造函数ArrayList
接受一个现有列表,读取其元素(不修改它们!),并将它们添加到新列表中。
回答by Andreas Dolk
It should work as described. Elements in an unmodifiable List are not unmodifiable themselves. Of course you can't sort an unmodifiable List, because it has to be rewritten with add and delete operations, and those are not allowed.
它应该像描述的那样工作。不可修改列表中的元素本身不是不可修改的。当然你不能对一个不可修改的List进行排序,因为它必须通过添加和删除操作进行重写,而这些操作是不允许的。
Guess the error is somewhere else, best would be to show your code :)
猜测错误在其他地方,最好是显示您的代码:)
回答by Kevin Bourrillion
FWIW, with google-collectionsit's a one-liner:
FWIW,使用google-collections,它是单行的:
List<Foo> sorted = Ordering.natural().sortedCopy(unmodifiableList);
回答by Kevin Brock
I will answer your second question:
我来回答你的第二个问题:
You cannot add to your 'taskListModifiable' list because that is an 'unmodifiable' list, which in Java means that it is immutable. An unmodifiable/immutable list or collection of any kind cannot be directly changed in any way - you cannot add elements to it, remove elements from it, or change references to existing elements.
您不能添加到“taskListModifiable”列表中,因为这是一个“不可修改”列表,在 Java 中这意味着它是不可变的。任何类型的不可修改/不可变列表或集合都不能以任何方式直接更改 - 您不能向其中添加元素、从中删除元素或更改对现有元素的引用。
Note that existing elements in the list/collection may be changed if they are themselves mutable, but references to them from the list cannot be changed. That is, until the list goes completely out of scope and is garbage collected, the unmodifiable list will always contain those same references.
请注意,如果列表/集合中的现有元素本身是可变的,则可能会更改它们,但不能更改列表中对它们的引用。也就是说,在列表完全超出范围并被垃圾收集之前,不可修改的列表将始终包含那些相同的引用。
Now, in your case, Collections.emptyList() returns an unmodifiable EMPTYlist. This is actually always the same instance since it is immutable and always empty. You cannot add to it because of the rule that an immutable list cannot be expanded (added to). Since it is empty there is also nothing in it that can be changed.
现在,在您的情况下, Collections.emptyList() 返回一个不可修改的EMPTY列表。这实际上总是同一个实例,因为它是不可变的并且总是空的。由于无法扩展(添加到)不可变列表的规则,您无法添加到它。因为它是空的,所以里面也没有什么可以改变的。
I should add to make it even more clear that Collection.emptyList() does not create an empty list. It just returns a reference to a singleton list instance.
我应该补充一点,以便更清楚地说明 Collection.emptyList() 不会创建空列表。它只返回对单例列表实例的引用。
回答by Soulman
In Java 8, you can use the streams API. For instance:
在 Java 8 中,您可以使用流 API。例如:
List<?> sortedList = unsortedList.stream().sorted().collect(Collectors.toList());