[英]Cast ArrayList to a Generic Abstract Collection
How do I cast an ArrayList to a Generic Collection? 如何将ArrayList强制转换为通用集合? I have highlighted the problem in "SearchListener" with "<-----------This Doesn't work!!!!" 我在“ SearchListener”中用“ <-----------这不起作用!!!”突出显示了问题。
I'm reading about Class.cast(o) but I'm not understanding how to apply it here. 我正在阅读有关Class.cast(o)的信息,但我不了解如何在此处应用它。
public abstract class MyTufinCollection<T> extends AbstractCollection<T> implements ListModel<String>{
} }
public class SearchListener<T,C extends MyTufinCollection<T>> implements DocumentListener{
private JList jlist;
private C staticCollectionOfTypeT;
private List<Predicate<T>> predicateList = new ArrayList<>();
//get the String fields, and build Predicate from them.. add to predicateList.
public SearchListener(JList jlist, List<Predicate<T>> predicateList, C collection) {
this.jlist = jlist;
this.staticCollectionOfTypeT=collection;
this.predicateList=predicateList;
}
@Override
public void insertUpdate(DocumentEvent de) {
filter();
}
@Override
public void removeUpdate(DocumentEvent de) {
filter();
}
@Override
public void changedUpdate(DocumentEvent de) {
filter();
}
private void filter() {
jlist.setModel((ListModel)staticCollectionOfTypeT);
C items = (C) jlist.getModel();
Stream<T> itemStream = items.stream().filter(x ->{
Stream<Predicate<T>> predicateStream= predicateList.stream();
return predicateStream.map(predicate -> predicate.test(x)).reduce((a,b)->a && b).get();
});
ArrayList<T> collect = itemStream.collect(Collectors.toCollection(ArrayList::new));
C name = (C)collect; // <-----------This Doesn't work!!!!
jlist.setModel((ListModel)name);
}
} }
Casting doesn't mean transforming A into B . 强制转换并不意味着将A转换为B。 It only means reducing the view . 这仅意味着缩小视图 。
So if you have an A
and this A
is also a B
, for example because it extends from it. 因此,如果您有一个A
并且该A
也是一个B
,例如,因为它是从A
扩展过来的。 Then you can reduce the view to this A
by casting it to B
. 然后,可以通过将视图强制转换为B
来缩小视图A
For example a List
extends from Collection
, thus you can cast it to Collection
. 例如, List
从Collection
扩展而来,因此您可以将其强制转换为Collection
。 But a String
does not extend from Collection
, you can't cast it. 但是String
不能从Collection
扩展,您不能转换它。
In your case you are trying to cast an ArrayList
to a C
, but C extends MyTufinCollection<T>
. 在您的情况下,您尝试将ArrayList
C extends MyTufinCollection<T>
转换为C
,但是C extends MyTufinCollection<T>
。 ArrayList
doesn't extend from that. ArrayList
不从此扩展。 It is like trying to cast an ArrayList
to a HashSet
, both are Collection
s but it doesn't work for obvious reasons. 就像试图将ArrayList
转换为HashSet
,它们都是Collection
但是由于明显的原因而无法正常工作。
For example MyTufinCollection
problably has some stuff that ArrayList
does not have, how should Java automatically determine what to do. 例如MyTufinCollection
有一些ArrayList
没有的东西,Java应该如何自动确定要做什么。 It can't, casting won't work. 不能,强制转换将不起作用。 You will need to explicitly give a conversion-method. 您将需要明确给出一种转换方法。
The term is the is-a principle. 该术语是is-a原理。 You can read more about casting in JLS§5.5.1 : 您可以在JLS§5.5.1中阅读有关铸造的更多信息:
If
S
(source) is a class type: 如果S
(源)是类类型:If
T
(target) is a class type, then either|S| <: |T|
如果T
(目标)是类类型,则|S| <: |T|
|S| <: |T|
, or|T| <: |S|
,或|T| <: |S|
|T| <: |S|
. 。
Otherwise , a compile-time error occurs. 否则 ,将发生编译时错误 。Furthermore, if there exists a supertype
X
ofT
, and a supertypeY
ofS
, such that bothX
andY
are provably distinct parameterized types ( §4.5 ), and that the erasures ofX
andY
are the same , a compile-time error occurs. 此外,如果存在T
的超类型X
和S
的超类型Y
,从而X
和Y
都是可证明是不同的参数化类型(第4.5节 ),并且X
和Y
的擦除 相同 ,则编译时发生错误 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.