将列表转换为集合

发布于 2024-08-25 13:53:04 字数 199 浏览 3 评论 0原文

我有一些pb。我想在 java 中将列表转换为集合

Collection<T> collection = new Collection<T>(mylList); 

,但出现此错误

无法实例化类型 Collection

i have some pb. I want to cast a List to Collection in java

Collection<T> collection = new Collection<T>(mylList); 

but i have this error

Can not instantiate the type Collection

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

全部不再 2024-09-01 13:53:04

List 已经实现了 Collection - 为什么您需要创建一个新的?

Collection<T> collection = myList;

错误消息是绝对正确的 - 你不能直接实例化接口。如果您想创建现有列表的副本,您可以使用类似以下内容的内容:

Collection<T> collection = new ArrayList<T>(myList);

List<T> already implements Collection<T> - why would you need to create a new one?

Collection<T> collection = myList;

The error message is absolutely right - you can't directly instantiate an interface. If you want to create a copy of the existing list, you could use something like:

Collection<T> collection = new ArrayList<T>(myList);
夜雨飘雪 2024-09-01 13:53:04

转换永远不需要 new

Collection<T> collection = myList;

您甚至不需要显式转换,因为 CollectionList 的超类型,因此它会就像这样工作。

Casting never needs a new:

Collection<T> collection = myList;

You don't even make the cast explicit, because Collection is a super-type of List, so it will work just like this.

咿呀咿呀哟 2024-09-01 13:53:04

将列表转换为集合有多种解决方案

解决方案 1

List<Contact> CONTACTS = new ArrayList<String>();
// fill CONTACTS
Collection<Contact> c = CONTACTS;

解决方案 2

private static final Collection<String> c = new ArrayList<String>(
                                                Arrays.asList("a", "b", "c"));

解决方案 3

private static final Collection<Contact> = new ArrayList<Contact>(
                       Arrays.asList(new Contact("text1", "name1")
                                     new Contact("text2", "name2")));

解决方案 4

List<? extends Contact> col = new ArrayList<Contact>(CONTACTS);

There have multiple solusions to convert list to a collection

Solution 1

List<Contact> CONTACTS = new ArrayList<String>();
// fill CONTACTS
Collection<Contact> c = CONTACTS;

Solution 2

private static final Collection<String> c = new ArrayList<String>(
                                                Arrays.asList("a", "b", "c"));

Solution 3

private static final Collection<Contact> = new ArrayList<Contact>(
                       Arrays.asList(new Contact("text1", "name1")
                                     new Contact("text2", "name2")));

Solution 4

List<? extends Contact> col = new ArrayList<Contact>(CONTACTS);
浅黛梨妆こ 2024-09-01 13:53:04

不知道你的代码,很难回答你的问题,但根据这里的所有信息,我相信问题是你正在尝试使用 Collections.sort 传入定义为 Collection 的对象,而排序不支持那。

第一个问题。为什么客户端的定义如此笼统?为什么不是列表、地图、集合或者更具体的东西?

如果 client 被定义为 List、Map 或 Set,则不会出现此问题,因为这样您就可以直接使用 Collections.sort(client)。

华泰

Not knowing your code, it's a bit hard to answer your question, but based on all the info here, I believe the issue is you're trying to use Collections.sort passing in an object defined as Collection, and sort doesn't support that.

First question. Why is client defined so generically? Why isn't it a List, Map, Set or something a little more specific?

If client was defined as a List, Map or Set, you wouldn't have this issue, as then you'd be able to directly use Collections.sort(client).

HTH

找回味觉 2024-09-01 13:53:04

第一个集合是类接口,您无法实例化。 集合 API

列表 Ver APi 也是一个接口类。

可能是这样

List list = Collections.synchronizedList(new ArrayList(...)); 

在此处输入链接描述

Collection collection= Collections.synchronizedList(new ArrayList(...)); 

First Collection is class Interface and you can not instantiate. Collection API

List Ver APi is also an interface class.

It may be so

List list = Collections.synchronizedList(new ArrayList(...)); 

ver enter link description here

Collection collection= Collections.synchronizedList(new ArrayList(...)); 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文