Package com.github.tommyettinger.ds
Class ObjectList<T>
java.lang.Object
java.util.AbstractCollection<E>
java.util.AbstractList<E>
java.util.ArrayList<T>
com.github.tommyettinger.ds.ObjectList<T>
- All Implemented Interfaces:
Arrangeable,Arrangeable.ArrangeableList<T>,EnhancedCollection<T>,Ordered<T>,Serializable,Cloneable,Iterable<T>,Collection<T>,List<T>,RandomAccess
- Direct Known Subclasses:
ObjectBag
public class ObjectList<T>
extends ArrayList<T>
implements Ordered<T>, EnhancedCollection<T>, Arrangeable.ArrangeableList<T>
A resizable, insertion-ordered list of
T items, typically objects (they can also be arrays).
This is a thin wrapper around ArrayList to implement Ordered and do some of
what libGDX's Array class does. Because this is a generic class and arrays do not interact
well with generics, ObjectList does not permit access to a T[] of items like Array
does; you can use ArrayList.toArray(Object[]) or (if you can use Java 11)
toArray(IntFunction) to make a new array of T from the contents of an ArrayList.
The second of these toArray methods is newer; You can use it with code like
ObjectList<String> myList = new ObjectList<>(); String[] s = myList.toArray(String::new);.-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classNested classes/interfaces inherited from interface com.github.tommyettinger.ds.Arrangeable
Arrangeable.ArrangeableList<T>Nested classes/interfaces inherited from interface com.github.tommyettinger.ds.Ordered
Ordered.OfBoolean, Ordered.OfByte, Ordered.OfChar, Ordered.OfDouble, Ordered.OfFloat, Ordered.OfInt, Ordered.OfLong, Ordered.OfShort -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected ObjectList.ObjectListIterator<T>protected ObjectList.ObjectListIterator<T>Fields inherited from class java.util.AbstractList
modCount -
Constructor Summary
ConstructorsConstructorDescriptionConstructs an empty list with an initial capacity of 10.ObjectList(boolean ordered, int initialCapacity) Deprecated.ObjectList(boolean ordered, Collection<? extends T> c) Deprecated.This is equivalent toObjectList(Collection); make anObjectBagfor an unordered listObjectList(int initialCapacity) Constructs an empty list with the specified initial capacity.ObjectList(Ordered<T> other, int offset, int count) Creates a new list by copyingcountitems from the given Ordered, starting atoffsetin that Ordered, into this.ObjectList(Collection<? extends T> c) Constructs a list containing the elements of the specified collection, in the order they are returned by the collection's iterator.ObjectList(Iterator<? extends T> coll) Creates a new instance containing the items in the specified iterator.ObjectList(T[] a) ObjectList(T[] a, int offset, int count) -
Method Summary
Modifier and TypeMethodDescriptionvoidbooleanAdds up tocountitems, starting fromoffset, in the Orderedotherto this list, inserting starting atinsertionIndexin the iteration order.booleanAdds each item in the arrayato this ObjectList, inserting starting atinsertionIndex.booleanAdds up tocountitems, starting fromoffset, in the arrayato this ObjectList, inserting starting atinsertionIndex.booleanAdds up tocountitems, starting fromoffset, in the Orderedotherto this list, inserting at the end of the iteration order.booleanAdds each item in the arrayato this ObjectList, appending to the end.booleanAdds up tocountitems, starting fromoffset, in the arrayato this ObjectList, appending to the end.appendTo(StringBuilder builder, String separator) booleancontainsAll(Object[] array) Exactly likeAbstractCollection.containsAll(Collection), but takes an array instead of a Collection.booleancontainsAll(Object[] array, int offset, int length) booleancontainsAny(Object[] values) Returns true if this ObjectList contains any of the specified values.booleancontainsAny(Object[] values, int offset, int length) Returns true if this ObjectList contains any items from the specified range of values.booleancontainsAnyIterable(Iterable<?> values) Returns true if this ObjectList contains any of the specified values.booleanduplicateRange(int index, int count) Inserts the specified number of items at the specified index.voidensureCapacity(int additionalCapacity) Increases the size of the backing array to accommodate the specified number of additional items.booleanbooleanequalsIdentity(Object object) Uses == for comparison of each item.first()Returns the first item.inthashCode()voidThis is an alias foradd(int, Object)to improve compatibility with primitive lists.iterator()Returns an iterator over the elements in this list in proper sequence.booleanReturns true if this implementation retains order, which it does.Returns a list iterator over the elements in this list (in proper sequence).listIterator(int index) Returns a list iterator over the elements in this list (in proper sequence), starting at the specified position in the list.booleannotEmpty()Returns true if the array has one or more items.order()Gets the ObjectList of T items that this data structure holds, in the order it uses for iteration.static <T> ObjectList<T>parse(String str, String delimiter, PartialParser<T> parser) Callsparse(String, String, PartialParser, boolean)with brackets set to false.static <T> ObjectList<T>parse(String str, String delimiter, PartialParser<T> parser, boolean brackets) Creates a new collection and fills it by callingEnhancedCollection.addLegible(String, String, PartialParser, int, int)on either all ofstr(ifbracketsis false) orstrwithout its first and last chars (ifbracketsis true).static <T> ObjectList<T>parse(String str, String delimiter, PartialParser<T> parser, int offset, int length) Creates a new collection and fills it by callingEnhancedCollection.addLegible(String, String, PartialParser, int, int)with the given five parameters as-is.peek()Returns the last item.pop()Removes and returns the last item.Returns aTitem from anywhere in this ObjectList, chosen pseudo-randomly usingrandom.remove(int index) booleanExactly likeArrayList.removeAll(Collection), but takes an array instead of a Collection.booleanremoveAt(int index) This is an alias forremove(int)to make the API the same for primitive lists.booleanremoveEach(Object[] array) Exactly likeremoveEachIterable(Iterable), but takes an array instead of a Collection.booleanremoveEach(Object[] array, int offset, int length) booleanremoveEachIterable(Iterable<?> other) Removes from this ObjectList element-wise occurrences of elements contained in the specified Iterable.voidremoveRange(int start, int end) Removes the items between the specified start index, inclusive, and end index, exclusive.intreplaceAll(T find, T replace) Replaces every occurrence offindwithreplace.intreplaceAllIdentity(T find, T replace) Replaces every occurrence offindwithreplace.booleanreplaceFirst(T find, T replace) Replaces the first occurrence offindwithreplace.booleanreplaceFirstIdentity(T find, T replace) Replaces the first occurrence offindwithreplace.voidreverse()Reverses the order of this Ordered in-place.voidshrink()voidPseudo-randomly shuffles the order of this Ordered in-place.voidsort()Sorts this ObjectList based on the natural order of its elements;Tmust implementComparablefor this to succeed.voidsort(int from, int to) Sorts a subrange of this ObjectList based on the natural order of its elements;Tmust implementComparablefor this to succeed.voidsort(int from, int to, Comparator<? super T> c) Sorts a subrange of this ObjectList using the given Comparator.voidsort(Comparator<? super T> c) Sorts this ObjectList using the given Comparator.voidsortJDK()Currently callssort()because this can't accessArrays.sort(Object[], int, int)without the parent ArrayList's items array.voidsortJDK(int from, int to) Currently callssort(int, int)because this can't accessArrays.sort(Object[], int, int)without the parent ArrayList's items array.voidsortJDK(int from, int to, Comparator<? super T> c) Currently callssort(int, int, Comparator)because this can't accessArrays.sort(Object[], int, int, Comparator)without the parent ArrayList's items array.voidsortJDK(Comparator<? super T> c) Currently callssort(Comparator)because this can't accessArrays.sort(Object[], int, int, Comparator)without the parent ArrayList's items array.voidswap(int first, int second) Switches the ordering of positionsaandb, without changing any items beyond that.toString()voidvoidtruncate(int newSize) Reduces the size of the array to the specified size.static <T> ObjectList<T>with()Constructs an empty list given the type as a generic type argument.static <T> ObjectList<T>with(T item) Creates a new ObjectList that holds only the given item, but can be resized.static <T> ObjectList<T>with(T... varargs) Creates a new ObjectList that will hold the items in the given array or varargs.static <T> ObjectList<T>with(T item0, T item1) Creates a new ObjectList that holds only the given items, but can be resized.static <T> ObjectList<T>with(T item0, T item1, T item2) Creates a new ObjectList that holds only the given items, but can be resized.static <T> ObjectList<T>with(T item0, T item1, T item2, T item3) Creates a new ObjectList that holds only the given items, but can be resized.static <T> ObjectList<T>with(T item0, T item1, T item2, T item3, T item4) Creates a new ObjectList that holds only the given items, but can be resized.static <T> ObjectList<T>with(T item0, T item1, T item2, T item3, T item4, T item5) Creates a new ObjectList that holds only the given items, but can be resized.static <T> ObjectList<T>with(T item0, T item1, T item2, T item3, T item4, T item5, T item6) Creates a new ObjectList that holds only the given items, but can be resized.static <T> ObjectList<T>with(T item0, T item1, T item2, T item3, T item4, T item5, T item6, T item7) Creates a new ObjectList that holds only the given items, but can be resized.Methods inherited from class java.util.ArrayList
add, addAll, addAll, clear, clone, contains, forEach, get, indexOf, isEmpty, lastIndexOf, remove, removeAll, removeIf, replaceAll, retainAll, set, size, spliterator, subList, toArray, toArrayMethods inherited from class java.util.AbstractCollection
containsAllMethods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface com.github.tommyettinger.ds.Arrangeable
rearrange, shuffle, sizeMethods inherited from interface java.util.Collection
parallelStream, removeIf, stream, toArrayMethods inherited from interface com.github.tommyettinger.ds.EnhancedCollection
add, add, add, addAll, addAllIterable, addLegible, addLegible, addVarargs, appendTo, appendTo, containsAll, containsAllIterable, containsAny, removeAll, removeAllIterable, removeEach, toString, toString, toStringMethods inherited from interface java.util.List
add, addAll, addAll, clear, contains, containsAll, get, indexOf, isEmpty, lastIndexOf, remove, removeAll, replaceAll, retainAll, set, size, spliterator, subList, toArray, toArrayMethods inherited from interface com.github.tommyettinger.ds.Ordered
getOrderType, random, selectRanked, selectRankedIndex
-
Field Details
-
iterator1
-
iterator2
-
-
Constructor Details
-
ObjectList
public ObjectList(int initialCapacity) Constructs an empty list with the specified initial capacity.- Parameters:
initialCapacity- the initial capacity of the list- Throws:
IllegalArgumentException- if the specified initial capacity is negative
-
ObjectList
Deprecated.This is equivalent toObjectList(int); make anObjectBagfor an unordered listConstructs an empty list with the specified initial capacity.- Parameters:
ordered- ignored; use anObjectBagfor an unordered listinitialCapacity- the initial capacity of the list- Throws:
IllegalArgumentException- if the specified initial capacity is negative
-
ObjectList
public ObjectList()Constructs an empty list with an initial capacity of 10. -
ObjectList
Constructs a list containing the elements of the specified collection, in the order they are returned by the collection's iterator.- Parameters:
c- the collection whose elements are to be placed into this list- Throws:
NullPointerException- if the specified collection is null
-
ObjectList
Deprecated.This is equivalent toObjectList(Collection); make anObjectBagfor an unordered listConstructs a list containing the elements of the specified collection, in the order they are returned by the collection's iterator.- Parameters:
ordered- ignored; use anObjectBagfor an unordered listc- the collection whose elements are to be placed into this list- Throws:
NullPointerException- if the specified collection is null
-
ObjectList
Creates a new instance containing the items in the specified iterator.- Parameters:
coll- an iterator that will have its remaining contents added to this
-
ObjectList
-
ObjectList
-
ObjectList
Creates a new list by copyingcountitems from the given Ordered, starting atoffsetin that Ordered, into this.- Parameters:
other- another Ordered of the same typeoffset- the first index in other's ordering to draw an item fromcount- how many items to copy from other
-
-
Method Details
-
keepsOrder
public boolean keepsOrder()Returns true if this implementation retains order, which it does.- Returns:
- true
-
add
-
insert
This is an alias foradd(int, Object)to improve compatibility with primitive lists.- Parameters:
index- index at which the specified element is to be insertedelement- element to be inserted
-
remove
-
removeAt
This is an alias forremove(int)to make the API the same for primitive lists.- Parameters:
index- must be non-negative and less thanArrayList.size()- Returns:
- the previously-held item at the given index
-
removeRange
public void removeRange(int start, int end) Removes the items between the specified start index, inclusive, and end index, exclusive. Note that this takes different arguments than some other range-related methods; this needs a start index and an end index, rather than a count of items. This matches the behavior in the JDK collections. This is also different from removeRange() in the libGDX Array class because it is exclusive on end, instead of how Array is inclusive on end.- Specified by:
removeRangein interfaceOrdered<T>- Overrides:
removeRangein classArrayList<T>- Parameters:
start- the first index to remove, inclusiveend- the last index (after what should be removed), exclusive
-
removeAll
Exactly likeArrayList.removeAll(Collection), but takes an array instead of a Collection.- Specified by:
removeAllin interfaceEnhancedCollection<T>- Parameters:
other- array containing elements to be removed from this list- Returns:
trueif this list changed as a result of the call- See Also:
-
removeAll
- Specified by:
removeAllin interfaceEnhancedCollection<T>- Parameters:
array- the elements to be removed from this listoffset- the index of the first item in array to removelength- how many items, at most, to get from array and remove from this- Returns:
trueif this list changed as a result of the call- See Also:
-
removeEachIterable
Removes from this ObjectList element-wise occurrences of elements contained in the specified Iterable. Note that if a value is present more than once in this ObjectList, only one of those occurrences will be removed for each occurrence of that value inother. Ifotherhas the same contents as this ObjectList or has additional items, then removing each ofotherwill clear this.
This matches the behavior of the libGDXArray.removeAll(Array)method in libGDX 1.10.0 and earlier. The methodArrayList.removeAll(Collection)here matches the behavior of the JDKList.removeAll(Collection)method.- Specified by:
removeEachIterablein interfaceEnhancedCollection<T>- Parameters:
other- an Iterable of T items to remove one-by-one, such as another ObjectList or an ObjectSet- Returns:
- true if this list was modified.
-
removeEach
Exactly likeremoveEachIterable(Iterable), but takes an array instead of a Collection.- Specified by:
removeEachin interfaceEnhancedCollection<T>- Parameters:
array- array containing elements to be removed from this list- Returns:
trueif this list changed as a result of the call- See Also:
-
removeEach
- Specified by:
removeEachin interfaceEnhancedCollection<T>- Parameters:
array- the elements to be removed from this listoffset- the index of the first item in array to removelength- how many items, at most, to get from array and remove from this- Returns:
trueif this list changed as a result of the call- See Also:
-
addAll
Adds each item in the arrayato this ObjectList, appending to the end.- Specified by:
addAllin interfaceEnhancedCollection<T>- Parameters:
a- a non-null array ofT- Returns:
- true if this is modified by this call, as
ArrayList.addAll(Collection)does
-
addAll
Adds each item in the arrayato this ObjectList, inserting starting atinsertionIndex.- Parameters:
insertionIndex- where to insert into this ObjectLista- a non-null array ofT- Returns:
- true if this is modified by this call, as
ArrayList.addAll(Collection)does
-
addAll
Adds up tocountitems, starting fromoffset, in the arrayato this ObjectList, appending to the end.- Specified by:
addAllin interfaceEnhancedCollection<T>- Parameters:
a- a non-null array ofToffset- the first index inato usecount- how many indices inato use- Returns:
- true if this is modified by this call, as
ArrayList.addAll(Collection)does
-
duplicateRange
public boolean duplicateRange(int index, int count) Inserts the specified number of items at the specified index. The new items will have values equal to the values at those indices before the insertion, and the previous values will be pushed to after the duplicated range.- Parameters:
index- the first index to duplicatecount- how many items to duplicate
-
containsAll
Exactly likeAbstractCollection.containsAll(Collection), but takes an array instead of a Collection.- Specified by:
containsAllin interfaceEnhancedCollection<T>- Parameters:
array- array to be checked for containment in this collection- Returns:
trueif this list contains all the elements in the specified array- See Also:
-
containsAll
- Specified by:
containsAllin interfaceEnhancedCollection<T>- Parameters:
array- array to be checked for containment in this collectionoffset- the index of the first item in array to checklength- how many items, at most, to check from array- Returns:
trueif this collection contains all the elements in the specified range of array- See Also:
-
containsAnyIterable
Returns true if this ObjectList contains any of the specified values.- Specified by:
containsAnyIterablein interfaceEnhancedCollection<T>- Parameters:
values- may contain nulls, but must not be null itself- Returns:
- true if this ObjectList contains any of the items in
values, false otherwise
-
containsAny
Returns true if this ObjectList contains any of the specified values.- Specified by:
containsAnyin interfaceEnhancedCollection<T>- Parameters:
values- may contain nulls, but must not be null itself- Returns:
- true if this ObjectList contains any of the items in
values, false otherwise
-
containsAny
Returns true if this ObjectList contains any items from the specified range of values.- Specified by:
containsAnyin interfaceEnhancedCollection<T>- Parameters:
values- may contain nulls, but must not be null itselfoffset- the index to start checking in valueslength- how many items to check from values- Returns:
- true if this ObjectList contains any of the items in the given range of
values, false otherwise
-
addAll
Adds up tocountitems, starting fromoffset, in the arrayato this ObjectList, inserting starting atinsertionIndex.- Parameters:
insertionIndex- where to insert into this ObjectLista- a non-null array ofToffset- the first index inato usecount- how many indices inato use- Returns:
- true if this is modified by this call, as
ArrayList.addAll(Collection)does
-
addAll
Adds up tocountitems, starting fromoffset, in the Orderedotherto this list, inserting at the end of the iteration order.- Parameters:
other- a non-nullOrderedofToffset- the first index inotherto usecount- how many indices inotherto use- Returns:
- true if this is modified by this call, as
ArrayList.addAll(Collection)does
-
addAll
Adds up tocountitems, starting fromoffset, in the Orderedotherto this list, inserting starting atinsertionIndexin the iteration order.- Parameters:
insertionIndex- where to insert into the iteration orderother- a non-nullOrderedofToffset- the first index inotherto usecount- how many indices inotherto use- Returns:
- true if this is modified by this call, as
ArrayList.addAll(Collection)does
-
pop
Removes and returns the last item. -
peek
Returns the last item. -
first
Returns the first item.- Specified by:
firstin interfaceEnhancedCollection<T>- Returns:
- the first item in this EnhancedCollection, as produced by
Collection.iterator()
-
notEmpty
public boolean notEmpty()Returns true if the array has one or more items. -
ensureCapacity
public void ensureCapacity(int additionalCapacity) Increases the size of the backing array to accommodate the specified number of additional items. Useful before adding many items to avoid multiple backing array resizes. Note that this has different behavior fromArrayList.ensureCapacity(int); ArrayList's version specifies a minimum capacity (which can result in no change if the capacity is currently larger than that minimum), whereas this version specifies additional capacity (which always increases capacity ifadditionalCapacityis non-negative). The behavior here matches the primitive-backed lists likeIntList, as well as libGDX Array classes.- Overrides:
ensureCapacityin classArrayList<T>- Parameters:
additionalCapacity- how much room to add to the capacity; this is measured in the number of items this can store
-
shrink
public void shrink() -
trimToSize
public void trimToSize()- Overrides:
trimToSizein classArrayList<T>
-
equalsIdentity
Uses == for comparison of each item. -
equals
-
hashCode
public int hashCode() -
toString
- Overrides:
toStringin classAbstractCollection<T>
-
appendTo
-
replaceFirst
Replaces the first occurrence offindwithreplace. Returns true if it performed the replacement, or false if there was nothing to replace. This also returns false if find and replace are the same. This compares T items byObject.equals(Object), unless an item is null.- Parameters:
find- the item to search forreplace- the item to replacefindwith, if possible- Returns:
- true if this changed, or false otherwise
-
replaceAll
Replaces every occurrence offindwithreplace. Returns the number of changed items, which is 0 if nothing was found or in the case that find and replace are the same. This compares T items byObject.equals(Object), unless an item is null.- Parameters:
find- the item to search forreplace- the item to replacefindwith, if possible- Returns:
- the number of replacements that occurred; 0 if nothing was found or replaced
-
replaceFirstIdentity
Replaces the first occurrence offindwithreplace. Returns true if it performed the replacement, or false if there was nothing to replace. This also returns false if find and replace are the same. This compares T items by identity, not withObject.equals(Object)! This may be useful to replace occurrences ofnullby a givenreplacevalue.- Parameters:
find- the item to search forreplace- the item to replacefindwith, if possible- Returns:
- true if this changed, or false otherwise
-
replaceAllIdentity
Replaces every occurrence offindwithreplace. Returns the number of changed items, which is 0 if nothing was found or in the case that find and replace are the same. This compares T items by identity, not withObject.equals(Object)! This may be useful to replace an occurrence ofnullby a givenreplacevalue.- Parameters:
find- the item to search forreplace- the item to replacefindwith, if possible- Returns:
- the number of replacements that occurred; 0 if nothing was found or replaced
-
listIterator
Returns a list iterator over the elements in this list (in proper sequence), starting at the specified position in the list. The specified index indicates the first element that would be returned by an initial call tonext. An initial call topreviouswould return the element with the specified index minus one.
The returned iterator is reused by this ObjectList, so it is likely unsuitable for nested iteration. UseObjectListIterator(ObjectList, int)to create a ListIterator if you need nested iteration.- Specified by:
listIteratorin interfaceList<T>- Overrides:
listIteratorin classArrayList<T>- Parameters:
index- the index to start the iterator at
-
listIterator
Returns a list iterator over the elements in this list (in proper sequence).
The returned iterator is reused by this ObjectList, so it is likely unsuitable for nested iteration. UseObjectListIterator(ObjectList)to create a ListIterator if you need nested iteration.- Specified by:
listIteratorin interfaceList<T>- Overrides:
listIteratorin classArrayList<T>- See Also:
-
iterator
Returns an iterator over the elements in this list in proper sequence.
The returned iterator is reused by this ObjectList, so it is likely unsuitable for nested iteration. UseObjectListIterator(ObjectList)to create an Iterator if you need nested iteration. -
order
Gets the ObjectList of T items that this data structure holds, in the order it uses for iteration. This method actually returns this ObjectList directly, since it extends ArrayList. -
swap
public void swap(int first, int second) Switches the ordering of positionsaandb, without changing any items beyond that.- Specified by:
swapin interfaceArrangeable- Specified by:
swapin interfaceOrdered<T>- Parameters:
first- the first positionsecond- the second position
-
shuffle
Pseudo-randomly shuffles the order of this Ordered in-place. You can use anyRandomclass forrng; -
random
Returns aTitem from anywhere in this ObjectList, chosen pseudo-randomly usingrandom. If this ObjectList is empty, returnsnull. -
truncate
public void truncate(int newSize) Reduces the size of the array to the specified size. If the array is already smaller than the specified size, no action is taken. -
reverse
public void reverse()Description copied from interface:OrderedReverses the order of this Ordered in-place.- Specified by:
reversein interfaceArrangeable- Specified by:
reversein interfaceOrdered<T>
-
sort
public void sort()Sorts this ObjectList based on the natural order of its elements;Tmust implementComparablefor this to succeed.
This usesObjectComparators.sort(List, Comparator), which is slower than the approach used by the JDK inArrays.sort(Object[], int, int), but ArrayList doesn't expose its internal array to its subclasses, so we need to use a different technique. You can potentially useObjectDeque, which also implementsList, and itsObjectDeque.sortJDK()method if TimSort is significantly faster for your use-case, such as if you have truly massive lists. -
sort
public void sort(int from, int to) Sorts a subrange of this ObjectList based on the natural order of its elements;Tmust implementComparablefor this to succeed.
This usesObjectComparators.sort(List, int, int, Comparator), which is slower than the approach used by the JDK inArrays.sort(Object[], int, int), but ArrayList doesn't expose its internal array to its subclasses, so we need to use a different technique. You can potentially useObjectDeque, which also implementsList, and itsObjectDeque.sortJDK(int, int)method if TimSort is significantly faster for your use-case, such as if you have truly massive lists.- Parameters:
from- the index of the first element (inclusive) to be sortedto- the index of the last element (exclusive) to be sorted
-
sort
Sorts this ObjectList using the given Comparator. If the Comparator is null, then this sorts based on the natural order of its elements, andTmust implementComparable.
This usesObjectComparators.sort(List, Comparator), which is slower than the approach used by the JDK inArrays.sort(Object[], int, int, Comparator), but ArrayList doesn't expose its internal array to its subclasses, so we need to use a different technique. You can potentially useObjectDeque, which also implementsList, and itsObjectDeque.sortJDK(Comparator)method if TimSort is significantly faster for your use-case, such as if you have truly massive lists.
This is implemented explicitly and not annotated with Override because of Android limitations. RoboVM also has limits here; when using RoboVM,Listdoes not have anysort()methods, so Override wouldn't make sense there either (except that Override annotations are ignored on RoboVM). These methods should be safe to call directly, even if they aren't as fast as the TimSort-based version in Java 8 onward in ArrayList's sort() methods, butCollections.sort(List, Comparator)should be used cautiously when the List parameter is an ObjectList. You should make sure to test that sorting, especially sorting Lists, works on every platform you intend to target. -
sort
Sorts a subrange of this ObjectList using the given Comparator. If the Comparator is null, then this sorts based on the natural order of its elements, andTmust implementComparable.
This usesObjectComparators.sort(List, int, int, Comparator), which is slower than the approach used by the JDK inArrays.sort(Object[], int, int, Comparator), but ArrayList doesn't expose its internal array to its subclasses, so we need to use a different technique. You can potentially useObjectDeque, which also implementsList, and itsObjectDeque.sortJDK(int, int, Comparator)method if TimSort is significantly faster for your use-case, such as if you have truly massive lists.
This is implemented explicitly and not annotated with Override because of Android limitations. RoboVM also has limits here; when using RoboVM,Listdoes not have anysort()methods, so Override wouldn't make sense there either (except that Override annotations are ignored on RoboVM). These methods should be safe to call directly, even if they aren't as fast as the TimSort-based version in Java 8 onward in ArrayList's sort() methods, butCollections.sort(List, Comparator)should be used cautiously when the List parameter is an ObjectList. You should make sure to test that sorting, especially sorting Lists, works on every platform you intend to target.- Parameters:
from- the index of the first element (inclusive) to be sortedto- the index of the last element (exclusive) to be sortedc- a Comparator that can compare T items, or null to use the natural order of Comparable T items
-
sortJDK
public void sortJDK()Currently callssort()because this can't accessArrays.sort(Object[], int, int)without the parent ArrayList's items array. You can useObjectDeque, which has a workingObjectDeque.sortJDK(), or you can just usesort(), which is likely slower but doesn't allocate. -
sortJDK
public void sortJDK(int from, int to) Currently callssort(int, int)because this can't accessArrays.sort(Object[], int, int)without the parent ArrayList's items array. You can useObjectDeque, which has a workingObjectDeque.sortJDK(int, int), or you can just usesort(int, int), which is likely slower but doesn't allocate.- Parameters:
from- the index of the first element (inclusive) to be sortedto- the index of the last element (exclusive) to be sorted- See Also:
-
sortJDK
Currently callssort(Comparator)because this can't accessArrays.sort(Object[], int, int, Comparator)without the parent ArrayList's items array. You can useObjectDeque, which has a workingObjectDeque.sortJDK(Comparator), or you can just usesort(Comparator), which is likely slower but doesn't allocate.- Parameters:
c- a Comparator that can compare T items, or null to use the natural order of Comparable T items- See Also:
-
sortJDK
Currently callssort(int, int, Comparator)because this can't accessArrays.sort(Object[], int, int, Comparator)without the parent ArrayList's items array. You can useObjectDeque, which has a workingObjectDeque.sortJDK(int, int, Comparator), or you can just usesort(int, int, Comparator), which is likely slower but doesn't allocate.- Parameters:
from- the index of the first element (inclusive) to be sortedto- the index of the last element (exclusive) to be sortedc- a Comparator that can compare T items, or null to use the natural order of Comparable T items- See Also:
-
with
Constructs an empty list given the type as a generic type argument. This is usually less useful than just using the constructor, but can be handy in some code-generation scenarios when you don't know how many arguments you will have.- Type Parameters:
T- the type of items; must be given explicitly- Returns:
- a new list containing nothing
-
with
Creates a new ObjectList that holds only the given item, but can be resized.- Type Parameters:
T- the type of item, typically inferred- Parameters:
item- one T item- Returns:
- a new ObjectList that holds the given item
-
with
Creates a new ObjectList that holds only the given items, but can be resized.- Type Parameters:
T- the type of item, typically inferred- Parameters:
item0- a T itemitem1- a T item- Returns:
- a new ObjectList that holds the given items
-
with
Creates a new ObjectList that holds only the given items, but can be resized.- Type Parameters:
T- the type of item, typically inferred- Parameters:
item0- a T itemitem1- a T itemitem2- a T item- Returns:
- a new ObjectList that holds the given items
-
with
Creates a new ObjectList that holds only the given items, but can be resized.- Type Parameters:
T- the type of item, typically inferred- Parameters:
item0- a T itemitem1- a T itemitem2- a T itemitem3- a T item- Returns:
- a new ObjectList that holds the given items
-
with
Creates a new ObjectList that holds only the given items, but can be resized.- Type Parameters:
T- the type of item, typically inferred- Parameters:
item0- a T itemitem1- a T itemitem2- a T itemitem3- a T itemitem4- a T item- Returns:
- a new ObjectList that holds the given items
-
with
Creates a new ObjectList that holds only the given items, but can be resized.- Type Parameters:
T- the type of item, typically inferred- Parameters:
item0- a T itemitem1- a T itemitem2- a T itemitem3- a T itemitem4- a T itemitem5- a T item- Returns:
- a new ObjectList that holds the given items
-
with
Creates a new ObjectList that holds only the given items, but can be resized.- Type Parameters:
T- the type of item, typically inferred- Parameters:
item0- a T itemitem1- a T itemitem2- a T itemitem3- a T itemitem4- a T itemitem5- a T itemitem6- a T item- Returns:
- a new ObjectList that holds the given items
-
with
public static <T> ObjectList<T> with(T item0, T item1, T item2, T item3, T item4, T item5, T item6, T item7) Creates a new ObjectList that holds only the given items, but can be resized.- Type Parameters:
T- the type of item, typically inferred- Parameters:
item0- a T itemitem1- a T itemitem2- a T itemitem3- a T itemitem4- a T itemitem5- a T itemitem6- a T item- Returns:
- a new ObjectList that holds the given items
-
with
Creates a new ObjectList that will hold the items in the given array or varargs. This overload will only be used when an array is supplied and the type of the items requested is the component type of the array, or if varargs are used and there are 9 or more arguments.- Type Parameters:
T- the type of items, typically inferred by all the items being the same type- Parameters:
varargs- either 0 or more T items, or an array of T- Returns:
- a new ObjectList that holds the given T items
-
parse
Callsparse(String, String, PartialParser, boolean)with brackets set to false.- Parameters:
str- a String that will be parsed in fulldelimiter- the delimiter between items in strparser- a PartialParser that returns aTitem from a section ofstr- Returns:
- a new collection parsed from str
-
parse
public static <T> ObjectList<T> parse(String str, String delimiter, PartialParser<T> parser, boolean brackets) Creates a new collection and fills it by callingEnhancedCollection.addLegible(String, String, PartialParser, int, int)on either all ofstr(ifbracketsis false) orstrwithout its first and last chars (ifbracketsis true). Each item is expected to be separated bydelimiter.- Parameters:
str- a String that will be parsed in full (depending on brackets)delimiter- the delimiter between items in strparser- a PartialParser that returns aTitem from a section ofstrbrackets- if true, the first and last chars in str will be ignored- Returns:
- a new collection parsed from str
-
parse
public static <T> ObjectList<T> parse(String str, String delimiter, PartialParser<T> parser, int offset, int length) Creates a new collection and fills it by callingEnhancedCollection.addLegible(String, String, PartialParser, int, int)with the given five parameters as-is.- Parameters:
str- a String that will have the given section parseddelimiter- the delimiter between items in strparser- a PartialParser that returns aTitem from a section ofstroffset- the first position to parse in str, inclusivelength- how many chars to parse, starting from offset- Returns:
- a new collection parsed from str
-
ObjectList(int); make anObjectBagfor an unordered list