How to Serialize a List in Java

How to Serialize a list in java?

All standard implementations of java.util.List already implement java.io.Serializable.

So even though java.util.List itself is not a subtype of java.io.Serializable, it should be safe to cast the list to Serializable, as long as you know it's one of the standard implementations like ArrayList or LinkedList.

If you're not sure, then copy the list first (using something like new ArrayList(myList)), then you know it's serializable.

Java List serialization

I figured out the answer, apparently every time I was calling the save class, I was not overwritting the file but appending to it, so I was only reading the first array list being stored, pretty simple fix I just added the lines

                File file = new File("data/userlist.ser");
file.delete();

to the beginning of my saveUsers function to clear the file, now it works perfectly.

Why doesn't java.util.List implement Serializable?

List does not implement Serializable because is it not a key requirement for a list. There is no guarantee (or need) that every possible implementation of a List can be serialized.

LinkedList and ArrayList choose to do so, but that is specific to their implementation. Other List implementations may not be Serializable.

How to generically specify a Serializable List

You need to declare your variable type as Result<? extends List<Integer>>.

The type checking knows that List isn't serializable, but a subtype of List can be serializable.

Here is some sample code. The interface implementation was just done with anonymous inner classes. You can see that the getResult will return a List<Integer> on the 2nd object

   Result<Integer> res = new Result<Integer>() {

Integer myInteger;

private static final long serialVersionUID = 1L;

@Override
public Integer getResult() {
return myInteger;
}

@Override
public void addResult(Integer input) {
this.myInteger = input;
}
};

Integer check = res.getResult();

Result<? extends List<Integer>> res2 = new Result<ArrayList<Integer>>() {

ArrayList<Integer> myList;

private static final long serialVersionUID = 1L;

@Override
public ArrayList<Integer> getResult() {
return myList;
}

@Override
public void addResult(ArrayList<Integer> input) {
this.myList = input;
}

};

List<Integer> check2 = res2.getResult();

Edit: Made the example more complete by implementing a void addResult(T input) interface method

How to serialize a list with Jackson without the list name?

This is just a slight variation on the @Andreas answer.

@JacksonXmlElementWrapper(localName = "ignoredName", useWrapping = false)
@JacksonXmlProperty(localName = "item")
private List<Item> itemList;

Use the @JacksonXmlElementWrapper annotation to identify that it is a list of stuff
and you don't want a wrapper element.
Use the @JacksonXmlProperty annotation to identify the element name.

This will cause a repetition of <item> xml elements in your output; one per entry in the itemList variable.



Related Topics



Leave a reply



Submit