import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class ListSerializer {
/**
* Serializes a list of objects to a string representation.
* Uses default values for null elements.
*
* @param list The list to serialize.
* @param <T> The type of objects in the list. Must be Serializable.
* @return A string representation of the list. Returns null if list is null.
*/
public static <T extends Serializable> String serializeList(List<T> list) {
if (list == null) {
return null;
}
StringBuilder sb = new StringBuilder();
sb.append("[");
for (int i = 0; i < list.size(); i++) {
Object element = list.get(i);
if (element == null) {
sb.append("null"); // Use "null" as default value.
} else {
sb.append(element.toString());
}
if (i < list.size() - 1) {
sb.append(", ");
}
}
sb.append("]");
return sb.toString();
}
/**
* Deserializes a string representation of a list back into a list of objects.
* Handles "null" strings as null elements.
*
* @param listString The string representation of the list.
* @param <T> The type of objects in the list. Must be Serializable.
* @return A list of objects. Returns an empty list if listString is null or empty.
* @throws IllegalArgumentException if the listString format is invalid.
*/
public static <T extends Serializable> List<T> deserializeList(String listString) {
List<T> list = new ArrayList<>();
if (listString == null || listString.isEmpty()) {
return list; // Return empty list for null or empty string
}
listString = listString.trim(); // Remove leading/trailing whitespace
if (listString.startsWith("[") && listString.endsWith("]")) {
listString = listString.substring(1, listString.length() - 1); // Remove brackets
String[] elements = listString.split(",");
for (String element : elements) {
element = element.trim();
if (element.equals("null")) {
list.add(null); // Add null if string equals "null"
} else {
try {
list.add((T) deserializeObject(element));
} catch (Exception e) {
throw new IllegalArgumentException("Invalid list format: " + element, e);
}
}
}
} else {
throw new IllegalArgumentException("Invalid list format.");
}
return list;
}
private static <T extends Serializable> T deserializeObject(String element) throws IllegalArgumentException {
try {
// Attempt to parse the element as an Integer. You can extend this to handle other types.
return (T) Integer.parseInt(element);
} catch (NumberFormatException e) {
try{
return (T) Double.parseDouble(element);
} catch (NumberFormatException e2){
return (T) new String(element);
}
}
}
public static void main(String[] args) {
// Example Usage
List<String> myList = new ArrayList<>();
myList.add("apple");
myList.add(null);
myList.add("banana");
String serializedList = serializeList(myList);
System.out.println("Serialized List: " + serializedList);
List<String> deserializedList = deserializeList(serializedList);
System.out.println("Deserialized List: " + deserializedList);
List<Integer> intList = new ArrayList<>();
intList.add(1);
intList.add(2);
intList.add(null);
String serializedIntList = serializeList(intList);
System.out.println("Serialized Integer List: " + serializedIntList);
List<Integer> deserializedIntList = deserializeList(serializedIntList);
System.
Add your comment