Week 12 Flashcards

1
Q

What is the purpose of the collections framework in Java?

A

Provides a set of interfaces and classes that allow developers to use collections of objects in an organized /efficient way

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Java collections framework: Common operations

A

Add, remove, search for elements, iterate/sort elems, and perform opeartions like union, intersection, and difference

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Benefits of the JCF

A

Productivity, readability, maintainability, fewer bugs, greater performance, generic programming

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is the collection interface in Java?

A

The root interface of the JCF.
Defines common methods,
Represents a group of objects called “elements”
Provides methods for adding/removing/querying elements in the collection

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What are the 4 types of collections interfaces?

A

List, Set, Queue, and Map

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

List, Set, Queue, and Map. What do they represent?

A

List - Ordered collection of elements
Set - Collection of unique elements
Queue - Collection of elements with a specific ordering
Map - Represents a collection of key-value pairs

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Doe the “List” interface allow duplicate elements? How about the Set interface?

A

List allows duplicate elements, Set does not

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What does peek() do?

A

Allows you to view the top element on the stack without removing it.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What sort of data structure style does a stack in Java follow?

A

Last in first out (Elements added to the top, removed from the top)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Queue vs Stack

A

Queue = First in first out (Elements added tot he rear end, removed from the front)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Does a queue provide access to elements in the middle?

A

No

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What does the map interface in Java represent?

A

A collection of key-value pairs. Each key is unique and maps to a corresponding value.

Allows elements to be accessed by using their key rather than their index or value

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

add(), remove(), contains(), size(), isEmpty(), iterator(), toArray()… What do these methods have in common?

A

These methods are available in the Java Collection Interface

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is the purpose of the diamond syntax? < >

A

It specifies generic types when creating objects of generic classes. Allows the compiler to infer the type based on the context. Improves readability.

// Before Java 7
List<String> myList = new ArrayList<String>();</String></String>

// With diamond syntax in Java 7 and later
List<String> myList = new ArrayList<>();</String>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

5 characteristics of linked lists

A
  1. Dynamic size (grow or shrink at any time)
  2. Efficient insertion or deletion at ends of list
  3. Do not require contiguous memory (unlike arrays) more efficient for memory
  4. Bad: Variable access time, needs to traverse each node from the beginning until the desired node is found
  5. Bad: Do not support random access to elements (unlike arrays), the items must be accessed sequentially from head or tail of list
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

This collection of elements does not allow duplicates and does not maintain an order, what is it?

A

A set

17
Q

Advantages of Set vs. List

A

Optimized organization of values for efficiency
Faster insert/removal of elems
Eliminates duplicates (efficient)

18
Q

What are the implementing classes of the set interface?

A

HashSet (based on a hash table) and TreeSet (based on a binary search tree)

19
Q

What is the purpose of the hashCode() method in a hash table?

A

Used to infer the hash code of an element, which is used to group together similar elements in the hash table

20
Q

What does the equals() method do? How is it used?

A

Determines if 2 elements in a hash table are equal. It’s used to ensure there are no duplicates allowed in the set

21
Q

What classes can be used in a hash set in Java?

A

Wrapper classes - String, Integer, Double, Point, Rectangle, Color

22
Q

This interface is used to define a natural order for objects, which is used for elements in TreeSet–where elements are kept in a sorted order

A

Comparable

23
Q

When do you use a TreeSet over a HashSet?

A

Use a TreeSet when you want to visit the sets elements in a sorted order. Otherwise, HashSet is more efficient.

24
Q

How can you declare/instantiate a set in Java?

A

Use the set interface

Set<String> name = new HashSet<>();
or
Set<String> name = new TreeSet<>();</String></String>

25
Q

A duplicate element is added to a set, what happens? When you try to remove an element that is not in a set, what happens?

A

Nothing for both. they’re ignored and not added/removed.

26
Q

Iterator<String> iter = names.iterator();
while (iter.hasNext()) {
String name = iter.next();
// Do something with name
}</String>

What does the above code do?

A

Iterates over each element in the “names” set

27
Q

Can you add an element to a set at an iterator position? How about remove?

A

You cannot add any elements, but you can remove them using “remove()”

28
Q

Maps. What is their purpose?

A

Associate keys with values, you can then sue the key to lookup values

29
Q

What is the purpose of this <String, HashSet<String>> ?</String>

A

Associates a String with a set of strings, like a thesaurus that lists synonyms for a given word

30
Q

Queues follow what order:

A

FIFO

31
Q

How are queues typically applied?

A

Print queues, print jobs are added to the queue and printed in the order they are added

32
Q

How do you initialize a queue? Where does it come from?

A

Queue<String> q = new LinkedList<>();</String>

Comes from the LinkedList class

33
Q

Can you use strings as elements in a priority queue?

A

Yes, as the elements are retrieved according to their priority, which can be determined by the natural order off the strings

34
Q

What advantage is there to declaring something a queue, rather than a linked list?

A

A queue object can only have queue operations invoked on it, maintains the integrity of the queue data structure

35
Q

ArrayLists to implement queues: yes or no?

A

No. Inefficient. Adding or removing elements at the head of the queue requires moving all other elements in the list