1. import java.io.Serializable;
  2. import java.util.ArrayList;
  3. import java.util.HashMap;
  4. import java.util.List;
  5. import java.util.Map;
  6. public class ListSerializer {
  7. /**
  8. * Serializes a list of objects to a string representation.
  9. * Uses default values for null elements.
  10. *
  11. * @param list The list to serialize.
  12. * @param <T> The type of objects in the list. Must be Serializable.
  13. * @return A string representation of the list. Returns null if list is null.
  14. */
  15. public static <T extends Serializable> String serializeList(List<T> list) {
  16. if (list == null) {
  17. return null;
  18. }
  19. StringBuilder sb = new StringBuilder();
  20. sb.append("[");
  21. for (int i = 0; i < list.size(); i++) {
  22. Object element = list.get(i);
  23. if (element == null) {
  24. sb.append("null"); // Use "null" as default value.
  25. } else {
  26. sb.append(element.toString());
  27. }
  28. if (i < list.size() - 1) {
  29. sb.append(", ");
  30. }
  31. }
  32. sb.append("]");
  33. return sb.toString();
  34. }
  35. /**
  36. * Deserializes a string representation of a list back into a list of objects.
  37. * Handles "null" strings as null elements.
  38. *
  39. * @param listString The string representation of the list.
  40. * @param <T> The type of objects in the list. Must be Serializable.
  41. * @return A list of objects. Returns an empty list if listString is null or empty.
  42. * @throws IllegalArgumentException if the listString format is invalid.
  43. */
  44. public static <T extends Serializable> List<T> deserializeList(String listString) {
  45. List<T> list = new ArrayList<>();
  46. if (listString == null || listString.isEmpty()) {
  47. return list; // Return empty list for null or empty string
  48. }
  49. listString = listString.trim(); // Remove leading/trailing whitespace
  50. if (listString.startsWith("[") && listString.endsWith("]")) {
  51. listString = listString.substring(1, listString.length() - 1); // Remove brackets
  52. String[] elements = listString.split(",");
  53. for (String element : elements) {
  54. element = element.trim();
  55. if (element.equals("null")) {
  56. list.add(null); // Add null if string equals "null"
  57. } else {
  58. try {
  59. list.add((T) deserializeObject(element));
  60. } catch (Exception e) {
  61. throw new IllegalArgumentException("Invalid list format: " + element, e);
  62. }
  63. }
  64. }
  65. } else {
  66. throw new IllegalArgumentException("Invalid list format.");
  67. }
  68. return list;
  69. }
  70. private static <T extends Serializable> T deserializeObject(String element) throws IllegalArgumentException {
  71. try {
  72. // Attempt to parse the element as an Integer. You can extend this to handle other types.
  73. return (T) Integer.parseInt(element);
  74. } catch (NumberFormatException e) {
  75. try{
  76. return (T) Double.parseDouble(element);
  77. } catch (NumberFormatException e2){
  78. return (T) new String(element);
  79. }
  80. }
  81. }
  82. public static void main(String[] args) {
  83. // Example Usage
  84. List<String> myList = new ArrayList<>();
  85. myList.add("apple");
  86. myList.add(null);
  87. myList.add("banana");
  88. String serializedList = serializeList(myList);
  89. System.out.println("Serialized List: " + serializedList);
  90. List<String> deserializedList = deserializeList(serializedList);
  91. System.out.println("Deserialized List: " + deserializedList);
  92. List<Integer> intList = new ArrayList<>();
  93. intList.add(1);
  94. intList.add(2);
  95. intList.add(null);
  96. String serializedIntList = serializeList(intList);
  97. System.out.println("Serialized Integer List: " + serializedIntList);
  98. List<Integer> deserializedIntList = deserializeList(serializedIntList);
  99. System.

Add your comment