• Shuffle
    Toggle On
    Toggle Off
  • Alphabetize
    Toggle On
    Toggle Off
  • Front First
    Toggle On
    Toggle Off
  • Both Sides
    Toggle On
    Toggle Off
  • Read
    Toggle On
    Toggle Off
Reading...
Front

Card Range To Study

through

image

Play button

image

Play button

image

Progress

1/51

Click to flip

Use LEFT and RIGHT arrow keys to navigate between flashcards;

Use UP and DOWN arrow keys to flip the card;

H to show hint;

A reads text to speech;

51 Cards in this Set

  • Front
  • Back

What are the 3 key players in the Java Collection Frameworks?

Interfaces - abstract notion of a collection type


Implementation - Specific concrete implementations of a collection


Algorithms - searching and sorting





What are the two super interfaces in the Collections Framework?

Collections and Map

What are the 4 main sub-interfaces to Collection ?

Collection -> Set, List, Queue, Deque

What is Java's strategy for keeping the number of core collection interfaces manageable?

Rather than having the interfaces define the exact supported methods, exceptions that are not supported by the specific implementation will throw UnsupportedOperationException

What is an example of when UnsupportedOperationException being thrown?

The specific implementation of a list might not allow elements to be added once created (Collections.unmodifiableList(existingCollection). In this instance an operation that attempts to add would throw this exception

How would you know which operations were supported for a given implementation?

Up to the API docs to show this in their JavaDoc

What 7 types of operation are available as a bare minumum on a java collection?

Adding


Clearing


Checks for element(s) contained within


Checks for empty/size of collection


Associated iterator from the collection


Means to create an array


Remove methods from an array

When would you use each type of collection?

Set - when I wanted a collection without duplicates


List when I want to order a collection, and control where an element gets inserted


Queue - to hold elements prior to processing


Deque - as per queue


Map - key to values

When would I use a sorted set or map

When the natural order is important - e.g. a sortedset for a register would be in alphabetical order


A sortedmap would be useful when I was building a book index, or a telephone directory



What useful convention do the implementations of the Collections interface have?

A constructor argument that takes a collection argument - allowing maximum flexibility in creating new types.

Maps don't have this since they are descended from the collection interface

What 3 ways can you traverse a collection? Example of each

Aggregate operations - Java 8 - declaratively operate on the collection by calling operations on it

For-each - simple loop over elements in a collection

Iterator - call the iterator on the object to get 'safe' access to modify when you iterate through it.

What methods does an Iterator have?

hasNext()


next() - the element that


remove() - the only safe way to do so during iteration

What are the 5 bulk operations of a collection?

clear


containsAll


removeAll


retainAll


addAll

Which packages are iterator implementations stored in?
Trick question: they are inner classes of the Collections, because they are conceptually coupled to the collection

The iterator interface is a standalone class file though

When would using an iterator *still* throw an exception?

If the collection that is being returned doesn't support removal then it won't matter if it's been accessed via an iterator. For example, Arrays.asList returns an inner class called Arrays$ArrayList which doesn't support removal, unlike the java.util.ArrayList we're used to.

How do the 3 implementations of set differ?

Hashset will store elements in a hashtable has better performance, but no guarantees on ordering - a sequential search will not necessarily yield elements in the order they were added, or any natural order.




Treeset is sorted - e.g country list in a drop down, would be retrieved.




LinkedHashset would use a linkedlist as a hastable with a linked list running through it -i.e. elements appear in the



How would you provide the union of two collections?

Use the addAll method to add only elements that aren't already included in the list

How would you provide the intersection of two collections?

Use the retainAll method to include only elements present in both sets



How could you provide only the unique elements in two collections?

Use the removeAll method to remove only elements present in both sets

Why does implementing equals but not hashCode violate a set?

If the object didn't implement equals then the set would consider all instances unique.




When equals is implemented, the add methods will look up the hash of the object about to be added. If there is an entry for the hashcode and equals is implemented, then the object will replace the existing entry, hence duplicates




Not implementing hashCode means that each object will get its own location in the map.

What operations does the List interface provide

Positional access - getting/setting and removing at numerical posotions in a list


Search - look for an object rather than a position


Iteration - listIterators provide extended operations


Range View - lists between two points



What are the two List implementations offered as standard?

ArrayList and LinkedList

What are the similarities and differences between add and set and remove?

Both take an index as position (overloaded to 0)


Add/Remove will insert/remove from a selected index and return a boolean if the collection was modified


Set and remove will return the old value being overwritten or removed, respectively.



What is the difference between Arrays.asList() and new ArrayList?

A list constructed from an array will have the a refrnece to the backing array so changes to the collection will reflect to the array, and vice-versa




For this reason, add/remove methods are not supported on ArrayLists created from asList() - since this would affect the backing array too.

What extra features does listIterator offer

Previous and hasPrevious


previousIndex and nextIndex

What useful algorithms does the Collections interface offer for dealing with lists?

sort


shuffle


reverse


rotate - move elements by n positions


swap


replaceAll (swap all values for another)


fill (replace global)


binarySearch


indexOfSublist


lastIndexOfSublist

Why would I use a queue

When I want to hold elements before I process them, typically I would access the first element in the queue rather than by the 'random' access of a list.

What methods does a queue have

add() - put an element in the queue


remove() take next element from the queue


element() examines the next element according to the queue ordering (non-destructive version of remove)

What are the special value counterparts of these operations

offer(e) returns false instead of throwing an exception if add fails - it's only intended for use on a bounded queue




poll() will return null if the queue is empty, whereas remove would throw an exception




ditto peek() for element

How does a priority queue differ from a standard queue?

It uses the natural ordering (or the rules of the comparator passed) to sort, rather than the order inserted

What is a deque

A collection that implements both queue and stack so LIFO and FIFO

What are the three ways of viewing a map as a collection, and how do they differ

keySet --> all the keys of a map (set because they are unique by definition


values --> all the values in the map - a collection, since values can be duplicates


entrySet --> a set of each key to value mapping

How can you alter a map whilst iterating

Use an entry set and the set value method

How can I check if two maps have the same keys if not the same values?

if (s1.keySet().equals(s2.keySet())

How can I check if the same keys *and* values are contained in the same map?

if (s1.entrySet().equals(s2.entrySet())

How does object sorting in a collection work?

The objects in the collection will sort as per the logic in their compareTo implementation of the Comparable interface, known as natural ordering

What if an object doesn't implement comparable?

You can provide your own Comparator implementation that provides a compare and equals for a given class

What if you don't want to use the default compareTo of a class that implements comparable?

You can provide your own Comparator implementation that provides a compare and equals for a given class

Two reasons that Collections.sort would throw a ClassCast

The collection is of a type that doesn't implement comparable


The instances of elements in the collection aren't of a suitable type for the comparator

How can you implement a reverse sort with a comparator

Flip the parameter compare around i.e. public int compare(Element o1, Element o2) { return o2.compareTo(o1);}




So that the value of the *second* parameter is assessed against the first, resulting in a reverse sort

Why is this bad? return e1.number() - e2.number();

Because when e2 - e1 produces a low enough number (that wraps around the limits of an int, it will produce a value that is actually positive, and result in the wrong order)

What is the definition of an implementation of a collection?

The actual data object used to store the collection

What is the definition of a 'general-purpose' implementation?

The one you'd use under normal circumstances for that kind of collection

What are the characteristics of a general purpose implementation?

Not thread safe (don't pay for a featrure you might not use) for performance


Fail-fast iterators


Serializable


Implements all the optional operations of the interface

How should you use a collection that needs to be thread safe

Use the wrappers that exist in the concurrent collection classes

What are the advantages of a Hashset

Faster, access time is pretty much constant, as you access an object via it's hash


Treeset

What is elegant about an EnumSet

You can use it to return a restricted range of type-safe values using .of() or range()

If I had a range of values that didn't change but I wanted to traverse them quickly, which would be the best implementation and why?

CopyOnWriteArraySet uses an array under the covers which is faster, and also immutable.




The downside is adding to it is slower as the underlying array is copied and new elements added to the end.

What is the expected hieracy of speed for maps

HashMap - general case


LinkedHashset - as long as accessing elements near the top of the list


Treeset - when searching for elements matching the natural order, they might be quicker than a hashset

How do the wrapper methods in collections work?

Decorator pattern to intercept operations to maintain the contract of the described operation, e.g. Unmodifiable will intercept add and remove methods
Accessed via static factory methods from Collections class

What are convenience methods for collections that are useful to know?

emptySet


singleton


asList


nCopies