Unable to Serialize Hashmap ,java.io.NotSerializableException - java

public class Common implements Serializable{
private static HashMap<Integer,List<LevelList>> levelListMap = new HashMap<>();
public static Map<Integer, List<LevelList>> getLevelListMap(Context context) {
File file = new File(context.getDir("data", MODE_PRIVATE), "map");
ObjectInputStream inputStream = null;
try {
inputStream = new ObjectInputStream(new FileInputStream(file));
levelListMap = (HashMap<Integer, List<LevelList>>) inputStream.readObject();
} catch (Exception e) {
e.printStackTrace();
}
return levelListMap;
} ...
}
I am unable to serialize hashmap.I keep getting java.io.NotSerializableException for
levelListMap = (HashMap<Integer, List<LevelList>>) inputStream.readObject();
public class LevelList implements Serializable{
public int id;
public String title;
public String imgurl;
public String songurl;
public String songtext;
boolean isFavourite;
public void release() {
}
public void setFavourite(boolean favourite) {
isFavourite = favourite;
}
public boolean isFavourite(){
return isFavourite;
}
}

HashMap is serializable, but the keys and values must also be serializable. Make sure that all keys and values are serializable, and that their fields are also serializable (excluding transient and static members).
Edit:
HashMap<Integer,List<LevelList>>, is your List implementation serializable?

Check this link How to serialize a list in Java. Standard implementation of List, i.e. ArrayList, LinkedList, etc. are serializable.
If you declare your List as one of the List subtypes such as ArrayList<LevelList> levelList = new ArrayList<LevelList>(); then it should be serializable out of the box. Otherwise you will need to cast in a safe way, such as setting the List implementation with <T extends List<Foo> & Serializable> setFooList(T list) as suggested by Theodore Murdock in that answer thread.

Related

Deserialization of a singleton object

Hi i'm trying to find a way to serialize and deserialize a singleton object while retrieving the object that was serialized before e.g: after I want to add doctors to my hospital object and after it being deserialized i get my doctors list back.
I read that in order to serialize a singleton I need to add readResolve() method.
but still every time i rebuild my object I'm getting new instance and it's empty, although i'm not getting any errors
public class Hospital implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
private static Hospital theHospital = null;
private HashMap<Integer, Doctor> doctors;
private HashMap<Integer, Nurse> nurses;
private HashMap<Integer, PatientReport> reports;
private HashMap<Integer, Patient> patients;
private HashMap<Integer, Patient> hotelPatients;
private HashMap<Integer, Disease> diseases;
private HashMap<Integer, Department> departments;
private HashMap<String,Department> departmentsByName;
private HashMap<Patient, HashSet<Doctor>> doctorsList;
private HashMap<Patient, HashSet<Nurse>> nursesList;
private TreeMap<Integer, Nurse> nurseShiftSet;
private ArrayList<SubDepartment> subSet;
private HashMap<String,HashMap<String,Doctor>> docUser;
private HashMap<String,HashMap<String,Nurse>> nurseUser;
private TreeSet<Department> DepList;
public static Hospital getInstance() {
if (theHospital == null){
theHospital = new Hospital();
}
return theHospital;
}
this is the object getInstance()
and here is the methods that i have used to write and read the serialized file .
private static ObjectInputStream input;
public static void writeObject(Hospital h) {
try {
FileOutputStream file = new FileOutputStream("Hospital.ser");
ObjectOutputStream out = new ObjectOutputStream(file);
out.writeObject(h);
out.close();
file.close();
} catch (IOException e) {
System.out.println("Error in creating the file");
}
}
public static Hospital readObject() {
try {
FileInputStream file = new FileInputStream("Hospital.ser");
input = new ObjectInputStream(file);
Hospital h = (Hospital) input.readObject();
file.close();
input.close();
return h;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
Update
Example :
Hospital h = Hospital.getInstance();
Department department = new Department("department name");
HashMap<String,Department> map = h.getDepartmentsByName();
// adding the department to the hospital instance .
map.put(department.getName(),department);
Serializing.writeObject(h); // writing the given instance h.
map.remove(d.getName());
// removing from the current instance after serializing.
h = Serializing.readObject(); // deserializing from the file.
map = h.getDepartmentsByName();
map.get(d.getName);// i'm expecting to return the department d , but it returns null.
Use object-mappers. Also your question doesn’t explain what exactly you are looking for. Share some examples of the same to explain the problem. Also, serialize and deserialise doesn’t seem right the way you are doing.

Need for custom serializers while using derivatives of ArrayList as value in Hazelcast

I have an IMAP whose key is a String and value is a derivative of ArrayList. I need to run EntryProcessor on a key of this map. Also note that Employee is a POJO which implements Serializable interface.
When I executed the code given below, the code prints "Why so !" and I got ClassCastException which mentioned that java.util.ArrayList cannot be cast to Employees in the process() method of ListValueEntryProcessor given below.
Q1. I learnt that I need to add custom serializer for my type (Employees) so that it could be serialized as Employees object and not as ArrayList object. I would like to know why is it mandatory to add a "custom serializer" for a built-in type like an ArrayList whose items are also marked Serializable ?
public class Employees extends ArrayList implements Serializable
{
private static final long serialVersionUID = 1L;
/**
Constructs a new employees object
*/
public Employees()
{
super();
}
}
HazelcastInstance hazelcastInstance = HazelcastHelper.getHazelcastInstance();
IMap<String, Employees> empMap = hazelcastInstance.getMap("employeesMap");
Employees empList = new Employees();
Employee employee = new Employee();
empList.add(employee);
empMap.put("companyId", employees);
empMap.executeOnKey("companyId", new IncSalaryEntryProcessor());
public static class ListValueEntryProcessor extends AbstractEntryProcessor<String, Employees>
{
private static final long serialVersionUID = 1L;
#Override
public Object process(Entry<String, Employees> arg0)
{
if(! (arg0.getValue() instanceof Employees))
{
System.out.println("Why so !");
}
// ClassCastException thrown here.
Employees empList = arg0.getValue();
return true;
}
}
This is a bug on our side. I created a bugreport:
https://github.com/hazelcast/hazelcast/issues/6455
The following code should resolve your problem for the time being:
public class Main {
public static void main(String[] args){
HazelcastInstance hz = Hazelcast.newHazelcastInstance();
IMap<String,Employees> map = hz.getMap("foo");
map.put("1", new Employees());
Employees employees = map.get("1");
System.out.println(employees);
}
static class Employees extends ArrayList implements DataSerializable {
#Override
public void writeData(ObjectDataOutput out) throws IOException {
out.writeInt(size());
for(Object item: this){
out.writeObject(item);
}
}
#Override
public void readData(ObjectDataInput in) throws IOException {
int size = in.readInt();
for(int k=0;k<size;k++){
add(in.readObject());
}
}
}
}

How to serialize/deserialize (JSON) ArrayLIst<A> which has an ArrayLIst<B> as field in Java

I'm new in Android programming's world and I've some problems using JSON to serialize and deserialize an ArrayList of custom objects. I know how to serialize an object, but not if it has an ArrayList as a field.
I found the GSON library but I don't really know how to use it.
I have an ArrayList of a class A which has an ArrayList of a class B. I'm serializing class A using a toJSON() method:
public class A
{
//Some fields like title, id...
private String mTitle;
private ArrayList<B> mArray; //I don't know how to serialize/deserialize this field
public A(){...}
public A(JSONObject json){...}
public JSONObject toJSON() throws JSONException //method I use to convert my object into a JSONObject
{
JSONObject json = new JSONObject();
json.put("TITLE",mTitle);
return json;
}
}
public class B
{
//some fields like a double, String...
public B(){...}
}
In case of collection, you must specify the type.
See a Guide User
In your case, I think this will solve:
Type collectionType = new TypeToken<Collection<A>>(){}.getType();
Collection<A> list = gson.fromJson(json, collectionType);
Where A is your class.
Are you trying to use Parcelable? Try something like this (pseudo code):
public class ParcelableObject implements Parcelable{
List<ObjectB> _customObjectList = new ArrayList<ObjectB>();
#Override
public void writeToParcel(Parcel outParcel_, int arg1) {
outParcel_.writeTypedList(_customObjectList); //where _customObjectList is your list variable
}
//Then in your constructor which takes in a Parcel:
public ParcelableObject(Parcel parcelIn_) {
parcelIn_.readTypedList(_customObjectList, ObjectB.CREATOR);
}
}
ObjectB
public class ObjectB implements Parcelable{
int _randomInt;
public static final Parcelable.Creator<ObjectB> CREATOR = new Parcelable.Creator<ObjectB>() {
#Override
public ObjectBcreateFromParcel(Parcel in) {
return new ObjectB(in);
}
#Override
public ObjectB[] newArray(int size) {
return new ObjectB[size];
}
};
public ObjectB(Parcel parcelIn_) {
_randomInt = parcelIn_.readInt();
}
#Override
public void writeToParcel(Parcel out_, int flags) {
out_.writeInt(_randomInt);
}
}
What is better is to do something like that:
List<YourObject> myList = new Genson().deserialize(dta.toString(), List.class);

Java - Instantiating generic typed class

I have a class for example
public class Example<T> {...}
I would like to instantiate class Example with a specific type class which I know. Pseudocode would look something like that
public Example<T> createTypedExample(Class exampleClass, Class typeClass) {
exampleClass.newInstance(typeClass); // made-up
}
So that this would give me same result
Example<String> ex = new Example<String>();
ex = createTypedExample(Example.class, String.class);
Is it possible in Java?
Since, the return type i.e. the class of the new instance is fixed; there's no need to pass it to the method. Instead, add a static factory method to your Example class as
public class Example<T> {
private T data;
static <T> Example<T> newTypedExample(Class<T> type) {
return new Example<T>();
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
}
Now, here's how you would create generic Example instances.
// String
Example<String> strTypedExample = Example.newTypedExample(String.class);
strTypedExample.setData("String Data");
System.out.println(strTypedExample.getData()); // String Data
// Integer
Example<Integer> intTypedExample = Example.newTypedExample(Integer.class);
intTypedExample.setData(123);
System.out.println(intTypedExample.getData()); // 123

How to efficiently get the values from a HashMap with a static class?

I have a class with a HashMap<k,v>.
The type of the values of this HashMap is a static class which has two different objects as attributes. i.e.,
public class Example {
private HashMap<String, StaticClassExample> map;
private static class StaticClassExample {
private Object1 o1;
private Object2 o2;
//...
}
//...
}
And my question is how can I do this operation efficiently:
public List<Object1> getAllObject1() {}
I know that I can do: map.values() and then iterate the values collection and get Object1 from each StaticClassExample, but this wouldn't be efficient.
It's possible what I ask or I must create another hashmap for my purpose?
If you don't mind some memory overhead, you could keep a separate list with the o1-values:
public class HashMapList
{
private HashMap<String, StaticClassExample> map = new HashMap<String, HashMapList.StaticClassExample>();
private List<Object> o1List = new LinkedList<Object>();
public static class StaticClassExample
{
private Object o1;
private Object o2;
}
public void addStaticClassExample(String key, StaticClassExample example)
{
StaticClassExample oldVal = map.put(key, example);
if(oldVal != null)
{
o1List.remove(oldVal.o1);
}
o1List.add(example.o1);
}
public StaticClassExample getStaticClassExampleByKey(String key)
{
return map.get(key);
}
public void removeStaticClassExampleByKey(String key)
{
StaticClassExample removed = map.remove(key);
if(removed != null)
{
o1List.remove(removed.o1);
}
}
public List<Object> getAllObject1()
{
return Collections.unmodifiableList(o1List);
}
}
Of course, this requires you to encapsule the HashMap inside the class and never give a straight access to it, because then someone using the class could modify the HashMap directly, and the List would no longer be in sync with the Map. Note that getAllObject1 returns an unmodifiable view of the internal list, so it can't be modified from outside of the class.

Categories

Resources