简体   繁体   English

如何将JAXB生成的集合中的All()项目添加到简单的ArrayList中?

[英]How do I addAll() items from a JAXB-generated collection to a simple ArrayList?

I'm trying to use JAXB to pull some elements from a large XML file (technically, it's XTCE .) 我正在尝试使用JAXB从大型XML文件中提取某些元素(从技术上讲,它是XTCE)

JAXB generated a collection of objects for me. JAXB为我生成了一个对象集合。 To get the subset of desired elements, I need to call: 要获取所需元素的子集,我需要调用:

List<SequenceEntryType> org.omg.space.xtce.EntryListType.getParameterRefEntryOrParameterSegmentRefEntryOrContainerRefEntry();

I'd like to fill up an 我想填一个

ArrayList<ParameterRefEntryType> integerParameters;

with the return value using the following code: 使用以下代码返回值:

integerParameters.addAll(...); // calling long JAXB-generated function above

The ParameterRefEntryType derives from SequenceEntryType , so this should be kosher as long as I used an appropriate cast. ParameterRefEntryType派生自SequenceEntryType ,因此只要我使用了适当的强制转换,就应该是洁净的。

However, Eclipse reports: 但是,Eclipse报告:

"The method addAll(Collection < ? extends ParameterRefEntryType>) in the type ArrayList is not applicable for the arguments (ParameterRefEntryType)." “类型为ArrayList的方法addAll(Collection <?扩展ParameterRefEntryType>)不适用于参数(ParameterRefEntryType)。”

I'm rusty on Java generic Collections, and I don't fully understand what's going on here. 我对Java通用Collection感到生疏,并且我不完全了解这里发生了什么。 It seems like I need to redefine my ArrayList to only accept items of type ParameterRefEntryType. 似乎我需要重新定义ArrayList以仅接受ParameterRefEntryType类型的项目。 Can I use addAll() like this, or do I need to write my own loop to add the desired elements? 我可以像这样使用addAll(),还是需要编写自己的循环来添加所需的元素?

The alternative seems to be: 替代方案似乎是:

List<SequenceEntryType> entries = 
    entryList.getParameterRefEntryOrParameterSegmentRefEntryOrContainerRefEntry();
Iterator<SequenceEntryType> entryIterator = entries.iterator();
while (entryIterator.hasNext()) {
    SequenceEntryType currEntry = entryIterator.next();
    if (currEntry instanceof ParameterRefEntryType) {
        _integers.add(currEntry);
    }
}

If this method is necessary, I'll definitely consider using lambdaj . 如果必须使用此方法,我一定会考虑使用lambdaj

I can't give you hints about JAXB but this is a bit about collections: 我不能给您关于JAXB的提示,但这有点关于集合:

You have: 你有:

1. ParameterRefEntryType extends SequenceEntryType
2. ArrayList<ParameterRefEntryType> integerParameters;

and now look at definiitions add and addAll: 现在看一下定义add和addAll:

public boolean addAll(Collection<? extends E> c)
public boolean add(E e)

In your case E is ParameterRefEntryType 在您的情况下,E为ParameterRefEntryType

so integerParameters.addAll() can work only with ? 所以integerParameters.addAll()只能与? extends ParameterRefEntryType it's mean that you can put there only elements ParameterRefEntryType and childrens 扩展ParameterRefEntryType意味着您只能在其中放置ParameterRefEntryType和childrens元素

add() has similar requirement so if you want add this object you can do this manually, but you must cast your currEntry: add()具有类似的要求,因此,如果要添加此对象,可以手动执行此操作,但是必须强制转换currEntry:

integerParameters.add((ParameterRefEntryType) currEntry);

Your loop can be written a bit shorter: 您的循环可以写得更短:

for(SequenceEntryType currEntry :
     entryList.getParameterRefEntryOrParameterSegmentRefEntryOrContainerRefEntry()) {

    if (currEntry instanceof ParameterRefEntryType) {
        _integers.add((ParameterRefEntryType)currEntry);
    }
}

Of course, it looks better if your method names are not so absurdly long. 当然,如果您的方法名不是那么长,那么看起来会更好。

smas already said why it does not work with addAll (the original list can contain elements which are not of type ParameterRefEntryType). smas已经说过为什么不能与addAll一起addAll (原始列表可以包含非ParameterRefEntryType类型的元素)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM