[英]How can I implement my own generic collection in java?
I have an inteface 'MyCollection' with just two methods : addAll and containsAll which take as a parameter a generic collection.我有一个只有两个方法的接口“MyCollection”:addAll 和 containsAll,它们将通用集合作为参数。 How can I effectively implement these methods in my class so that they would work on any type of collection.
我怎样才能在我的类中有效地实现这些方法,以便它们可以处理任何类型的集合。 This is what I've done so far :
这是我到目前为止所做的:
The interface:界面:
interface MyCollection<T extends Collection> {
boolean containsAll(T c);
boolean addAll(T c);
}
My class where I implement the methods:我在其中实现方法的班级:
public class MyPersonalCollection<E extends Collection> implements MyCollection {
private E myCollection;
public MyPersonalCollection(E myCollection) {
this.myCollection = myCollection;
}
public boolean containsAll(Collection c) {
return myCollection != null && myCollection.containsAll(c);
}
public boolean addAll(Collection c) {
return myCollection != null && myCollection.addAll(c);
}
}
And the tests:和测试:
@Test
public void testIfNewCollectionCanBeAdded() {
ArrayList<String> input = new ArrayList<>();
MyPersonalCollection<ArrayList<String>> myCollection = new MyPersonalCollection<>(input);
input.add("first");
input.add("secon");
input.add("third");
assertTrue(myCollection.addAll(input));
}
@Test
public void testIfMyCollectionContainsAnotherCollection() {
LinkedList<String> list = new LinkedList<>();
MyPersonalCollection<LinkedList<String>> myCollection = new MyPersonalCollection<>(list);
list.add("bacon");
list.add("tuna");
list.add("steak");
assertTrue(myCollection.addAll(list));
}
I also get a warning : Unchecked call to 'containsAll(Collection) as a member of raw type 'Java.Util.Collection" in my class when I call the methods containsAll() and addAll(). So how can I tackle this problem ? Many thanks in advance !我还收到一个警告:当我调用方法 containsAll() 和 addAll() 时,在我的班级中,未经检查地调用 'containsAll(Collection) 作为原始类型'Java.Util.Collection' 的成员。那么我该如何解决这个问题? 提前谢谢了 !
Both E
and T
extend Collection
, but you want to treat a Collection as T
in MyCollection
in this line: E
和T
扩展了Collection
,但您希望在此行MyCollection
Collection 视为MyCollection
中的T
:
return myCollection != null && myCollection.containsAll(c);
Which can be wrong because every Collection
is not from type T
.这可能是错误的,因为每个
Collection
都不是来自类型T
。
Anyway if you are sure that this type casting is safe, simply ignore it and use无论如何,如果您确定这种类型转换是安全的,只需忽略它并使用
@SuppressWarnings("unchecked")
to suppress that warning.抑制该警告。
The problem is that you have to define 2 generic:问题是你必须定义2个泛型:
I fixed your code, now there is no warnings我修复了你的代码,现在没有警告
interface MyCollection<C> {
boolean containsAll(C c);
boolean addAll(C c);
}
public class MyPersonalCollection<E, C extends Collection<E>>
implements MyCollection<C> {
private C collection;
public MyPersonalCollection(C myCollection) {
this.collection = myCollection;
}
public boolean containsAll(C c) {
return collection != null && collection.containsAll(c);
}
public boolean addAll(C c) {
return collection != null && collection.addAll(c);
}
}
You can use your class in the test like this:您可以在测试中使用您的类,如下所示:
MyPersonalCollection<String, LinkedList<String>> myCollection =
new MyPersonalCollection<String, LinkedList<String>>(list);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.