Class MultiList<E>
- Type Parameters:
E- The element type of this list.
- All Implemented Interfaces:
Iterable<E>,Collection<E>,List<E>
This class allows multiple lists to be viewed and accessed as if they were merged into a single list, without actually copying the elements. Modifications made through the iterator or list iterator affect the underlying lists.
Features:
- Zero-Copy Composition: No data is copied when creating a MultiList; it simply wraps the provided lists
- Transparent Access: Accessing elements by index or iterating over a MultiList seamlessly traverses all underlying lists in order
- Modification Support: Elements can be removed via the iterator's
Iterator.remove()method - Efficient Size Calculation: The size is computed by summing the sizes of all underlying lists
- Enumeration Support: Provides an
Enumerationview viaenumerator()
Usage:
Behavior Notes:
- The order of access follows the order of lists as provided in the constructor
- Within each list, access order follows the list's natural order
- The underlying lists must not be
null , but can be empty - Modifications via
Iterator.remove()orListIterator.remove()are delegated to the underlying list's iterator - This class does not support
AbstractList.add(Object),AbstractList.add(int, Object),AbstractList.set(int, Object), orAbstractList.remove(int)operations - The
size()method recomputes the sum each time it's called (not cached) - The
get(int)method locates the element by traversing lists until the correct index is found
Thread Safety:
This class is not inherently thread-safe. If the underlying lists are modified concurrently during iteration or access, the behavior is undefined. Synchronization must be handled externally if needed.
Example - Processing Multiple Data Sources:
See Also:
-
Field Summary
Fields inherited from class java.util.AbstractList
modCount -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionReturns anEnumerationview of this list.booleanCompares the specified object with this list for equality.get(int index) Returns the element at the specified position in this list.inthashCode()Returns the hash code value for this list.iterator()Returns an iterator over all elements in all underlying lists.Returns a list iterator over all elements in all underlying lists.listIterator(int index) Returns a list iterator over all elements in all underlying lists, starting at the specified position.intsize()Returns the total number of elements across all underlying lists.toString()Returns a string representation of this MultiList.Methods inherited from class java.util.AbstractList
add, add, addAll, clear, indexOf, lastIndexOf, remove, removeRange, set, subListMethods inherited from class java.util.AbstractCollection
addAll, contains, containsAll, isEmpty, remove, removeAll, retainAll, toArray, toArrayMethods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface java.util.Collection
parallelStream, removeIf, stream, toArrayMethods inherited from interface java.util.List
addAll, contains, containsAll, isEmpty, remove, removeAll, replaceAll, retainAll, sort, spliterator, toArray, toArray
-
Constructor Details
-
MultiList
Creates a new MultiList that presents the specified lists as a single unified list.The lists are stored by reference (not copied), so modifications made through the MultiList's iterator will affect the original lists.
Example:
List<String>
list1 =new ArrayList<>(List.of("a" ,"b" )); List<String>list2 =new ArrayList<>(List.of("c" ,"d" )); MultiList<String>multiList =new MultiList<>(list1 ,list2 );// multiList now represents all elements from both lists - Parameters:
c- Zero or more lists to combine into this list. Must not benull , and no individual list can benull (but lists can be empty).- Throws:
IllegalArgumentException- if the lists array or any list within it isnull .
-
-
Method Details
-
enumerator
Returns anEnumerationview of this list.This is useful for compatibility with legacy APIs that require an
Enumerationrather than anIterator.Example:
MultiList<String>
multiList =new MultiList<>(list1, list2); Enumeration<String>enumeration =multiList .enumerator();while (enumeration .hasMoreElements()) { Stringelement =enumeration .nextElement();// Process element }- Returns:
- An
Enumerationthat iterates over all elements in all underlying lists. - See Also:
-
get
Returns the element at the specified position in this list.The index is resolved by traversing the underlying lists in order until the correct list and position within that list is found.
Example:
List<String>
list1 = List.of("a" ,"b" );// indices 0-1 List<String>list2 = List.of("c" ,"d" ,"e" );// indices 2-4 MultiList<String>multiList =new MultiList<>(list1 ,list2 );multiList .get(0);// Returns "a" multiList .get(2);// Returns "c" multiList .get(4);// Returns "e" - Specified by:
getin interfaceList<E>- Specified by:
getin classAbstractList<E>- Parameters:
index- The index of the element to return.- Returns:
- The element at the specified position.
- Throws:
IndexOutOfBoundsException- if the index is out of range (index < 0 || index >= size()).
-
iterator
Returns an iterator over all elements in all underlying lists.The iterator traverses each list in the order they were provided to the constructor. Within each list, the iteration order follows the list's natural order.
The returned iterator supports the
Iterator.remove()operation, which removes the current element from its underlying list.Behavior:
- Elements from the first list are iterated first, then the second, and so on
- If a list is empty, it is skipped during iteration
- Calling
Iterator.remove()removes the element from the underlying list - Calling
Iterator.next()whenIterator.hasNext()returnsfalse throwsNoSuchElementException - Calling
Iterator.remove()before callingIterator.next()or calling it twice in a row may throwIllegalStateException(behavior depends on underlying list)
Example:
List<String>
list1 =new ArrayList<>(List.of("a" ,"b" )); List<String>list2 =new ArrayList<>(List.of("c" ,"d" )); MultiList<String>multiList =new MultiList<>(list1 ,list2 ); Iterator<String>it =multiList .iterator();while (it .hasNext()) { Stringelement =it .next();if (element .equals("b" )) {it .remove();// Removes "b" from list1 } } -
listIterator
Returns a list iterator over all elements in all underlying lists.The list iterator traverses each list in the order they were provided to the constructor. The iterator starts at the beginning of the first list.
The returned list iterator supports the
ListIterator.remove()operation, which removes the current element from its underlying list.Behavior:
- Elements from the first list are iterated first, then the second, and so on
- If a list is empty, it is skipped during iteration
- Calling
ListIterator.remove()removes the element from the underlying list - Bidirectional navigation is supported, but may be less efficient than forward-only iteration
- Specified by:
listIteratorin interfaceList<E>- Overrides:
listIteratorin classAbstractList<E>- Returns:
- A list iterator over all elements in all underlying lists, starting at the beginning.
-
listIterator
Returns a list iterator over all elements in all underlying lists, starting at the specified position.The list iterator traverses each list in the order they were provided to the constructor. The iterator starts at the specified index.
- Specified by:
listIteratorin interfaceList<E>- Overrides:
listIteratorin classAbstractList<E>- Parameters:
index- The index to start the iterator at.- Returns:
- A list iterator over all elements in all underlying lists, starting at the specified index.
- Throws:
IndexOutOfBoundsException- if the index is out of range (index < 0 || index > size()).
-
size
Returns the total number of elements across all underlying lists.This method computes the size by summing the
List.size()of each underlying list. The size is recalculated each time this method is called (it is not cached).Example:
List<String>
list1 = List.of("a" ,"b" );// size = 2 List<String>list2 = List.of("c" ,"d" ,"e" );// size = 3 MultiList<String>multiList =new MultiList<>(list1 ,list2 );int totalSize =multiList .size();// Returns: 5 - Specified by:
sizein interfaceCollection<E>- Specified by:
sizein interfaceList<E>- Specified by:
sizein classAbstractCollection<E>- Returns:
- The sum of sizes of all underlying lists.
-
toString
Returns a string representation of this MultiList.The format is
"[[...],[...],...]" where each[...] is the standard standard string representation of each underlying list (as returned byObject.toString()).Example:
List<String>
list1 = List.of("a" ,"b" ); List<String>list2 = List.of("c" ,"d" ); MultiList<String>multiList =new MultiList<>(list1 ,list2 );multiList .toString();// Returns: "[[a, b], [c, d]]" - Overrides:
toStringin classAbstractCollection<E>- Returns:
- A string representation of this MultiList.
-
equals
Compares the specified object with this list for equality.Returns
true if and only if the specified object is also a list, both lists have the same size, and all corresponding pairs of elements in the two lists are equal. In other words, two lists are defined to be equal if they contain the same elements in the same order.This implementation first checks if the specified object is this list. If so, it returns
true ; if not, it checks if the specified object is a list. If not, it returnsfalse ; if so, it iterates over both lists, comparing corresponding pairs of elements.- Specified by:
equalsin interfaceCollection<E>- Specified by:
equalsin interfaceList<E>- Overrides:
equalsin classAbstractList<E>- Parameters:
o- The object to be compared for equality with this list.- Returns:
true if the specified object is equal to this list.
-
hashCode
Returns the hash code value for this list.The hash code of a list is defined to be the result of the following calculation:
int hashCode = 1;for (E e : list) hashCode = 31 * hashCode + (e ==null ? 0 : e.hashCode());This ensures that
list1.equals(list2) implies thatlist1.hashCode()==list2.hashCode() for any two listslist1 andlist2 , as required by the general contract ofObject.hashCode().- Specified by:
hashCodein interfaceCollection<E>- Specified by:
hashCodein interfaceList<E>- Overrides:
hashCodein classAbstractList<E>- Returns:
- The hash code value for this list.
-