Week 3 + Week 2 and 1 Review Flashcards
(252 cards)
What is a generic type?
A generic class or interface that is parameterized over types.
Why use Generics?
Generics enable types (classes and interfaces) to be parameters when defining classes, interfaces, and methods.
Two types of generics? What does each do?
Generic method - introduce their own type parameters.
Generic class -
What is a generic method?
Exactly like a normal method but a generic method has type parameters that are cited by actual type.
Benefits of generic code over non-generic?
- Stronger type checks at compile time. A java compiler applies strong type checking to generic code and issues errors if the code violates type safety.
- Elimination of casts.
What are generics commonly used with?
Collections.
What is a generic called when declared on a class?
Generic type.
Why are generics useful?
Because it can help you to restrict a class to only accept objects of a given type and the compiler will prevent you from using any other type.
Why would the following code benefit from generics? How would you implement that code?
List names = new ArrayList();
names. add(“Alice”);
name. add(new Object());
In this code you would have to hope that other developers would know to use a certain data type. Generics would make it so the developer can only use objects of a given type.
List names = new ArrayList<>();
names. add(“Alice”);
names. add(new Object());
What is the Collections Framework?
A set of classes and interfaces that implement commonly used data structures.
What is a collection?
A single object which acts as a container for other objects.
Are collections iterable?
yes.
Main difference between Collection and Map?
Collection’s are iterable, maps are not.
Three interfaces derived from the Collection interface?
List.
Queue.
Set.
Classes derived from the List interface?
ArrayList.
Vector.
LinkedList.
Classes derived from the Queue interface?
LinkedList.
Priority Queue.
Classes derived from the Set interface?
Hash Set.
Linked Hash Set.
What is a List in Java?
A collection that is ordered and preserves the ordered in which elements are inserted into the list.
What is a Vector?
An older class which implements List.
A thread safe implementation of an ArrayList.
Vector.
What should you use for implementing a stack?
ArrayDeque.
ArrayList(class) implements _______ which extends _______.
List (interface)
Collection
Set Extends _______ which Extends ________ which Implements ________.
Sorted Set.
Navigable Set.
Treeset.
What is the Set interface?
an unordered collection of objects in which duplicate values cannot be stored.









