Level 131 Level 133
Level 132

[Quiz] Collections

97 words 0 ignored

Ready to learn       Ready to review

Ignore words

Check the boxes below to ignore/unignore words, then click save at the bottom. Ignored words will never appear in any learning session.

All None

Java Collections Framework
A unified architecture for representing and manipulating collections
Reduces programing effort
Benefits of Using Java Collections
a collection object can store an arbitrary number of other objects.
implement the Set interface
implement the Set interface
a class that supports dynamic arrays that can grow as needed. That is, an ArrayList can dynamically increase or decrease in size.
A Linked List is ordered by index position, like ArrayList, except that the elements are doubly linked to one another. So this linkage gives new methods for adding and removing from the beginning or …
implements the List interface
Priority Queue
implements the Queue interface
implements the Deque interface
The Hashmap provides an unsorted, unordered Map. With a Map the elements are not ordered. HashMap allows one null key and multiple null values in a collection
Hashtable is a synchronized counterpart to HashMap. Hashtable does not allow any null values.
a Set that maintains its elements in ascending order
a Map that maintains its mappings in ascending key order
Using Java Collections
Used via their interfaces
Traversing Collections
For Comprehensions
Bulk Operations
perform an operation on an entire collection
Collection Interface Array Operations
toArray methods are provided as a bridge b/w collections and older APIs that expect arrays on input
consists exclusively of static methods that operate on or return collections
Other Collections Methods
binarySearch, copy, frequency, min and max, reverse, shuffle, sort
used to represent the property of an object
a powerful mechanism for expressing direct relationships b/w variable
add a change listener to be notified when a property's value has changed.
assembled from one or more sources, known as dependencies
Binding APIs
High-Level API: simple way to create bindings for the most common use cases
if a dependency changes, the result of a binding is not immediately recalculated, but it's marked as invalid
Change event
indicates that the value has changed; requires eager evaluation
Invalidation event
generated if the current value is not valid anymore
JavaFx Collections
Consists of the following interfaces:
software that can do more than one thing at a time
a self-contained execution environment
Implement the Runnable interface
defines a single method, run, meant to contain the code executed in the thread
Subclass the Thread class
implements Runnable, though its run method does nothing
Concurrency in JavaFX
The JavaFX scene graph is not thread-safe and can only be accessed and modified from the UI thread also known as the JavaFX Application thread
javafx.concurrent Package
Worker interface- provides APIs that are useful for a background worker to communicate with the UI
Canceling Tasks
No reliable way in Java to stop a thread in process
Data Structure/Container
An Object that stores other objects referred to as data or elements.
True of False? To create a data structure is to create an instance from the class.
True ( You can then apply methods on the instance to manipulate the data structure like insertion or deletion)
Two Type of JCF Containers
(1) Collections [Stores a collection of elements] and (2) Maps [Stores key/value pairs (efficient for fast searching an element using a key)]
Set, List, and Queue
Three Types of JCF Collections
Interfaces in the JCF
Defines the Framework (ex. Sets, Iterator)
Abstract Classes in JCF
Provides partial implementation for convienience
Concrete Classes in the JCF
Implements the interfaces with concrete data structures
The Collection Interface
The Root interface for manipulating a collection of objects. ( Provides basic operations for adding & removing elements.)
Ask/request information from a database
True of False? A Collection may be a Set or a List?
What is a set?
a collection of distinct objects
Like a regular HashMap, except it can remember the order in which elements (name/value pairs) were inserted, or it can be configured to remember the order in which elements were last accessed.
implement java.lang.Comparable
What must a java object do inorder to be sortable?
what does the Comparable.compareTo method return
positive integer if the "this" object is larger, 0 if equal, neg integer if smaller
How to sort a collection of objects: one way
Collections.sort(collection): element implements Comparable interface and sort uses the implemented compareTo() method
How to sort collection of objects: more than one way
Collections.sort(<collection>, <Comparator>): create "comparator" object which implements Comparator (spec compare method). Pass collection and comparator object to sort method. Sort uses comparator's compare method
public interface Comparator<T> {
When to use a List?
when sequence matters: collections that know about index position
When to use a Set?
when uniqueness matters: collections that don't allow duplicates
When to use a Map?
when finding something by key matters: collections that use key-value pairs
How to add all the elements of an Arraylist, songList, to a HashSet, songSet?
Equal to (Relational Operators)
What is the default behavoir for hashCode()
return hashcode based on the objects memory address on heap
Why would you want to override hashCode()?
If you want to treat objects without the same address equally.
If you overrride Hashcode(), what other method of object must you override? Why?
equals(), because HashSet uses both to check for dupes. having the same hashcode does not guarantee equality, having differing hashcodes does preclude equality
add() method returns false
How do you know that an object was a dupe when added to a set.
an == comparison (test references)
What is the default behavior of equals()
relationship of hashcode and equality
If tow objects are equal, they must have same hashcode. If have same hashcode may not be equal (collision)
Hashset will use equals method to determine if perfect match
What happens when Hashset finds more than one object in a hash bucket?
How does TreeSet keep objects sorted?
compareTo(), unless you pass a comparator to the constructor
compiles, but blows up at runtime, because TreeSet tries to call compareTo() on object
What happens when you try to add an object that does not implement comparable to a TreeSet?
HashMap<String,Integer> scores = new HashMap<String, Integer>;
Declare and init a HashMap of key type String and value Integer
yes, b/c jvm will check at runtime
Can you pass an array of a subtype to an super-type defined parameter?
No, b/c jvm doesn't check at runtime
Can you pass an a collection of a subtype to a supertype defined parameter?
Why can you pass subtype Arrays, but not subtype collections?
Arrays are checked both at compile time (by compiler) and at run-time (by JVM), so attempt to add subtype to incompatible subtype array would be caught. However, collections are only checked at compile time…
public void takeThing(ArrayList< ? extends Animal> list)
Specify two possible signatures of a public void function "takeThing" that takes subTypes of Animal.
What do you use wildcards for?
Allow you to specify that a subtype can be passed in Supertypes place.
How do wildcards solve the problem?
compiler doesn't allow you to add to collection that is wildcarded
ArrayList<Dog> myDogArrayList = new ArrayList<Dog>();
ArrayList<Object> myDogArrayList = new ArrayList<Object>();
int[] myarray = new int[3];
Declare a normal integer array with size of 3.
original.charAt(i) === reverse.charAt(i);
Compare characters of two strings
Check if a character is a letter in a string
An object of a particular class. In programs written in the Java programming language, an instance of a class is created using the new operator followed by the class name.
has it owns copies of these members.
This means that each object (instance) that is created from the class...
What happens when an object is created?
some memory is allocated to hold all instance members of the class from which the object is created.
does not
Changing the value of an instance variable of an object does/does not affect the value of the same instance variable in another object of the same class
class name
Static method can be invoked through its
math class
Give an example of a class with static methods
How about variables?
Yes, variables can be static as well
s t a t i c modifier
Which modifier is used to create static methods/variables?
All Java methods must be contained in a ...
class variables
Variables that apply to a class as a whole , rather than a particular object of that class. Declared using the "static" keyword
How many copies of a variable exists if the variable is declared static?
When the CLASS is first referenced
When is memory space for a static variable created?
All objects instantiated from the class share its static variables
It is changed for all other objects
What happens when the value of the static variable is changed in an object?
Visibility modifiers
What modifier comes first?
Why can't static methods reference instance variables?
because instance variables don't exist until an object is created
Define a class called MyClass that extends MySuperclass and implements Yourinterface.