Colecciones

Colecciones de Java
Nivel 7 , Lección 9
Disponible

"Aquí estoy."

"Te he estado esperando aquí durante mucho tiempo".

"Eso espero. Sigamos, entonces."

"Voy a presentarles para presentarles una clase de utilidad super-duper con todas las funciones para colecciones".

"Ya estoy temblando de emoción".

"Genial. Entonces comencemos. Estos son los métodos de la clase Collections:"

Código Explicación
boolean addAll(Collection<? super T> c, T... elements)
Agrega los elementos pasados ​​a la colección pasada.
Este método se puede llamar convenientemente así: Collections.addList (list, 10,11,12,13,14,15)
Queue<T> asLifoQueue(Deque<T> deque)
Devuelve una «cola normal» hecha a partir de un deque.
int binarySearch(List<? extends T> list, T key, Comparator<? super T> c)
Performs a binary search for the key element in the list.
The list must be sorted.
You can specify a comparator to compare the elements.
Collection<E> checkedCollection(Collection<E> c, Class<E> type)
Checks that all elements of collection c are of a certain type.
Similar methods exist for List, Map, Set, and SortedSet.
void copy(List<? super T> dest, List<? extends T> src)
Copies the src list to the dest list.
boolean disjoint(Collection<?> c1, Collection<?> c2)
Checks that the collections do not contain common elements
void fill(List<? super T> list, T obj)
Fills list with the element obj
int frequency(Collection<?> c, Object o)
Counts how many times object o exists in collection c
int indexOfSubList(List<?> source, List<?> target)
Determines the index of the first occurrence of the target list within the source list.
The principle is similar to String.indexOf("some string")
int lastIndexOfSubList(List<?> source, List<?> target)
Determines the index of the last occurrence of the target list within the source list.
Similar to String.lastIndexOf("some string")
max(Collection<? extends T> coll)
Searches for the maximum number/value in a collection.
How do we find the maximum of 6 numbers?
Collections.max(Arrays.asList(51, 42, 33, 24, 15, 6));
min(Collection<? extends T> coll)
Searches for the minimum value in a collection.
List<T>nCopies(int n, To)
Creates a dummy collection in which the o element appears n times.
boolean replaceAll(List<T> list, T oldVal, T newVal)
Replaces all oldVal elements in the list with newVal elements
void reverse(List<?> list)
Reverses the list.
void shuffle(List<?> list)
Randomly shuffles the elements of the list.
List<T>singletonList(To)
Returns an immutable list consisting of one passed element.
Similar methods exist for Map, Set, and SortedSet.
void sort(List<T> list)
Sorts the list in ascending order.
void swap(List<?> list, int i, int j)
Swaps the elements of the list
Collection<T>synchronizedCollection(Collection<T> c)
Wraps this collection in a synchronized counterpart.
Similar methods exist for List, Map, Set, and SortedSet.

"Whoa! Yeah, this is a whole arsenal, and I've hardly used any of it."

"Actually, this is where my lesson ends today."

"Take a look at these methods, and find the ones you are most likely to use."

"Or better yet, study them. They will be useful to you."

"Thank you, Rishi. I'll go study."

Comentarios
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION