CS计算机代考程序代写 data structure compiler Java flex SWEN20003 Object Oriented Software Development

SWEN20003 Object Oriented Software Development

SWEN20003
Object Oriented Software Development

Collections and Maps

Collections and Maps SWEN20003 1 / 25

Lecture Objectives

After this lecture you will be able to:

Choose appropriate data structures storing, retrieving and
manipulating objects (data)

Use the Java Collections Framework

Use the Java Maps Framework

Collections and Maps SWEN20003 2 / 25

Collections and Maps

Understanding how to store data (a collection of objects) for later retrieval
and manipulation is an essential when writing programs.

Java provides two frameworks to support this.

Keyword

Collections: A framework that permits storing, accessing and manipulating
lists (an ordered collection).

Keyword

Maps: A framework that permits storing, accessing and manipulating
key-value pairs.

Collections and Maps SWEN20003 3 / 25

Back to ArrayList

Last lecture we looked at the ArrayList as a generic class.

ArrayList is a class in the Java Collections framework that can be used
for storing, retrieving and manipulating a group of objects.

In this lecture we will take a closer look at how we can use the ArrayList
class for more sophisticated data manipulations.

Collections and Maps SWEN20003 4 / 25

Using the ArrayList Class for storing

import java.util.ArrayList;

public class PrintCircleRadius {

public static void main(String[] args) {

ArrayList circles = new ArrayList();

circles.add(new Circle(0.0, 0.0, 5));

circles.add(new Circle(0.0, 0.0, 10));

circles.add(new Circle(0.0, 0.0, 7));

printRadius(circles);

}

private static void printRadius(ArrayList circles){

int index = 0;

for(Circle c: circles) {

System.out.println(“Radius at index ” + index +

” = ” + c.getRadius());

index++;

}

}

}

Collections and Maps SWEN20003 5 / 25

Using the ArrayList Class for sorting

Elements of an ArrayList can be easily sorted if:

The stored element class implements the Comaparable interface!

The compareTo() method of the class must provide a comparison
(returning an integer) which will be used to decide how the elements are
sorted.

Collections and Maps SWEN20003 6 / 25

Using the ArrayList Class for sorting

import java.util.*;

class Movie implements Comparable

{

private double rating;

private String name;

private int year;

public Movie(String name, double rating, int year)

{

this.name = name;

this.rating = rating;

this.year = year;

}

public int compareTo(Movie m)

{

return this.year – m.year;

}

// Getters and setters go here – not shown

}

Collections and Maps SWEN20003 7 / 25

Using the ArrayList Class for sorting

import java.util.ArrayList;

import java.util.Collections;

public class MovieSorter {

public static void main(String[] args) {

ArrayList list = new ArrayList();

list.add(new Movie(“Force Awakens”, 8.3, 2015));

list.add(new Movie(“Star Wars”, 8.7, 1977));

list.add(new Movie(“Empire Strikes Back”, 8.8, 1980));

list.add(new Movie(“Return of the Jedi”, 8.4, 1983));

Collections.sort(list);

printList(list);

}

public static void printList(ArrayList list) {

for (Movie movie: list)

System.out.println(movie.getRating() + ” ” +

movie.getName() + ” ” + movie.getYear());

}

}

Collections and Maps SWEN20003 8 / 25

Using the ArrayList Class for sorting

What would the program print?

8.7 Star Wars 1977

8.8 Empire Strikes Back 1980

8.4 Return of the Jedi 1983

8.3 Force Awakens 2015

Now, what if we want to sort the movies by rating or name – not year?

How can we do that?

Good news is java Comparator and Collections.sort() can still help
you!

Collections and Maps SWEN20003 9 / 25

Using the ArrayList Class for sorting

import java.util.Comparator;

class RatingComparator implements Comparator

{

public int compare(Movie m1, Movie m2)

{

if (m1.getRating() < m2.getRating()) return -1; if (m1.getRating() > m2.getRating()) return 1;

else return 0;

}

}

import java.util.Comparator;

public class NameComparator implements Comparator {

public int compare(Movie m1, Movie m2) {

return m1.getName().compareTo(m2.getName());

}

}

Collections and Maps SWEN20003 10 / 25

Using the ArrayList Class for sorting

// import statements

public class MovieSorter {

public static void main(String[] args) {

// Code to add movies to the arraylist – same as pervious example

Collections.sort(list);

printList(list);

System.out.println(“*****************************”);

Collections.sort(list,new RatingComparator());

printList(list);

System.out.println(“****************************”);

Collections.sort(list,new NameComparator());

printList(list);

}

public static void printList(ArrayList list) {

for (Movie movie: list)

System.out.println(movie.getRating() + ” ” +

movie.getName() + ” ” + movie.getYear());

}

}

Collections and Maps SWEN20003 11 / 25

Using the ArrayList Class for sorting

What would the program print?

8.7 Star Wars 1977

8.8 Empire Strikes Back 1980

8.4 Return of the Jedi 1983

8.3 Force Awakens 2015

************************************

8.3 Force Awakens 2015

8.4 Return of the Jedi 1983

8.7 Star Wars 1977

8.8 Empire Strikes Back 1980

************************************

8.8 Empire Strikes Back 1980

8.3 Force Awakens 2015

8.4 Return of the Jedi 1983

8.7 Star Wars 1977

Collections and Maps SWEN20003 12 / 25

Using the ArrayList Class for sorting

In the previous example, we developed new comparator class for each
comparison.

Was it necessary? Is that a bit of an overkill?

Is there a different solution?

Anonymous Inner Class is the solution.

Keyword

Anonymous Inner Class: A class created “on the fly”, without a new file,
or class name for which only a single object is created.

Collections and Maps SWEN20003 13 / 25

Using the ArrayList Class for sorting

public class MovieSorterAnnonymous {

public static void main(String[] args) {

// Same code as the previous example

Collections.sort(list, new Comparator(){

@Override

public int compare(Movie m1, Movie m2){

if (m1.getRating() < m2.getRating()) return -1; if (m1.getRating() > m2.getRating()) return 1;

else return 0;

}});

printList(list);

Collections.sort(list, new Comparator(){

@Override

public int compare(Movie m1, Movie m2) {

return m1.getName().compareTo(m2.getName());

}});

printList(list);

}

}

Collections and Maps SWEN20003 14 / 25

Collections Hierarchy

Source: https://dzone.com/articles/java-collections

Collections and Maps SWEN20003 15 / 25

Common Operations – Collections

Length int size()

Presence boolean contains(Object element)
Only works when element defines
equals(Object element)

Add boolean add(E element)

Remove boolean remove(Object element)

Iterating Iterator iterator()

Iterating for (T t : Collection)

Retrieval Object get(int index)
Supported only at AbstractList level and below.

Collections and Maps SWEN20003 16 / 25

Maps

Keyword

Maps: A framework that permits storing, accessing and manipulating
key-value pairs.

Collections and Maps SWEN20003 17 / 25

Maps Hierarchy

Source: https://en.wikipedia.org/wiki/Java collections framework [Note: Not UML]

Collections and Maps SWEN20003 18 / 25

Common Operations – Maps

Length int size()

Presence boolean containKey(Object key)

Presence boolean containValue(Object value)

Add/Replace boolean put(K key, V value)

Remove boolean remove(Object key)

Iterating Set keySet()

Iterating Set> entrySet()

Retrieval V get(Object key)

Collections and Maps SWEN20003 19 / 25

Using HashMap

A generic class that takes two types: K (the key) and V (the value)

import java.util.HashMap;

public static void main(String[] args) {

HashMap library = new HashMap<>();

Book b1 = new Book(“J.R.R. Tolkien”, “The Lord of the Rings”, 1178);

Book b2 = new Book(“George R. R. Martin”, “A Game of Thrones”, 694);

library.put(b1.author, b1);

library.put(b2.author, b2);

for(String author : library.keySet()) {

Book b = library.get(author);

System.out.format(“%s, %s, %d\n”, b.getAuthor(),

b.getTitle(), b.getNumPages());

}

}

Collections and Maps SWEN20003 20 / 25

Assess Yourself

If you were to create a digital phonebook using a HashMap, what would
the key and value types be?

HashMap phonebook = new HashMap<>();

Collections and Maps SWEN20003 21 / 25

Assess Yourself

If you were to create a system to link a pet’s ID to it’s owner, what would
the key and value types be?

HashMap petTracker = new HashMap<>();

Collections and Maps SWEN20003 22 / 25

Assess Yourself

Write a class called Tracker, which accepts two type parameters. The
first type must be subclass of Person, and the second type a subclass of
Locator.

A Person object could be a Hiker, Diver, or Pilot.

A Locator object could be GPS, Infrared, or IP.

The Tracker class maintains a list of TwoTypePair objects, with the
elements of the TwoTypePair being a Person and a Locator.

Collections and Maps SWEN20003 23 / 25

Generics in the Collections and Maps

If we didn’t have generic classes, how would you implement a list, a map,
etc.?

Rewrite your code for any type you might use it with

Generics give us flexibility; code once, reuse the code for any type. They
also allow objects to keep their type (i.e. not be Objects), and, allows
the compiler to detect errors, thereby prevent run-time errors if code is
properly designed.

Collections and Maps SWEN20003 24 / 25

Lecture Objectives

After this lecture you will be able to:

Choose appropriate data structures storing, retrieving and
manipulating objects (data)

Use Java Collections Framework

Use Java Maps Framework

Collections and Maps SWEN20003 25 / 25