JAXB 可以生成 ArrayList 而不是 List 吗? [英] Can JAXB generate ArrayList instead of List?

查看:25
本文介绍了JAXB 可以生成 ArrayList 而不是 List 吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<序列><choice minOccurs="0" maxOccurs="unbounded"><element name="newBook" type="string"/><元素名称=oldBook"类型=字符串"/></序列></complexType>

JAXB 将属性生成为 List>.有什么办法可以将它生成为ArrayList吗?

解决方案

为什么,那对你有什么好处?

  1. ArrayList 没有公开不在的方法List 接口,所以有你无能为力ArrayList 你做不到的与任何其他 List(实际上有一个:ArrayList.trimToSize(),谢谢@Joachim Sauer,但它是几乎不需要).
  2. API 的做法很糟糕接受或返回实现类型而不是底层接口.我建议你按照Collections TrailSun Java 教程和/或阅读Joshua Bloch 的《Effective Java》(你会知道他是什么从这个短片谈论 预览,这是下面引用的来源)以了解更多关于集合框架和接口用法.
  3. 谁说底层列表实现不是ArrayList?ArrayList 是最常用的List实现无论如何,所以机会很高JAXB 实际上会返回一个ArrayList,它不会告诉你所以(因为你不需要知道).

项目 52:通过接口引用对象(摘录)

<块引用>

第 40 项包含的建议是您应该使用接口而不是类作为参数类型.更多的一般来说,你应该赞成使用接口而不是类参考对象.如果合适接口类型存在,那么参数、返回值、变量、和字段都应该声明使用接口类型.唯一一次你真的需要参考一个对象的类是你创建的时候它带有一个构造函数.为了使这个具体来说,考虑以下情况Vector,这是一个实现List 界面.进入打字的习惯:

//好 - 使用接口作为类型列表<订阅者>订阅者 = 新向量<订阅者>();

<块引用>

而不是这个:

//不好 - 使用类作为类型!矢量<订阅者>订阅者 = 新向量<订阅者>();

[ ... ]

来源: Effective Java,在 SafariBooksOnline 上预览.

<complexType name="BookShelf">
   <sequence>
      <choice minOccurs="0" maxOccurs="unbounded">
         <element name="newBook" type="string"/>
         <element name="oldBook" type="string"/>
      </choice>
   </sequence>
</complexType>

JAXB generates the property as a List<JAXBElement<String>>. Is there any way it can be generated as ArrayList?

解决方案

Why, what good would that do you?

  1. ArrayList<E> has no public methods that are not in the List<E> interface, so there is nothing you could do with the ArrayList<E> that you couldn't do with any other List<E> (actually there is one: ArrayList.trimToSize(), thanks @Joachim Sauer, but it's hardly ever needed).
  2. It's awful practice for an API to 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.
  3. Who says the underlying List implementation isn't ArrayList? ArrayList is the most commonly-used List implementation anyway, so chances are high that JAXB will actually return an ArrayList, it just won't tell you so (because you don't need to know).

Item 52: Refer to Objects by their Interfaces (excerpt)

Item 40 contains the advice that you should use interfaces rather than classes as parameter types. More generally, you should favor the use of interfaces rather than classes to refer to objects. If appropriate interface types exist, then parameters, return values, variables, and fields should all be declared using interface types. The only time you really need to refer to an object’s class is when you’re creating it with a constructor. To make this concrete, consider the case of Vector, which is an implementation of the List interface. Get in the habit of typing this:

// Good - uses interface as type
List<Subscriber> subscribers = new Vector<Subscriber>();

rather than this:

// Bad - uses class as type!
Vector<Subscriber> subscribers = new Vector<Subscriber>();

[ ... ]

Source: Effective Java, preview on SafariBooksOnline.

这篇关于JAXB 可以生成 ArrayList 而不是 List 吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆