Chapter 15 - The JAVA Collections Framework

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

What classes implement the the Map interface?

The HashMap and TreeMap classes. After constructing a HashMap or TreeMap, you can store the reference to the map object in a Map reference: Map<String, Color> favoriteColors = new HashMap<>();

run-time stack

The data structure that stores the local variables of all called methods as a program runs.

What method returns the value associated with a key?

The get method returns the value associated with a key. Color julietsFavoriteColor = favoriteColors.get("Juliet"); If you ask for a key that isn't associated with any values, the get method returns null.

What is backtracking and what data structure is best used for it?

The process of returning to a choice point and trying another choice. By using a stack, you return to your most recent choices

What method do you use in a map object to remove an association?

The remove method. favoriteColors.remove("Juliet");

Given a Set<String> names, compare the code segments if (names.contains("Fred")) { System.out.println("found"); } and for (String element : names) { if (element.equals("Fred")) { System.out.println("found"); } }

They have the same result, but the first one is more efficient.

Why is it considered good style to store a reference to a HashSet or TreeSet in a variable of type Set such as the following: Set<String> words = new HashSet<>();

This way, you have to change only one line if you decide to use a TreeSet instead

If you want to visit a sets elements in sorted order choose a _______ set

Tree

What do you need to consider when making hash code for your classes and objects?

Use the Objects.hash method to take the instance variables of objects and combine them for a unique hash code Make sure that when you supply your own hashCode method, that provide a compatible equals method to differentiate between two objects that have the same hash code.

What is the value of numbers after the following set operations? Set<Integer> numbers = new TreeSet<>(); for (int i = 1; i <= 10; i++) { numbers.add(i); } for (int i = 2; i <= 10; i = i + 2) { numbers.remove(i); } for (int i = 3; i <= 10; i = i + 3) { numbers.add(i); }

[1, 3, 5, 6, 7, 9]

Consider the following statements: List<String> letters = new LinkedList<>(); letters.add("F"); letters.add("R"); letters.add("E"); letters.add("D"); ListIterator<String> iter = letters.listIterator(); iter.next(); iter.next(); iter.remove(); iter.next(); iter.add("X"); What is the contents of letters?

[F, E, X, D]

generic class

a class with one or more type parameters LinkedList, Iterator, and Array List are generic classes such that you specify the type of of the list element in the angle brackets, such as LinkedList<String> or LinkedList<Employee>

queue

a collection of items with "first in, first out" retrieval items are added to one end (the tail) and they are removed from the other end (the head)

map

a data structure that keeps associations between key and value objects

collection

a data structure that provides a mechanism for adding, removing, and locating elements groups together elements and allows them to be retrieved later

stack

a data structure with "last-in, first-out" retrieval elements can be added and removed only at one position, called the top of the stack

hash function

a function that computes an integer value from an object in such a way that different objects are likely to yield different values.

doubly-linked list

a linked list in which each link has a reference to both its predecessor and successor links You can use the previous and hasPrevious methods of the ListIterator interface to move the iterator position backward.

hash code

a value that is computed by a hash function

The ______ method inserts an object before the iterator position

add() You can visualize insertion to be like typing text in a word processor. Each character is inserted after the cursor, then the cursor moves past the inserted character

priority queue

an abstract data type that enables efficient insertion of elements and efficient removal of the smallest element

iterator

an object containing data and methods to iterate through a collection of data, allowing processing of one data item at a time.

set

an unordered collection that allows efficient addition, location, and removal of elements it is faster to find an element in a set than in a list

What are the mechanisms of a set to arrange the elements so that the operations of finding, adding, and removing elements become more efficient?

classes, provided by the Java library, called hash tables and binary search trees

The LIST interface describes an important category of collections. In Java, what is a list?

collection that remembers the order of its elements use a list whenever you want to retain the order that you established

a good hash function should minimize _____________

collisions - which are identical hash codes for different objects

The _______ method tests whether an element is in the set:

contains if (names.contains("Juliet")) . . . If your set collects String or Integer objects, you don't have to worry. Those classes provide an equals method. However, if you implemented the element type yourself, then you need to define the equals method

Java collections framework

data structures that can be used to organize and manipulate data efficiently includes 2 types of containers: 1.collections 2.maps

A set is a data structure that does not allow _____ elements.

duplicate If an element is added twice, the second insertion is ignored.

Java 8 added a useful merge method to the Map interface. You specify a key, a value to be used if the key is not yet present, and a function to compute the updated value if the key is present. Show an example where the function is specified in lambda expression:

frequencies.merge(word, 1, (oldValue, notPresentValue) -> oldValue + notPresentValue); does the same as these 4 lines of code

In order to use a hash table, the elements must have a method to compute those integer values. This method is called ___________.

hashCode

map

keeps associations between key and value objects stores the keys, values, and the associations between them not considered a collection

add and remove set elements with the add and remove methods:

names.add("Romeo"); names.remove("Juliet");

You can move the iterator position with the ______ method

next The next method returns the element that the iterator is passing.

The __________ method removes the object that was returned by the last call to next or previous.

remove() It can be called only once after calling next or previous, and you cannot call it immediately after a call to add. If you call the method improperly, it throws an IllegalStateException.

The keys of a map form a ______.

set

What method adds an association in your map object?

the put method: favoriteColors.put("Juliet", Color.Red); You can change the value of an existing association, simply by calling put again: favoriteColors.put("Juliet", Color.BLUE);

The Set interface has the same methods as the ________ interface.

Collection The parameter types and return types of the methods are the same, but the Set methods ensure that there are no duplicates.

If a method can operate on arbitrary collections, use the _________ interface type for the parameter variable

Collection public static void removeLongWords(Collection<String> words)

hash tables

Hash tables contain a table with data and a mapping function which assigns each entry a corresponding hash

The ArrayList class implements the List interface. What are its inefficiencies?

If an element is added or removed, the elements at larger positions must be moved

What does it mean that the LinkedList class implements the Queue interface?

It means that whenever you need a queue you can initialize a Queue variable with a LinkedList object.

The ______________ class also implements the List interface and unlike an array list, it allows for efficient insertion and removal of elements in the middle of the list.

LinkedList

Are duplicates allowed in sets?

No. If you add an element to a set that is already present, the insertion is ignored.

The next method throws a ______________________ if you are already past the end of the list.

NoSuchElementException You should always call the iterator's hasNext method before calling next—it returns true if there is a next element.

After you construct a HashSet or a TreeSet, you should keep the reference to the set object in a variable of type _____.

Set After constructing the object, the implementation no longer matters; only the interface is important.

binary search tree

A data structure very similar to a tree with the following additional restrictions. Each node can have only 0, 1 or 2 leaf nodes. All left nodes and all of its descendants have smaller values that the root node, while all right nodes and all of its descendants have larger values than the root node.

linked list

A linear data structure, much like an array, that consists of nodes, where each node contains data as well as a link to the next node, but that does not use contiguous memory. Sequential order is efficient, but random access is not.

List Iterators

used to access elements inside a linked list

set iterator

visits the elements in the order in which the set implementation keeps them you cannot add an element to a set at an iterator position, so you just add elements directly to a set you can remove an element at an iterator position


संबंधित स्टडी सेट्स

Health assessment exam 3 Fall 2023

View Set

Soc Final Exam - Comprehensive Review

View Set

Future Technologies in Safety and Risk Management

View Set

Factoring Polynomials: Difference of Squares / Instruction / Summary / Assignment

View Set