---
# System prepended metadata

title: Thinking in Java (Chapter 11)
tags: [OOP, Java]

---

Thinking in Java (Chapter 11)
===
###### tags: `Java` `OOP`
1. @SuppressWarnings and the argument indicates that "unchecked" warnings only should be suppressed
1. cast is no longer necessary when fetching items back out from the List. Since the List knows what type it holds, it does the cast for you when you call get( ).
1. A List must hold the elements in the way that they were inserted, a Set cannot have duplicate elements, and a Queue produces the elements in the order determined by a queuing discipline(usually the same order in which they are inserted).
1. ArrayList allows you to look up an object using a number, so in a sense it associates numbers to objects. A map allows you to look up an object using another object. It’s also called an associative array
1. LinkedList has additional methods that are not in the List interface, and a TreeMap has methods that are not in the Map interface. If you need to use those methods, you won’t be able to upcast to the more general interface
1. All Collections can be traversed using the foreach syntax
1. Arrays.asList( ) takes either an array or a comma separated list of elements (using varargs) and turns it into a List object
1. Collections.addAll( ) takes a Collection object and either an array or a comma-separated list and adds the elements to the Collection
1. underlying array cannot be resized
1. you can use Arrays.asList( ) to produce input for the constructor. However, Collections.addAll( ) runs much faster, and it’s just as easy to construct the Collection with no elements and then call Collections.addAll( )
1. The Collection.addAll( ) member method can only take an argument of another Collection object, so it is not as flexible as Arrays.asList( ) or Collections.addAll( ), which use variable argument lists.
1. use the output of Arrays.asList( ) directly, as a List, but the underlying representation in this case is the array, which cannot be resized. If you try to add( ) or delete( ) elements in such a list, that would attempt to change the size of an array, so you’ll get an "Unsupported Operation" error at run time
1. it’s possible to insert a "hint" in the middle of Arrays.asList( ), to tell the compiler what the actual target type should be for the resulting List type produced by Arrays.asList( ). This is called an explicit type argument specification.
1. Maps are more complex, the Java standard library does not provide any way to automatically initialize them, except from the contents of another Map.
1. The storage order of HashSet can seem nonsensical
1. You can use a TreeSet, which keeps the objects in ascending comparison order, or a LinkedHashSet, which keeps the objects in the order in which they were added.
1. Map.put(key, value) adds a value (the thing you want) and associates it with a key (the thing you look it up with). Map.get(key) produces the value associated with that key
1. The basic ArrayList, which excels at randomly accessing elements, but is slower when inserting and removing elements in the middle of a List.
1. The LinkedList, which provides optimal sequential access, with inexpensive insertions and deletions from the middle of the List. A LinkedList is relatively slow for random access, but it has a larger feature set than the ArrayList.
1. The retainAll( ) method is effectively a "set intersection" operation, in this case keeping all the elements in copy that are also in sub. Again, the resulting behavior depends on the equals( ) method.
1. addAll( ) method that allows you to insert the new list in the middle of the original list
1. you can convert any Collection to an array using toArray( ).
1. The Java Iterator can move in only one direction
1. display( ) contains no information about the type of sequence that it is traversing, and this shows the true power of the Iterator: the ability to separate the operation of traversing a sequence from the underlying structure of that sequence
1. iterators unify access to containers.
1. ListIterator is bidirectional. It can also produce the indexes of the next and previous elements relative to where the iterator is pointing in the list, and it can replace the last element that it visited using the set( ) method
1. getFirst( ) and element( ) return the head (first element) of the list without removing it, and throw NoSuchElementException if the List is empty. peek( ) is a slight variation of those two that returns null if the list is empty. removeFirst( ) and remove( ) remove and return the head of the list, and throw NoSuchElementException for an empty list, and poll( ) is a slight variation that returns null if this list is empty. addFirst( ) inserts an element at the beginning of the list. offer( ) is the same as add( ) and addLast( ). They all add an element to the tail (end) of a list.
1. LinkedList has methods that directly implement stack functionality
1. after the class name tells the compiler that this will be a parameterized type
1. TextFile is inherited from List. The TextFile constructor opens the file and breaks it into words according to the regular expression "\\W+", which means "one or more letters
1. TreeSet sorting is done lexicographically so that the uppercase and lowercase letters are in separate groups. If you’d like to sort it alphabetically, you can pass the String.CASE_INSENSITIVE_ORDER Comparator (a comparator is an object that establishes order) to the TreeSet constructor
1. autoboxing converts the randomly generated int into an Integer reference that can be used with the HashMap (you can’t use primitives with containers)
1. A queue is typically a “first-in, first-out" (FIFO) container
1. Queues are especially important in concurrent programming
1. LinkedList can be used as a Queue implementation
1. the Standard C++ Library has no common base class for its containers—all commonality between containers is achieved through iterators
1. In java implementing Collection also means providing an iterator( ) method
1. in the implementation of display(Collection) the foreach construct can be used
1. Trying to pass an array as an Iterable argument fails. There is no automatic conversion to an Iterable; you must do it by hand.
1. Arrays.asList( ) produces a List object that uses the underlying array as its physical implementation. If you do anything to that List that modifies it, and you don’t want the original array modified, you should make a copy into another container.
1. interface java.util.RandomAccess, which is attached to ArrayList but not to LinkedList
