Skillstorm Intro to Coding - Data Structures in Java Flashcards
In Java, every Object has two comparison methods. What are they?
equals and hashCode Methods
The equals and hashCode methods must be _________ to work properly.
Why?
Overridden
Without them, you would have to create very large “if” comparisons, comparing every field from an object, making code really confusing and hard to read.
Fill in the Blanks of the attached image
boolean equals Object
Fill in the blanks of the following code:
public _____ _____ () {
final int prime = 31;
int result = 1;
result = prime * result + size;
if (topping == null) {
result = prime * result + 0;
}else {
result = prime * result + topping.hashCode();
}
return result;
}
int hashCode
A List is an _________ collection that may contain _________ elements.
Ordered
duplicate
What are the two types of general-purpose list implementations?
ArrayList and LinkedList
Are Java LinkedList automatically Doubly LinkedList?
Yes
ArrayLists are resizable. But by what size do they increase when they are full?
50%
ArrayList and LinkedList are both ordered by ________ position.
index
Because you can add and remove from the heads and tails of LinkedList, this makes them perfect for __________ and __________.
Stacks (and) Queues
What is Last In First Out? Stacks or Queues
Stacks
What is First In First Out? Stacks or Queues
Queues
Manipulating LinkedList. See image…
add
add
remove
Manipulating ArrayList. See attached image…
< String >
add
get
size
What is a Queue?
A collection for holding elements prior to processing
A ________ is a double-ended queue.
Deque
A Deque supports ________, removal, and __________ of elements at both end-points!
insertion,
examination
Deque interface implements both ______ and ______ at the same time.
Stacks
Queues
Queue Manipulation. See attached images…
queue. offer
queue. peek
queue. poll
Stack Manipulation. See attached images…
push
push
pop
peek
- Is a Map an object that maps equal keys?
- What are mapped to unique keys?
- Do Maps include methods for basic operations?
- Do Maps include methods for non-bulk operations?
- Do Maps includes methods for collection views?
- False: They map unique keys
- Values
- True
- False: They include methods for bulk operations
- True
Java contains three general-purpose Map implementations. What are they?
HashMap, TreeMap, and LinkedHashMap
HashMap Manipulation. See attached image…
put
put
get
size
containsKey
Can a Set contain duplicate elements?
No
Sets add a stronger contract on the behavior of the _______ and _______. It allows Set instances to be compared meaningfully even if their implementation types differ.
equals (and) hashCode
What are the three general-purpose Set implementations?
HashSet, TreeSet, and LinkedHashSet
HashSet Manipulation. See attached images…
public int hashCode
public boolean equals Object obj
LinkedList is a(n) ______ collection.
Ordered
LinkedList has an underlying array that changes size as needed? True or False
False
Two objects with the same hashCode must be equal? True or False
False
A Queue is a first-in last-out collection? True or False
False
Two objects that are equal must have the same hashCode? True or False
True
The equals method in java.lang.Object compares two objects and returns a(n)______.
boolean
A LinkedList in Java is _______ by default.
Doubly-linked
The performance of accessing a value in a HashMap is ______________.
Constant Time
An ArrayList will grow ___ in size when it reaches its maximum capacity.
50%
A Stack is a ______________ collection
last-in first-out
A HashSet will maintain duplicate values. True or False….
False
In Java with Object Equality, you can decide when two objects are equivalent, but with what method and how is it written in Java?
- The method is ==> java.lang.Object.equals method
- In Java it is written as ==> public boolean equals(Object other)
In Object Equality, does it check the memory location by default?
Yes
In Object equality what must you override to compare object state?
The method
True or False, in Object Equality, some data structures require unique objects…
True