Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Java ArrayList Video Tutorial

In this presentation you will learn everything you need to know about ArrayList. I explain the difference of capacity and size, and I introduce you to the methods of the Java ArrayList class, divided by the interfaces java.util.Collection and java.util.List.

  • Login to see the comments

Java ArrayList Video Tutorial

  1. 1.  2016, Marcus Biel, http://www.marcus-biel.com/ Marcus Biel, Software Craftsman http://www.marcus-biel.com
  2. 2.  2016, Marcus Biel, http://www.marcus-biel.com/ Collection Interface Hierarchy<<interface>> Collection <<interface>> Set <<interface>> List <<interface>> Queue HashSet <<interface>> SortedSet <<interface>> NavigableSet TreeSet ArrayList LinkedList PriorityQueue LinkedHashSet implements extends ArrayList is the default implementation of the List interface.
  3. 3.  2016, Marcus Biel, http://www.marcus-biel.com/ Collection Interface Hierarchy<<interface>> Collection <<interface>> Set <<interface>> List <<interface>> Queue HashSet <<interface>> SortedSet <<interface>> NavigableSet TreeSet ArrayList LinkedList PriorityQueue LinkedHashSet implements extends As with any implementation of List, you can have duplicate elements in your ArrayList, and you can go from element to element in the same order as the elements were inserted.
  4. 4.  2016, Marcus Biel, http://www.marcus-biel.com/ Collection Interface Hierarchy<<interface>> Collection <<interface>> Set <<interface>> List <<interface>> Queue HashSet <<interface>> SortedSet <<interface>> NavigableSet TreeSet ArrayList LinkedList PriorityQueue LinkedHashSet implements extends As it is based on arrays, ArrayList provides fast access, but inserting or removing an element at a random position requires more time, as this will require to reorganize the list.
  5. 5.  2016, Marcus Biel, http://www.marcus-biel.com/ Collection Interface Hierarchy<<interface>> Collection <<interface>> Set <<interface>> List <<interface>> Queue HashSet <<interface>> SortedSet <<interface>> NavigableSet TreeSet ArrayList LinkedList PriorityQueue LinkedHashSet implements extends Fast access however is crucial for most applications, which is why ArrayList is the most commonly used Collection. To store data that changes frequently, however, consider using an alternative container, for example LinkedList.
  6. 6.  2016, Marcus Biel, http://www.marcus-biel.com/ Size Capacity Terminology Before continuing, let me introduce you to two different terms which are important to understand in context with ArrayList: Size and capacity.
  7. 7.  2016, Marcus Biel, http://www.marcus-biel.com/ Size Capacity Terminology Size is the number of elements the ArrayList currently holds. For every element you add to the list, the size will grow by one.
  8. 8.  2016, Marcus Biel, http://www.marcus-biel.com/ Size Capacity Terminology Capacity however is the number of elements the currently underlying Array can hold.
  9. 9.  2016, Marcus Biel, http://www.marcus-biel.com/ Terminology Size Capacity The capacity of the ArrayList grows in intervals. The ArrayList starts with an initial capacity. Every time you exceed the capacity of the Array, the ArrayList copies the data over to a new Array that is about fifty percent larger than the previous one.
  10. 10.  2016, Marcus Biel, http://www.marcus-biel.com/ ArrayList Capacity …10 Let’s say you want to add one hundred elements to an ArrayList of an initial capacity of ten. As the list grows, the system will create six more arrays to take the place of the first.
  11. 11.  2016, Marcus Biel, http://www.marcus-biel.com/ ArrayList Capacity …15 …10 First one array that can hold fifteen elements …
  12. 12.  2016, Marcus Biel, http://www.marcus-biel.com/ ArrayList Capacity …22 …15 …10 …then one for a maximum of twenty two elements…
  13. 13.  2016, Marcus Biel, http://www.marcus-biel.com/ ArrayList Capacity …22 …15 …10 …33 …then arrays with a capacity of thirty three…
  14. 14.  2016, Marcus Biel, http://www.marcus-biel.com/ ArrayList Capacity …49 …22 …15 …10 …33 …forty nine…
  15. 15.  2016, Marcus Biel, http://www.marcus-biel.com/ ArrayList Capacity …73 …49 …22 …15 …10 …33 …seventy three…
  16. 16.  2016, Marcus Biel, http://www.marcus-biel.com/ ArrayList Capacity …109 …73 …49 …22 …15 …10 …33 and finally one hundred and nine elements to hold the growing list. These restructuring arrangements can negatively impact performance.
  17. 17.  2016, Marcus Biel, http://www.marcus-biel.com/ ArrayList Capacity List<String> myList = new ArrayList<>(INITIAL_CAPACITY); You can instantly create an Array of the correct size to minimize these merging activities by defining the correct capacity at creation time.
  18. 18.  2016, Marcus Biel, http://www.marcus-biel.com/ ArrayList Capacity List<String> myList = new ArrayList<>(INITIAL_CAPACITY); In case you don’t know the final size of the ArrayList at creation time, estimate it as close as possible. Choosing a too large capacity however can also negatively impact performance, so choose this value carefully.
  19. 19.  2016, Marcus Biel, http://www.marcus-biel.com/ ArrayList Capacity List<String> myList = new ArrayList<>(INITIAL_CAPACITY); I advise you to always explicitly set the capacity at creation time, as it documents your intentions.
  20. 20.  2016, Marcus Biel, http://www.marcus-biel.com/ ArrayList Capacity Low performance requirements are no excuse for sloppy design and poor implementation. For most projects you won’t have to worry about optimizing performance due to powerful hardware, but this is no excuse for sloppy design and poor implementation!
  21. 21.  2016, Marcus Biel, http://www.marcus-biel.com/ ArrayListpackage java.util; public class ArrayList<E> { private static final int DEFAULT_CAPACITY = 10; private Object[] elementData; private int size; public E get(int index) {…} public boolean add(E e) {…} public E remove(int index) {…} […] } Here you can see a simplified extract of the class ArrayList. Keep in mind the real class looks a bit more complicated. This is just meant to give you a more concrete idea of what the class ArrayList looks like.
  22. 22.  2016, Marcus Biel, http://www.marcus-biel.com/ ArrayListpackage java.util; public class ArrayList<E> { private static final int DEFAULT_CAPACITY = 10; private Object[] elementData; private int size; public E get(int index) {…} public boolean add(E e) {…} public E remove(int index) {…} […] } As you can see, ArrayList is just a class anyone could have written, given enough time and knowledge. There is no black magic. You can find the actual source code online.
  23. 23.  2016, Marcus Biel, http://www.marcus-biel.com/ ArrayListpackage java.util; public class ArrayList<E> { private static final int DEFAULT_CAPACITY = 10; private Object[] elementData; private int size; public E get(int index) {…} public boolean add(E e) {…} public E remove(int index) {…} […] } However, don’t rely too much on internals that you spot in the source code, as they may change any time, if they are not defined in the Java language specification.
  24. 24.  2016, Marcus Biel, http://www.marcus-biel.com/ private static final int DEFAULT_CAPACITY = 10; private Object[] elementData; private int size; public E remove(int index) {…} public boolean add(E e) {…} public E get(int index) {…} package java.util; public class ArrayList<E> { […] } ArrayList default capacity is the initial size of the array, when you don’t specify it as I recommended before.
  25. 25.  2016, Marcus Biel, http://www.marcus-biel.com/ private Object[] elementData; private int size; public E remove(int index) {…} private static final int DEFAULT_CAPACITY = 10; public boolean add(E e) {…} public E get(int index) {…} package java.util; public class ArrayList<E> { […] } ArrayList “elementData” is the Array used to store the elements of the ArrayList.
  26. 26.  2016, Marcus Biel, http://www.marcus-biel.com/ private Object[] elementData; private int size; public E remove(int index) {…} private static final int DEFAULT_CAPACITY = 10; public boolean add(E e) {…} public E get(int index) {…} package java.util; public class ArrayList<E> { […] } ArrayList int size is the number of elements the ArrayList currently holds.
  27. 27.  2016, Marcus Biel, http://www.marcus-biel.com/ private Object[] elementData; private int size; public E remove(int index) {…} private static final int DEFAULT_CAPACITY = 10; public boolean add(E e) {…} public E get(int index) {…} package java.util; public class ArrayList<E> { […] } ArrayList …get…
  28. 28.  2016, Marcus Biel, http://www.marcus-biel.com/ public boolean add(E e) {…} private Object[] elementData; private int size; public E remove(int index) {…} private static final int DEFAULT_CAPACITY = 10; public E get(int index) {…} package java.util; public class ArrayList<E> { […] } ArrayList …add..
  29. 29.  2016, Marcus Biel, http://www.marcus-biel.com/ private Object[] elementData; private int size; public E remove(int index) {…} private static final int DEFAULT_CAPACITY = 10; public boolean add(E e) {…} public E get(int index) {…} package java.util; public class ArrayList<E> { […] } ArrayList … and remove are some of the many functions ArrayList provides. We will look at those methods now.
  30. 30.  2016, Marcus Biel, http://www.marcus-biel.com/ Methods Overview <<interface>> Collection <<interface>> List ArrayList implements extends So let me give you a short overview of the methods of the ArrayList class. To make things easy for you, I have broken up the overview into methods belonging to the java.util.Collection interface and methods belonging to the java.util.List interface.
  31. 31.  2016, Marcus Biel, http://www.marcus-biel.com/ Collection Interface Hierarchy <<interface>> Collection Okay, so let’s start with the methods belonging to the java.util.Collection interface. The contract of the collection interface does not guarantee any particular order and therefore does not provide any index or order related methods.
  32. 32.  2016, Marcus Biel, http://www.marcus-biel.com/ boolean add(E e) boolean addAll(Collection<? extends E> c) boolean remove(Object o) boolean removeAll(Collection<?> c) java.util.Collection So here you can see the first set of methods that implement the collection interface. So what I say about these methods does not only apply to ArrayList, But also to all classes that implement the collection interface.
  33. 33.  2016, Marcus Biel, http://www.marcus-biel.com/ boolean add(E e) boolean addAll(Collection<? extends E> c) boolean remove(Object o) boolean removeAll(Collection<?> c) java.util.Collection The method “boolean add” appends the element to the end of the collection to the next empty cell of the underlying array.
  34. 34.  2016, Marcus Biel, http://www.marcus-biel.com/ boolean add(E e) boolean addAll(Collection<? extends E> c) boolean remove(Object o) boolean removeAll(Collection<?> c) java.util.Collection boolean addAll – appends all given elements to the end of the collection.
  35. 35.  2016, Marcus Biel, http://www.marcus-biel.com/ boolean add(E e) boolean addAll(Collection<? extends E> c) boolean remove(Object o) boolean removeAll(Collection<?> c) java.util.Collection The stuff in angle brackets is related to Generics. it ensures that no one can call such a method with the wrong arguments. I will tell you more in my upcoming slides about Generics.
  36. 36.  2016, Marcus Biel, http://www.marcus-biel.com/ boolean add(E e) boolean addAll(Collection<? extends E> c) boolean remove(Object o) boolean removeAll(Collection<?> c) java.util.Collection boolean remove – removes the first occurrence of the element you specify from the collection.
  37. 37.  2016, Marcus Biel, http://www.marcus-biel.com/ java.util.Collection boolean add(E e) boolean addAll(Collection<? extends E> c) boolean remove(Object o) boolean removeAll(Collection<?> c) boolean removeAll – removes the given elements from the Collection.
  38. 38.  2016, Marcus Biel, http://www.marcus-biel.com/ Iterator<E> iterator() int size() boolean contains(Object o) java.util.Collection The iterator method returns an object you usually use in a loop to move from one element to the next element of the collection, step by step. You say “I iterate over the collection” – hence the name Iterator.
  39. 39.  2016, Marcus Biel, http://www.marcus-biel.com/ java.util.Collection Iterator<E> iterator() int size() boolean contains(Object o) int size() – returns the number of elements of the collection.
  40. 40.  2016, Marcus Biel, http://www.marcus-biel.com/ Iterator<E> iterator() int size() boolean contains(Object o) java.util.Collection boolean contains - returns true if the collection contains at least - one instance of the element you specify.
  41. 41.  2016, Marcus Biel, http://www.marcus-biel.com/ void clear() boolean isEmpty() <T> T[] toArray(T[] a) java.util.Collection void clear() - removes all elements from the collection.
  42. 42.  2016, Marcus Biel, http://www.marcus-biel.com/ void clear() boolean isEmpty() <T> T[] toArray(T[] a) java.util.Collection boolean isEmpty() – This returns true if the collection contains no elements.
  43. 43.  2016, Marcus Biel, http://www.marcus-biel.com/ void clear() boolean isEmpty() <T> T[] toArray(T[] a) java.util.Collection and toArray – returns an array containing all of the elements of the collection.
  44. 44.  2016, Marcus Biel, http://www.marcus-biel.com/ Collection Interface Hierarchy <<interface>> List ArrayList Let’s move on to the methods of the java.util.List interface. The methods are similar in part to the methods we just looked at, but they differ in that they require an order on the elements of the list.
  45. 45.  2016, Marcus Biel, http://www.marcus-biel.com/ java.util.List boolean add(int index, E element) E remove(int index) So here again you should know that everything I say about these methods does not only apply to ArrayList but to all classes that implement the list interface.
  46. 46.  2016, Marcus Biel, http://www.marcus-biel.com/ java.util.List boolean add(int index, E element) E remove(int index) This add method with an index parameter acts actually more like an insert method. It allows you to insert an element at any index position of the list, instead of just adding the element to the end of the list. In the process, the elements of the underlying array will be shifted to the right and migrated to a larger array if necessary.
  47. 47.  2016, Marcus Biel, http://www.marcus-biel.com/ java.util.List boolean add(int index, E element) E remove(int index) The remove index method allows to remove an element from any index position of the list. Similar to the add method we just looked at, this might require to shift the remaining elements of the underlying array to the left.
  48. 48.  2016, Marcus Biel, http://www.marcus-biel.com/ E get(int index) int indexOf(Object o) int lastIndexOf(Object o) java.util.List The get by index method returns an element from any given position of the list.
  49. 49.  2016, Marcus Biel, http://www.marcus-biel.com/ java.util.List E get(int index) int indexOf(Object o) int lastIndexOf(Object o) The indexOf method takes an object and returns the index of the first occurrence of the element in the list or “-1” if the element is not found.
  50. 50.  2016, Marcus Biel, http://www.marcus-biel.com/ java.util.List E get(int index) int indexOf(Object o) int lastIndexOf(Object o) int lastIndexOf returns the index of the last occurrence of the element in the list and as before, “-1” if the element is not found.
  51. 51.  2016, Marcus Biel, http://www.marcus-biel.com/ java.util.List List<E> subList(int fromIndex, int toIndex) void sort(Comparator<? super E> c) List subList returns a view of the list starting with the position you specify as fromIndex and ending one position before the one you specify as toIndex.
  52. 52.  2016, Marcus Biel, http://www.marcus-biel.com/ java.util.List List<E> subList(int fromIndex, int toIndex) void sort(Comparator<? super E> c) Last but not least, the sort method sorts the list following the order of the given Comparator.
  53. 53.  2016, Marcus Biel, http://www.marcus-biel.com/ Copyright © 2016 Marcus Biel All rights reserved

×