JAXB可以产生的ArrayList,而不是名单? [英] Can JAXB generate ArrayList instead of List?

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

问题描述

 <复杂类型名称=书架>
   <序列>
      &下;选择的minOccurs =0maxOccurs为=无限>
         <元素名称=newBook​​类型=字符串/>
         <元素名称=oldBook类型=字符串/>
      < /选择>
   < /序列>
< /复杂类型>

JAXB生成属性作为列表与LT; JAXBElement的<串GT;> 。有没有可以作为ArrayList中会产生什么办法?


解决方案

为什么,有什么好会是你?


  1. 的ArrayList< E> 没有公共
    不在该方法
    列表< E> 接口,所以
    什么,你可以用做
    的ArrayList< E> ,你不能这样做
    与任何其他列表< E> (实际上
    有一个:
    的ArrayList .trimToSize()
    感谢@Joachim绍尔,但它的
    几乎没有必要)。

  2. 这是可怕的做法API来
    接受或返回执行
    类型,而不是潜在的
    接口。我建议你​​来
    按照集合资源
    在Sun Java教程和/或读
    有效的Java由约书亚布洛赫
    (你会得到他的什么的想法
    说起<一个href=\"http://my.safaribooksonline.com/book/programming/java/9780137150021/general-programming/ch08lev1sec8\"相对=nofollow>此短
    preVIEW ,低于报价的来源),以了解更多有关
    集合框架和接口
    用法。

  3. 谁说基础列表
    执行不的ArrayList
    的ArrayList 是最
    常用List实现
    无论如何,所以机会是很高,
    JAXB实际上将返回
    的ArrayList ,它只是不会告诉你
    所以(因为你不需要知道)。

第52项:由它们的接口参照对象(节选)


  

第40条包含的意见,你
  应该使用接口,而不是
  类作为参数类型。更多
  通常,你应该赞成使用
  接口而不是类
  参考对象。 如果适当
  接口类型存在,那么
  参数,返回值,变量,
  和领域都应该申报
  使用接口类型。
的唯一时间
  你真的需要参考的
  对象的类是当你创建
  它的构造函数。为了使这个
  具体的,考虑的情况下,
  矢量,这是一个实现
  在列表接口。进得
  这个打字习惯:


  //好 - 使用的接口类型
清单&LT;订户GT;用户=新的矢量&lt;订户GT;();


  

而不是这样的:


  //错误 - 使用类类型!
矢量&lt;订户GT;用户=新的矢量&lt;订户GT;();

[...]

来源:有效的Java,<一个href=\"http://my.safaribooksonline.com/book/programming/java/9780137150021/general-programming/ch08lev1sec8\"相对=nofollow> $ P $上SafariBooksOnline PVIEW。

<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,而不是名单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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