1

I have xml returned like,

<UniequeTestList>
 <UniequeTest>
  <FirstName>a</FirstName>
  <LastName>b</LastName>
 </UniequeTest>
 <UniequeTest>
  <FirstName>a</FirstName>
  <LastName>b</LastName>
 </UniequeTest>
</UniequeTestList>

I unmarshal it using JaxB in List<UniequeTest>. And obviously that list must have mulitple object which are same. Can I make it unieque at the same time I unmarshall it?

I can't use HashSet or LinkedHashSet to make it unieque at same time. I mean what I want in Unmarshall list is unieque object if value under that object is same.

4

1 回答 1

3

您最终可以使用 JAXB Unmarshaller 的侦听器:

 class JAXBUnmarshallingListener extends Unmarshaller.Listener {

   @Override
   public void beforeUnmarshal(Object target, Object parent) {
   }

   @Override
   public void afterUnmarshal(Object target, Object parent) {
   }
 }

在回调方法中,您可以实现一个简单的缓存并将 JAXB 新创建的对象替换为您自己的唯一实例(新的 JAXB 实例将在之后被垃圾收集,因为没有人继续引用它。注意此方法不会为您保存这些对象的实例化和 gc 的成本。)。

于 2012-08-22T13:48:56.487 回答