Docsity

Java HashMap and Student Comparator - Prof. Douglas J. Wokoun, Study notes of Computer Science

Partial preview of the text

Download Java HashMap and Student Comparator - Prof. Douglas J. Wokoun and more Study notes Computer Science in PDF only on Docsity!

CS251 Intermediate Programming

Lecture 22

Data Structures

  • Quiz today.

Comparator To create a secondary ordering, you create an extra class that is a subclass of

:

{ public class StudentGPAComparator implements Comparator { public int compare(Student s1, Student s2)

return (int)Math.signum(gpa1 - gpa2);double gpa2 = s2.getGPA();double gpa1 = s1.getGPA();

}

Math.signum()}

(^) returns

(^) -1.

(^) for negative numbers,

(^) 1.

(^) for positive

numbers, and

(^) 0.

(^) for zero.

To use the

(^) Comparator

:

public SortedSet sortStudents (Collection students)

{ StudentGPAComparator c =

new StudentGPAComparator();

TreeSet t =

new TreeSet(c);

for (Student s : students)

t.add(s);

return t;

}

Pass the

(^) Comparator

(^) as an argument to

the constructor of the

(^) TreeSet

.

To use the

(^) Comparator

:

public SortedSet sortStudents (Collection students)

{ StudentGPAComparator c =

new StudentGPAComparator();

TreeSet t =

new TreeSet(c);

for (Student s : students)

t.add(s);

return t;

}

Add all the

(^) Student

s from the

Collection

(^) we received as an argument

(we don't care what kind of

(^) Collection

t.addAll(students);These two lines of code can be replaced by:it) to the sorted collection. it is because we're just going to iterate over

Map

s are structures that contain mappings. A mapping is an

mappings makes up aassociation of one object with another object. A collection of

Map

The purpose of amapping. It is called the "value."The other object makes up the bulk of the data stored in theup. It is called the "key."mapping and is the "token" by which the mapping can be lookedOne of the objects in a mapping forms the identity of the

Map

is to allow you to translate quickly from

a key you have to a value that key is "mapped" to.

Map

s are most useful when you want to quickly look up an

object by one of its properties.

If you have a

List

of

Student

s and you want to

fi nd the

Student

with number

415-99-

, you have to examine

every

Student

object in the

List

, access its number

fi eld,

until youcheck it against the value you’re searching for, and continue

fi nd the right one. This is called a linear search.

Accessing aAs a collection gets large, linear searching gets very slow.

List

by index is very fast, but it's not always

practical to use numeric indexes to look up objects.

Indexes don't work at all for non-numeric properties.number, you might get overlaps and lose data.If you used only part of the number instead of the wholeevery possible student number would take 4GB of RAM.Instantiating an array with enough indexes to hold

HashMap

is a concrete implementation of

Map

that uses hash

serves as a kind of "A hash code is a number generated from an object's data andcodes to look up key objects quickly.

fi ngerprint" for the object.

A

HashMap

divides all the stored keys into bins based on their

When looking up a key,elements.hash codes. Each bin contains zero, one, or a small number of

HashMap

calculates the hash code of

withThis greatly reduces the number of objects that must be testedthe key, and uses it to determine which bin to look in.

equals()

, and ensures that

HashMap

's performance

Object To allow any type of object to be stored in a HashMap, thedoes not degrade on large collections.

class declares a

hashCode()

method that returns an

int

A (^) HashMap

(^) might look like this:

Keys are spread across the bins in a striped fashion using the modulo (

% ) or

into isremainder operator. The formula for determining which bin a hash code goes

(^) B = H % C

, where

(^) B

(^) is the bin number,

(^) H

(^) is the hash code, and

C (^) is the

number of bins.

size = 0

Bin Bin Bin Bin

A (^) put()

into a

(^) HashMap

works like this:

genre = Tragedyname = Othello date = 1603

put(

,

);

genre = Tragedyname = Othello date = 1603

key

value

entry

←

bin number

←

bin count

hashCode()

size = 0

Bin Bin Bin Bin

nullnullnullnull