JAXB 可以生成 ArrayList 而不是 List 吗?
<complexType name="BookShelf">
<sequence>
<choice minOccurs="0" maxOccurs="unbounded">
<element name="newBook" type="string"/>
<element name="oldBook" type="string"/>
</choice>
</sequence>
</complexType>
JAXB 将属性生成为 List
。有什么办法可以将其生成为ArrayList吗?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
为什么,那对你有什么好处?
ArrayList
没有公共方法中没有的方法
列表
接口,所以有你无能为力
ArrayList
你做不到与任何其他
List
(实际上有一个:
ArrayList.trimToSize( )
,谢谢@Joachim Sauer,但它是
几乎不需要)。
接受或返回实施
类型而不是底层
接口。我建议你
遵循 Collections Trail
Sun Java 教程和/或阅读
Joshua Bloch 的《Effective Java》
(你就会知道他是什么
谈论这个简短的
预览,这是下面引用的来源)以了解有关
集合框架和接口
用法。
实现不是 ArrayList 吗?
ArrayList
是最多的常用的List实现
无论如何,所以可能性很高
JAXB 实际上会返回一个
ArrayList
,它只是不会告诉你所以(因为你不需要知道)。
第 52 条:通过接口引用对象(摘录)
[...]
来源:有效的Java,在 SafariBooksOnline 上预览。
Why, what good would that do you?
ArrayList<E>
has no publicmethods that are not in the
List<E>
interface, so there isnothing you could do with the
ArrayList<E>
that you couldn't dowith any other
List<E>
(actuallythere is one:
ArrayList.trimToSize()
,thanks @Joachim Sauer, but it's
hardly ever needed).
accept or return implementation
types instead of the underlying
interfaces. I'd suggest you to
follow the Collections Trail of
the Sun Java Tutorial and / or read
Effective Java by Joshua Bloch
(you'll get an idea of what he's
talking about from this short
preview, which is the source of the quote below) to learn more about the
Collections framework and interface
usage.
implementation isn't
ArrayList
?ArrayList
is the mostcommonly-used List implementation
anyway, so chances are high that
JAXB will actually return an
ArrayList
, it just won't tell youso (because you don't need to know).
Item 52: Refer to Objects by their Interfaces (excerpt)
[ ... ]
Source: Effective Java, preview on SafariBooksOnline.
默认情况下,该属性将是一个 List,底层实现将是一个 ArrayList。当然,您可以使用 JAXB 自定义来更改底层实现,或者使用您自己的类和 ArrayList 类型的属性(尽管出于其他答案中提到的原因,这很少是一个好主意)。
默认 JAXB 生成
给定您的 XML 模式:
使用以下命令行:
JAXB 将生成以下类:
自定义生成
默认情况下,JAXB 的属性类型为 List,其中包含底层实现是ArrayList。如果您希望控制底层实现,您可以使用外部绑定文件,例如:
和以下 XJC 调用:
要改为获取以下类:
使用您自己的类:
您还可以使用您自己的类ArrayList 类型的属性(尽管出于其他答案中提到的原因,这很少是一个好主意)。
了解更多信息:
By default the property will be a List and the underlying implementation will be an ArrayList. Of course you can use JAXB customizations to change the underlying implementation, or use your own class with a property of type ArrayList (although for the reasons mentioned in other answers this is rarely a good idea).
Default JAXB Generation
Given your XML Schema:
Using the following command line:
JAXB will generate the following class:
Customizing the Generation
By default JAXB will have the property type be List with the underlying implementation being ArrayList. If you wish to control the underlying implementation you can use an external binding file like:
And the following XJC call:
To get the following class instead:
Using your own class:
You can also use your own class with a property of type ArrayList (although for the reasons mentioned in other answers this is rarely a good idea).
For More Information:
您无法更改 API 生成列表的事实。
但是,假设底层实现实际上生成了一个 ArrayList,您始终可以将其转换为 ArrayList:
或者如果它不是一个 arraylist(即尝试上述操作时出现异常...),您可以生成一个新的 ArrayList,其中包含列表中的相同元素。
然而,一般来说,您不需要执行任何此操作:只要有可能,针对接口抽象而不是底层具体类进行编码总是更好。
You can't change the fact that the API generates a List.
However, assuming that the underlying implementation actually produces an ArrayList you can always just cast it to an ArrayList:
Or if it isn't an arraylist (i.e. you get an exception trying the above...), you can generate a new ArrayList containing the same elements of the list.
In general however, you shouldn't need to do any of this: it's always better to code against an interface abstraction rather than the underlying concrete class whenever you can.