Advanced Programming Lab 4 Collections and Streams A Collection is - - PowerPoint PPT Presentation

advanced programming lab 4
SMART_READER_LITE
LIVE PREVIEW

Advanced Programming Lab 4 Collections and Streams A Collection is - - PowerPoint PPT Presentation

Advanced Programming Lab 4 Collections and Streams A Collection is a group of individual objects (elements) represented as a single unit. A Stream is a sequence of elements supporting sequential and parallel aggregate operations.


slide-1
SLIDE 1

Advanced Programming Lab 4

slide-2
SLIDE 2

Collections and Streams

  • A Collection is a group of individual objects (elements)

represented as a single unit.

  • A Stream is a sequence of elements supporting

sequential and parallel aggregate operations.

slide-3
SLIDE 3

Resident and Hospital Classes

// Create the class Resident // In the Main class, create Resident objects // The ugly way Resident r0 = new Resident("R0"); Resident r1 = new Resident("R1"); Resident r2 = new Resident("R2"); Resident r3 = new Resident("R3"); // The simple way var r = IntStream.rangeClosed(0, 3) .mapToObj(i -> new Resident("R" + i) ) .toArray(Resident[]::new); // r[0], r[1], r[2], r[3] are the residents

slide-4
SLIDE 4

Creating Lists and Sets

List<Resident> residentList = new ArrayList<>(); // add elements, one by one residentList.add(r0); residentList.add(r1); //... // a better way (if you have them in an array) for (Resident res : r) { residentList.add(res); } // or simpler residentList.addAll( Arrays.asList(r) );

slide-5
SLIDE 5

Sorting

// Natural order given by implementing Comparable // It sorts the given argument Collections.sort(residentList); // Using lambdas Collections.sort(residentList, ((r1, r2) -> r1.getName().compareTo(r2.getName()))); //Using functions Collections.sort(residentList, Comparator.comparing(Resident::getName)); //Using streams (creates a new list!) List<Resident> newSortedList = residentList.stream() .sorted(Comparator.comparing(Resident::getName)) .collect(Collectors.toList());

slide-6
SLIDE 6

Creating a Map

Map<Resident, List<Hospital>> resPrefMap = new HashMap<>(); // One by one List<Hospital> prefList = new ArrayList<>(); prefList.add(h[0]); prefList.add(h[1]); prefList.add(h[2]); resPrefMap.put(r[0], prefList); ... // or simpler resPrefMap.put(r[0], Arrays.asList(h[0], h[1], h[2])); //...

slide-7
SLIDE 7

“Querying” - Filtering

// printing the residents who accept H0 residentList.stream() .filter(res -> resPrefMap.get(res).contains(h[0])) .forEach(System.out::println); // collecting the residents who accept H0 and H2 List<Hospital> target = Arrays.asList(h[0], h[2]); List<Resident> result = residentList.stream() .filter(res -> resPrefMap.get(res).containsAll(target)) .collect(Collectors.toList<>);

slide-8
SLIDE 8

The Algorithms

  • Optional: “First come, first served”

In some order, assign residents to hospitals as long as the capacity of the hospitals allows.

  • Bonus: Deferred Late Acceptance – Gale Shapley
  • an unassigned resident makes a “proposal” to the

best current hospital on its list;

  • if the hospital is not full, accept it; otherwise check to

see if this resident is better than the bottom of the accepted list – if it is, reject the bottom one and accept this one;

slide-9
SLIDE 9

Using Third-Party Libraries

  • “Classical” Ant Project

– Download the library .jars – Add them in CLASSPATH

Project → Properties → Libraries

  • Maven Project

– Edit pom.xml

<dependencies> <dependency> <groupId>com.github.javafaker</groupId> <artifactId>javafaker</artifactId> <version>1.0.2</version> </dependency> </dependencies>

slide-10
SLIDE 10

pom.xml (Project Object Model)

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.mycompany.app</groupId> <artifactId>my-app</artifactId> <version>1.0-SNAPSHOT</version> <properties> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> </dependencies> </project>

groupId uniquely identifies your project across all projects. A group ID should follow Java's package name rules. artifactId is the name of the jar without version.