List of Chapters
Chapter 12: Using Collections and Streams (When Arrays Aren't Good Enough)
In this chapter:
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
ArrayList list = new ArrayList<>();
list.add(85);
list.add(19);
list.add(0);
list.add(103);
list.add(13);
int largest = Integer.MIN_VALUE;
for (Integer i : list) {
if (i > largest) {
largest = i;
}
}
System.out.println(largest);
}
}
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
ArrayList words = new ArrayList<>();
words.add("cat");
words.add("dog");
words.add("horse");
words.add("zebra");
Scanner keyboard = new Scanner(System.in);
String newWord = keyboard.next();
int i = 0;
while (i < words.size() && newWord.compareToIgnoreCase(words.get(i)) > 0) {
i++;
}
words.add(i, newWord);
for (String string : words) {
System.out.println(string);
}
keyboard.close();
}
}
import static java.lang.System.out;
import java.util.ArrayList;
import java.util.Scanner;
public class Editor {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
ArrayList lines = new ArrayList<>();
String command = "";
while (!command.equals("q")) {
out.println();
for (int i = 0; i < lines.size(); i++) {
out.println(i + ": " + lines.get(i));
}
out.println();
int lineNumber = 0;
out.print("Command (i #, r #, d #, q): ");
command = keyboard.next();
if (!command.equals("q")) {
lineNumber = keyboard.nextInt();
keyboard.nextLine();
System.out.println(command + " " + lineNumber);
switch (command) {
case "i":
out.print("Type the new line: ");
while (lineNumber > lines.size()) {
lines.add("");
}
lines.add(lineNumber, keyboard.nextLine());
break;
case "r":
out.print("Type the new line: ");
lines.set(lineNumber, keyboard.nextLine());
break;
case "d":
lines.remove(lineNumber);
}
}
}
keyboard.close();
}
}
import java.text.NumberFormat;
import java.util.ArrayList;
public class Bonuses {
public static void main(String[] args) {
ArrayList employees = new ArrayList<>();
NumberFormat currency = NumberFormat.getCurrencyInstance();
fillTheList(employees);
double total = employees.stream()
.filter((employee) -> employee.getScore() >= 3)
.map((employee) -> 100.00)
.reduce(0.0, (bonus1, bonus2) -> bonus1 + bonus2);
System.out.println(currency.format(total));
}
static void fillTheList(ArrayList employees) {
employees.add(new Employee("Barry", 2));
employees.add(new Employee("Harriet", 3));
employees.add(new Employee("Jennie", 5));
employees.add(new Employee("Sam", 5));
}
}
public enum Ingredient {
HAM, BEEF, PEAS, CARROTS, APPLES
}
import java.util.ArrayList;
public class Recipe {
private String name;
private ArrayList ingredients;
private int time;
public Recipe(String name, ArrayList ingredients, int time) {
this.name = name;
this.ingredients = ingredients;
this.time = time;
}
public boolean isVegetarian() {
return !ingredients.contains(Ingredient.HAM) &&
!ingredients.contains(Ingredient.BEEF);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public ArrayList getIngredients() {
return ingredients;
}
public void setIngredients(ArrayList ingredients) {
this.ingredients = ingredients;
}
public int getTime() {
return time;
}
public void setTime(int time) {
this.time = time;
}
}
import java.util.ArrayList;
public class Recipes {
public static void main(String[] args) {
ArrayList recipes = new ArrayList<>();
fillTheList(recipes);
int total = recipes.stream()
.filter((recipe) -> recipe.isVegetarian())
.map((recipe) -> recipe.getTime())
.reduce(0, (time1, time2) -> time1 + time2);
int count = recipes.stream()
.filter((recipe) -> recipe.isVegetarian())
.map((recipe) -> 1)
.reduce(0, (value1, value2) -> value1 + value2);
System.out.println(total / (double) count);
}
static void fillTheList(ArrayList recipes) {
ArrayList ingredients = new ArrayList<>();
ingredients.add(Ingredient.BEEF);
ingredients.add(Ingredient.HAM);
ingredients.add(Ingredient.APPLES);
recipes.add(new Recipe("Recipe 1", ingredients, 100));
ingredients = new ArrayList<>();
ingredients.add(Ingredient.PEAS);
ingredients.add(Ingredient.CARROTS);
recipes.add(new Recipe("Recipe 2", ingredients, 200));
ingredients = new ArrayList<>();
ingredients.add(Ingredient.PEAS);
ingredients.add(Ingredient.APPLES);
recipes.add(new Recipe("Recipe 3", ingredients, 300));
}
}