Package com.github.tommyettinger.ds
Class ObjectBag<T>
java.lang.Object
java.util.AbstractCollection<E>
java.util.AbstractList<E>
java.util.ArrayList<T>
com.github.tommyettinger.ds.ObjectList<T>
com.github.tommyettinger.ds.ObjectBag<T>
- All Implemented Interfaces:
Arrangeable,Arrangeable.ArrangeableList<T>,EnhancedCollection<T>,Ordered<T>,Serializable,Cloneable,Iterable<T>,Collection<T>,List<T>,RandomAccess
An unordered List of T items. This allows efficient iteration via a reused iterator or via index.
This class avoids a memory copy when removing elements (the last element is moved to the removed element's position).
Items are permitted to change position in the ordering when any item is removed or added.
Although this won't keep an order during modifications, you can
ObjectList.sort() the bag to ensure,
if no modifications are made later, that the iteration will happen in sorted order.- See Also:
-
Nested Class Summary
Nested classes/interfaces inherited from class com.github.tommyettinger.ds.ObjectList
ObjectList.ObjectListIterator<T>Nested 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
Fields inherited from class com.github.tommyettinger.ds.ObjectList
iterator1, iterator2Fields inherited from class java.util.AbstractList
modCount -
Constructor Summary
ConstructorsConstructorDescriptionConstructs an empty bag with an initial capacity of 10.ObjectBag(int initialCapacity) Constructs an empty bag with the specified initial capacity.Creates a new bag by copyingcountitems from the given Ordered, starting atoffsetin that Ordered, into this.ObjectBag(Collection<? extends T> c) Constructs a bag containing the elements of the specified collection, in the order they are returned by the collection's iterator.Creates a new instance containing the items in the specified iterator. -
Method Summary
Modifier and TypeMethodDescriptionvoidThis always addselementto the end of this bag's ordering;indexis ignored.booleanequalsIdentity(Object object) Uses == for comparison of the bags; does not compare their items.inthashCode()voidThis always addselementto the end of this bag's ordering;indexis ignored.booleanReturns true if this implementation retains order, which it does not.static <T> ObjectBag<T>parse(String str, String delimiter, PartialParser<T> parser) Callsparse(String, String, PartialParser, boolean)with brackets set to false.static <T> ObjectBag<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> ObjectBag<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.remove(int index) This removes the item at the given index and returns it, but also changes the ordering.removeAt(int index) This removes the item at the given index and returns it, but also changes the ordering.static <T> ObjectBag<T>with()Constructs an empty bag given the type as a generic type argument.static <T> ObjectBag<T>with(T item) Creates a new ObjectBag that holds only the given item, but can be resized.static <T> ObjectBag<T>with(T... varargs) Creates a new ObjectBag that will hold the items in the given array or varargs.static <T> ObjectBag<T>with(T item0, T item1) Creates a new ObjectBag that holds only the given items, but can be resized.static <T> ObjectBag<T>with(T item0, T item1, T item2) Creates a new ObjectBag that holds only the given items, but can be resized.static <T> ObjectBag<T>with(T item0, T item1, T item2, T item3) Creates a new ObjectBag that holds only the given items, but can be resized.static <T> ObjectBag<T>with(T item0, T item1, T item2, T item3, T item4) Creates a new ObjectBag that holds only the given items, but can be resized.static <T> ObjectBag<T>with(T item0, T item1, T item2, T item3, T item4, T item5) Creates a new ObjectBag that holds only the given items, but can be resized.static <T> ObjectBag<T>with(T item0, T item1, T item2, T item3, T item4, T item5, T item6) Creates a new ObjectBag that holds only the given items, but can be resized.static <T> ObjectBag<T>with(T item0, T item1, T item2, T item3, T item4, T item5, T item6, T item7) Creates a new ObjectBag that holds only the given items, but can be resized.Methods inherited from class com.github.tommyettinger.ds.ObjectList
addAll, addAll, addAll, addAll, addAll, addAll, appendTo, containsAll, containsAll, containsAny, containsAny, containsAnyIterable, duplicateRange, ensureCapacity, equals, first, iterator, listIterator, listIterator, notEmpty, order, peek, pop, random, removeAll, removeAll, removeEach, removeEach, removeEachIterable, removeRange, replaceAll, replaceAllIdentity, replaceFirst, replaceFirstIdentity, reverse, shrink, shuffle, sort, sort, sort, sort, sortJDK, sortJDK, sortJDK, sortJDK, swap, toString, trimToSize, truncateMethods 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
-
Constructor Details
-
ObjectBag
public ObjectBag()Constructs an empty bag with an initial capacity of 10. -
ObjectBag
public ObjectBag(int initialCapacity) Constructs an empty bag with the specified initial capacity.- Parameters:
initialCapacity- the initial capacity of the bag- Throws:
IllegalArgumentException- if the specified initial capacity is negative
-
ObjectBag
Constructs a bag 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 bag- Throws:
NullPointerException- if the specified collection is null
-
ObjectBag
-
ObjectBag
-
ObjectBag
Creates a new instance containing the items in the specified iterator.- Parameters:
coll- an iterator that will have its remaining contents added to this
-
ObjectBag
Creates a new bag 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 not.- Overrides:
keepsOrderin classObjectList<T>- Returns:
- false
-
add
This always addselementto the end of this bag's ordering;indexis ignored. -
insert
This always addselementto the end of this bag's ordering;indexis ignored. This is an alias foradd(int, Object)to improve compatibility with primitive lists.- Overrides:
insertin classObjectList<T>- Parameters:
index- ignoredelement- element to be inserted
-
remove
This removes the item at the given index and returns it, but also changes the ordering.- Specified by:
removein interfaceList<T>- Overrides:
removein classObjectList<T>- Parameters:
index- the index of the element to be removed, which must be non-negative and less thanArrayList.size()- Returns:
- the removed item
- Throws:
IndexOutOfBoundsException- if the bag is empty
-
removeAt
This removes the item at the given index and returns it, but also changes the ordering. This is an alias forremove(int)to make the API the same for primitive lists.- Overrides:
removeAtin classObjectList<T>- Parameters:
index- must be non-negative and less thanArrayList.size()- Returns:
- the previously-held item at the given index
- Throws:
IndexOutOfBoundsException- if the bag is empty
-
equalsIdentity
Uses == for comparison of the bags; does not compare their items.- Overrides:
equalsIdentityin classObjectList<T>
-
hashCode
public int hashCode()- Specified by:
hashCodein interfaceCollection<T>- Specified by:
hashCodein interfaceList<T>- Overrides:
hashCodein classObjectList<T>
-
with
Constructs an empty bag 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 bag containing nothing
-
with
Creates a new ObjectBag 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 ObjectBag that holds the given item
-
with
Creates a new ObjectBag 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 ObjectBag that holds the given items
-
with
Creates a new ObjectBag 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 ObjectBag that holds the given items
-
with
Creates a new ObjectBag 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 ObjectBag that holds the given items
-
with
Creates a new ObjectBag 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 ObjectBag that holds the given items
-
with
Creates a new ObjectBag 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 ObjectBag that holds the given items
-
with
Creates a new ObjectBag 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 ObjectBag that holds the given items
-
with
public static <T> ObjectBag<T> with(T item0, T item1, T item2, T item3, T item4, T item5, T item6, T item7) Creates a new ObjectBag 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 ObjectBag that holds the given items
-
with
Creates a new ObjectBag 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 ObjectBag 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> ObjectBag<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> ObjectBag<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
-