After rewriting Type Information Chapter, added missing @Override

This commit is contained in:
Bruce Eckel 2021-01-31 15:42:31 -07:00
parent dfdbcf604f
commit 55de56e524
1034 changed files with 102363 additions and 2776 deletions

5
CI.txt
View File

@ -1,5 +0,0 @@
Links formerly in the README. Both of these mysteriously started breaking.
[![](https://travis-ci.org/BruceEckel/OnJava8-Examples.svg?branch=master)](https://travis-ci.org/BruceEckel/OnJava8-Examples)
[![](https://ci.appveyor.com/api/projects/status/github/BruceEckel/OnJava8-Examples)](https://ci.appveyor.com/project/BruceEckel/onjava-examples)

View File

@ -1,5 +1,5 @@
// Copyright.txt // Copyright.txt
This computer source code is Copyright ©2020 MindView LLC. This computer source code is Copyright ©2021 MindView LLC.
All Rights Reserved. All Rights Reserved.
Permission to use, copy, modify, and distribute this Permission to use, copy, modify, and distribute this

View File

@ -1,5 +1,5 @@
// annotations/AUComposition.java // annotations/AUComposition.java
// (c)2020 MindView LLC: see Copyright.txt // (c)2021 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose. // We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information. // Visit http://OnJava8.com for more book information.
// Creating non-embedded tests // Creating non-embedded tests
@ -23,8 +23,8 @@ public class AUComposition {
} }
/* Output: /* Output:
annotations.AUComposition annotations.AUComposition
. tMethodOne
. tMethodTwo This is methodTwo . tMethodTwo This is methodTwo
. tMethodOne
OK (2 tests) OK (2 tests)
*/ */

View File

@ -1,5 +1,5 @@
// annotations/AUExternalTest.java // annotations/AUExternalTest.java
// (c)2020 MindView LLC: see Copyright.txt // (c)2021 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose. // We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information. // Visit http://OnJava8.com for more book information.
// Creating non-embedded tests // Creating non-embedded tests

View File

@ -1,5 +1,5 @@
// annotations/AtUnitExample1.java // annotations/AtUnitExample1.java
// (c)2020 MindView LLC: see Copyright.txt // (c)2021 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose. // We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information. // Visit http://OnJava8.com for more book information.
// {java onjava.atunit.AtUnit // {java onjava.atunit.AtUnit
@ -34,15 +34,15 @@ public class AtUnitExample1 {
} }
/* Output: /* Output:
annotations.AtUnitExample1 annotations.AtUnitExample1
. m3 . anotherDisappointment (failed)
. methodOneTest . methodOneTest
. failureTest (failed)
. m2 This is methodTwo . m2 This is methodTwo
. failureTest (failed) . m3
. anotherDisappointment (failed)
(5 tests) (5 tests)
>>> 2 FAILURES <<< >>> 2 FAILURES <<<
annotations.AtUnitExample1: failureTest
annotations.AtUnitExample1: anotherDisappointment annotations.AtUnitExample1: anotherDisappointment
annotations.AtUnitExample1: failureTest
*/ */

View File

@ -1,5 +1,5 @@
// annotations/AtUnitExample2.java // annotations/AtUnitExample2.java
// (c)2020 MindView LLC: see Copyright.txt // (c)2021 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose. // We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information. // Visit http://OnJava8.com for more book information.
// Assertions and exceptions can be used in @Tests // Assertions and exceptions can be used in @Tests
@ -40,18 +40,18 @@ public class AtUnitExample2 {
} }
/* Output: /* Output:
annotations.AtUnitExample2 annotations.AtUnitExample2
. exceptionExample java.io.FileNotFoundException:
nofile.txt (The system cannot find the file specified)
(failed)
. assertExample
. assertAndReturn This is methodTwo
. assertFailureExample java.lang.AssertionError: What . assertFailureExample java.lang.AssertionError: What
a surprise! a surprise!
(failed) (failed)
. assertExample
. exceptionExample java.io.FileNotFoundException:
nofile.txt (The system cannot find the file specified)
(failed)
. assertAndReturn This is methodTwo
(4 tests) (4 tests)
>>> 2 FAILURES <<< >>> 2 FAILURES <<<
annotations.AtUnitExample2: exceptionExample
annotations.AtUnitExample2: assertFailureExample annotations.AtUnitExample2: assertFailureExample
annotations.AtUnitExample2: exceptionExample
*/ */

View File

@ -1,5 +1,5 @@
// annotations/AtUnitExample3.java // annotations/AtUnitExample3.java
// (c)2020 MindView LLC: see Copyright.txt // (c)2021 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose. // We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information. // Visit http://OnJava8.com for more book information.
// {java onjava.atunit.AtUnit // {java onjava.atunit.AtUnit
@ -35,8 +35,8 @@ public class AtUnitExample3 {
/* Output: /* Output:
annotations.AtUnitExample3 annotations.AtUnitExample3
. initialization . initialization
. methodOneTest
. m2 This is methodTwo . m2 This is methodTwo
. methodOneTest
OK (3 tests) OK (3 tests)
*/ */

View File

@ -1,5 +1,5 @@
// annotations/AtUnitExample4.java // annotations/AtUnitExample4.java
// (c)2020 MindView LLC: see Copyright.txt // (c)2021 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose. // We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information. // Visit http://OnJava8.com for more book information.
// {java onjava.atunit.AtUnit // {java onjava.atunit.AtUnit

View File

@ -1,5 +1,5 @@
// annotations/AtUnitExample5.java // annotations/AtUnitExample5.java
// (c)2020 MindView LLC: see Copyright.txt // (c)2021 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose. // We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information. // Visit http://OnJava8.com for more book information.
// {java onjava.atunit.AtUnit // {java onjava.atunit.AtUnit
@ -14,8 +14,7 @@ public class AtUnitExample5 {
public AtUnitExample5(String text) { public AtUnitExample5(String text) {
this.text = text; this.text = text;
} }
@Override @Override public String toString() { return text; }
public String toString() { return text; }
@TestProperty @TestProperty
static PrintWriter output; static PrintWriter output;
@TestProperty @TestProperty

View File

@ -1,5 +1,5 @@
// annotations/DemoProcessFiles.java // annotations/DemoProcessFiles.java
// (c)2020 MindView LLC: see Copyright.txt // (c)2021 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose. // We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information. // Visit http://OnJava8.com for more book information.
import onjava.ProcessFiles; import onjava.ProcessFiles;

View File

@ -1,5 +1,5 @@
// annotations/HashSetTest.java // annotations/HashSetTest.java
// (c)2020 MindView LLC: see Copyright.txt // (c)2021 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose. // We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information. // Visit http://OnJava8.com for more book information.
// {java onjava.atunit.AtUnit // {java onjava.atunit.AtUnit
@ -29,8 +29,8 @@ public class HashSetTest {
} }
/* Output: /* Output:
annotations.HashSetTest annotations.HashSetTest
. tContains
. initialization . initialization
. tRemove . tRemove
. tContains
OK (3 tests) OK (3 tests)
*/ */

View File

@ -1,5 +1,5 @@
// annotations/PasswordUtils.java // annotations/PasswordUtils.java
// (c)2020 MindView LLC: see Copyright.txt // (c)2021 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose. // We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information. // Visit http://OnJava8.com for more book information.
import java.util.*; import java.util.*;

View File

@ -1,5 +1,5 @@
// annotations/SimulatingNull.java // annotations/SimulatingNull.java
// (c)2020 MindView LLC: see Copyright.txt // (c)2021 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose. // We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information. // Visit http://OnJava8.com for more book information.
import java.lang.annotation.*; import java.lang.annotation.*;

View File

@ -1,5 +1,5 @@
// annotations/StackL.java // annotations/StackL.java
// (c)2020 MindView LLC: see Copyright.txt // (c)2021 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose. // We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information. // Visit http://OnJava8.com for more book information.
// A stack built on a LinkedList // A stack built on a LinkedList

View File

@ -1,5 +1,5 @@
// annotations/StackLStringTst.java // annotations/StackLStringTst.java
// (c)2020 MindView LLC: see Copyright.txt // (c)2021 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose. // We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information. // Visit http://OnJava8.com for more book information.
// Applying @Unit to generics // Applying @Unit to generics
@ -35,8 +35,8 @@ StackLStringTst extends StackL<String> {
} }
/* Output: /* Output:
annotations.StackLStringTst annotations.StackLStringTst
. tPop
. tTop . tTop
. tPush . tPush
. tPop
OK (3 tests) OK (3 tests)
*/ */

View File

@ -1,5 +1,5 @@
// annotations/Testable.java // annotations/Testable.java
// (c)2020 MindView LLC: see Copyright.txt // (c)2021 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose. // We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information. // Visit http://OnJava8.com for more book information.
package annotations; package annotations;

View File

@ -1,5 +1,5 @@
// annotations/UseCase.java // annotations/UseCase.java
// (c)2020 MindView LLC: see Copyright.txt // (c)2021 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose. // We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information. // Visit http://OnJava8.com for more book information.
import java.lang.annotation.*; import java.lang.annotation.*;

View File

@ -1,5 +1,5 @@
// annotations/UseCaseTracker.java // annotations/UseCaseTracker.java
// (c)2020 MindView LLC: see Copyright.txt // (c)2021 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose. // We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information. // Visit http://OnJava8.com for more book information.
import java.util.*; import java.util.*;
@ -27,11 +27,11 @@ public class UseCaseTracker {
} }
} }
/* Output: /* Output:
Found Use Case 49
New passwords can't equal previously used ones
Found Use Case 48 Found Use Case 48
no description no description
Found Use Case 47 Found Use Case 47
Passwords must contain at least one numeric Passwords must contain at least one numeric
Found Use Case 49
New passwords can't equal previously used ones
Missing use case 50 Missing use case 50
*/ */

View File

@ -1,5 +1,5 @@
// annotations/database/Constraints.java // annotations/database/Constraints.java
// (c)2020 MindView LLC: see Copyright.txt // (c)2021 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose. // We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information. // Visit http://OnJava8.com for more book information.
package annotations.database; package annotations.database;

View File

@ -1,5 +1,5 @@
// annotations/database/DBTable.java // annotations/database/DBTable.java
// (c)2020 MindView LLC: see Copyright.txt // (c)2021 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose. // We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information. // Visit http://OnJava8.com for more book information.
package annotations.database; package annotations.database;

View File

@ -1,5 +1,5 @@
// annotations/database/Member.java // annotations/database/Member.java
// (c)2020 MindView LLC: see Copyright.txt // (c)2021 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose. // We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information. // Visit http://OnJava8.com for more book information.
package annotations.database; package annotations.database;
@ -16,7 +16,8 @@ public class Member {
public String getReference() { return reference; } public String getReference() { return reference; }
public String getFirstName() { return firstName; } public String getFirstName() { return firstName; }
public String getLastName() { return lastName; } public String getLastName() { return lastName; }
@Override @Override public String toString() {
public String toString() { return reference; } return reference;
}
public Integer getAge() { return age; } public Integer getAge() { return age; }
} }

View File

@ -1,5 +1,5 @@
// annotations/database/SQLInteger.java // annotations/database/SQLInteger.java
// (c)2020 MindView LLC: see Copyright.txt // (c)2021 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose. // We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information. // Visit http://OnJava8.com for more book information.
package annotations.database; package annotations.database;

View File

@ -1,5 +1,5 @@
// annotations/database/SQLString.java // annotations/database/SQLString.java
// (c)2020 MindView LLC: see Copyright.txt // (c)2021 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose. // We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information. // Visit http://OnJava8.com for more book information.
package annotations.database; package annotations.database;

View File

@ -1,5 +1,5 @@
// annotations/database/TableCreator.java // annotations/database/TableCreator.java
// (c)2020 MindView LLC: see Copyright.txt // (c)2021 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose. // We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information. // Visit http://OnJava8.com for more book information.
// Reflection-based annotation processor // Reflection-based annotation processor

View File

@ -1,5 +1,5 @@
// annotations/database/Uniqueness.java // annotations/database/Uniqueness.java
// (c)2020 MindView LLC: see Copyright.txt // (c)2021 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose. // We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information. // Visit http://OnJava8.com for more book information.
// Sample of nested annotations // Sample of nested annotations

View File

@ -1,5 +1,5 @@
// annotations/ifx/ExtractInterface.java // annotations/ifx/ExtractInterface.java
// (c)2020 MindView LLC: see Copyright.txt // (c)2021 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose. // We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information. // Visit http://OnJava8.com for more book information.
// javac-based annotation processing // javac-based annotation processing

View File

@ -1,5 +1,5 @@
// annotations/ifx/IfaceExtractorProcessor.java // annotations/ifx/IfaceExtractorProcessor.java
// (c)2020 MindView LLC: see Copyright.txt // (c)2021 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose. // We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information. // Visit http://OnJava8.com for more book information.
// javac-based annotation processing // javac-based annotation processing
@ -21,14 +21,12 @@ extends AbstractProcessor {
interfaceMethods = new ArrayList<>(); interfaceMethods = new ArrayList<>();
Elements elementUtils; Elements elementUtils;
private ProcessingEnvironment processingEnv; private ProcessingEnvironment processingEnv;
@Override @Override public void init(
public void init(
ProcessingEnvironment processingEnv) { ProcessingEnvironment processingEnv) {
this.processingEnv = processingEnv; this.processingEnv = processingEnv;
elementUtils = processingEnv.getElementUtils(); elementUtils = processingEnv.getElementUtils();
} }
@Override @Override public boolean process(
public boolean process(
Set<? extends TypeElement> annotations, Set<? extends TypeElement> annotations,
RoundEnvironment env) { RoundEnvironment env) {
for(Element elem:env.getElementsAnnotatedWith( for(Element elem:env.getElementsAnnotatedWith(

View File

@ -1,5 +1,5 @@
// annotations/ifx/Multiplier.java // annotations/ifx/Multiplier.java
// (c)2020 MindView LLC: see Copyright.txt // (c)2021 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose. // We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information. // Visit http://OnJava8.com for more book information.
// javac-based annotation processing // javac-based annotation processing

View File

@ -1,5 +1,5 @@
// annotations/simplest/Simple.java // annotations/simplest/Simple.java
// (c)2020 MindView LLC: see Copyright.txt // (c)2021 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose. // We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information. // Visit http://OnJava8.com for more book information.
// A bare-bones annotation // A bare-bones annotation

View File

@ -1,5 +1,5 @@
// annotations/simplest/SimpleProcessor.java // annotations/simplest/SimpleProcessor.java
// (c)2020 MindView LLC: see Copyright.txt // (c)2021 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose. // We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information. // Visit http://OnJava8.com for more book information.
// A bare-bones annotation processor // A bare-bones annotation processor
@ -14,8 +14,7 @@ import java.util.*;
@SupportedSourceVersion(SourceVersion.RELEASE_8) @SupportedSourceVersion(SourceVersion.RELEASE_8)
public class SimpleProcessor public class SimpleProcessor
extends AbstractProcessor { extends AbstractProcessor {
@Override @Override public boolean process(
public boolean process(
Set<? extends TypeElement> annotations, Set<? extends TypeElement> annotations,
RoundEnvironment env) { RoundEnvironment env) {
for(TypeElement t : annotations) for(TypeElement t : annotations)

View File

@ -1,5 +1,5 @@
// annotations/simplest/SimpleTest.java // annotations/simplest/SimpleTest.java
// (c)2020 MindView LLC: see Copyright.txt // (c)2021 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose. // We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information. // Visit http://OnJava8.com for more book information.
// Test the "Simple" annotation // Test the "Simple" annotation

View File

@ -1,5 +1,5 @@
// arrays/AlphabeticSearch.java // arrays/AlphabeticSearch.java
// (c)2020 MindView LLC: see Copyright.txt // (c)2021 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose. // We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information. // Visit http://OnJava8.com for more book information.
// Searching with a Comparator import // Searching with a Comparator import

View File

@ -1,5 +1,5 @@
// arrays/ArrayCopying.java // arrays/ArrayCopying.java
// (c)2020 MindView LLC: see Copyright.txt // (c)2021 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose. // We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information. // Visit http://OnJava8.com for more book information.
// Demonstrate Arrays.copy() and Arrays.copyOf() // Demonstrate Arrays.copy() and Arrays.copyOf()
@ -10,8 +10,7 @@ import static onjava.ArrayShow.*;
class Sup { // Superclass class Sup { // Superclass
private int id; private int id;
Sup(int n) { id = n; } Sup(int n) { id = n; }
@Override @Override public String toString() {
public String toString() {
return getClass().getSimpleName() + id; return getClass().getSimpleName() + id;
} }
} }
@ -26,21 +25,21 @@ public class ArrayCopying {
int[] a1 = new int[SZ]; int[] a1 = new int[SZ];
Arrays.setAll(a1, new Count.Integer()::get); Arrays.setAll(a1, new Count.Integer()::get);
show("a1", a1); show("a1", a1);
int[] a2 = Arrays.copyOf(a1, a1.length); // [1] int[] a2 = Arrays.copyOf(a1, a1.length); // [1]
// Prove they are distinct arrays: // Prove they are distinct arrays:
Arrays.fill(a1, 1); Arrays.fill(a1, 1);
show("a1", a1); show("a1", a1);
show("a2", a2); show("a2", a2);
// Create a shorter result: // Create a shorter result:
a2 = Arrays.copyOf(a2, a2.length/2); // [2] a2 = Arrays.copyOf(a2, a2.length/2); // [2]
show("a2", a2); show("a2", a2);
// Allocate more space: // Allocate more space:
a2 = Arrays.copyOf(a2, a2.length + 5); a2 = Arrays.copyOf(a2, a2.length + 5);
show("a2", a2); show("a2", a2);
// Also copies wrapped arrays: // Also copies wrapped arrays:
Integer[] a3 = new Integer[SZ]; // [3] Integer[] a3 = new Integer[SZ]; // [3]
Arrays.setAll(a3, new Count.Integer()::get); Arrays.setAll(a3, new Count.Integer()::get);
Integer[] a4 = Arrays.copyOfRange(a3, 4, 12); Integer[] a4 = Arrays.copyOfRange(a3, 4, 12);
show("a4", a4); show("a4", a4);
@ -62,7 +61,7 @@ public class ArrayCopying {
Arrays.setAll(b2, Sup::new); Arrays.setAll(b2, Sup::new);
try { try {
Sub[] d3 = Arrays.copyOf( Sub[] d3 = Arrays.copyOf(
b2, b2.length, Sub[].class); // [6] b2, b2.length, Sub[].class); // [6]
} catch(Exception e) { } catch(Exception e) {
System.out.println(e); System.out.println(e);
} }

View File

@ -1,5 +1,5 @@
// arrays/ArrayOfGenericType.java // arrays/ArrayOfGenericType.java
// (c)2020 MindView LLC: see Copyright.txt // (c)2021 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose. // We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information. // Visit http://OnJava8.com for more book information.

View File

@ -1,5 +1,5 @@
// arrays/ArrayOfGenerics.java // arrays/ArrayOfGenerics.java
// (c)2020 MindView LLC: see Copyright.txt // (c)2021 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose. // We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information. // Visit http://OnJava8.com for more book information.
import java.util.*; import java.util.*;

View File

@ -1,5 +1,5 @@
// arrays/ArrayOptions.java // arrays/ArrayOptions.java
// (c)2020 MindView LLC: see Copyright.txt // (c)2021 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose. // We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information. // Visit http://OnJava8.com for more book information.
// Initialization & re-assignment of arrays // Initialization & re-assignment of arrays

View File

@ -1,5 +1,5 @@
// arrays/ArraySearching.java // arrays/ArraySearching.java
// (c)2020 MindView LLC: see Copyright.txt // (c)2021 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose. // We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information. // Visit http://OnJava8.com for more book information.
// Using Arrays.binarySearch() // Using Arrays.binarySearch()

View File

@ -1,5 +1,5 @@
// arrays/AssemblingMultidimensionalArrays.java // arrays/AssemblingMultidimensionalArrays.java
// (c)2020 MindView LLC: see Copyright.txt // (c)2021 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose. // We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information. // Visit http://OnJava8.com for more book information.
// Creating multidimensional arrays // Creating multidimensional arrays

View File

@ -1,5 +1,5 @@
// arrays/AutoboxingArrays.java // arrays/AutoboxingArrays.java
// (c)2020 MindView LLC: see Copyright.txt // (c)2021 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose. // We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information. // Visit http://OnJava8.com for more book information.
import java.util.*; import java.util.*;

View File

@ -1,5 +1,5 @@
// arrays/CollectionComparison.java // arrays/CollectionComparison.java
// (c)2020 MindView LLC: see Copyright.txt // (c)2021 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose. // We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information. // Visit http://OnJava8.com for more book information.
import java.util.*; import java.util.*;
@ -9,8 +9,7 @@ import static onjava.ArrayShow.*;
class BerylliumSphere { class BerylliumSphere {
private static long counter; private static long counter;
private final long id = counter++; private final long id = counter++;
@Override @Override public String toString() {
public String toString() {
return "Sphere " + id; return "Sphere " + id;
} }
} }

View File

@ -1,5 +1,5 @@
// arrays/CompType.java // arrays/CompType.java
// (c)2020 MindView LLC: see Copyright.txt // (c)2021 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose. // We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information. // Visit http://OnJava8.com for more book information.
// Implementing Comparable in a class // Implementing Comparable in a class
@ -16,15 +16,13 @@ public class CompType implements Comparable<CompType> {
i = n1; i = n1;
j = n2; j = n2;
} }
@Override @Override public String toString() {
public String toString() {
String result = "[i = " + i + ", j = " + j + "]"; String result = "[i = " + i + ", j = " + j + "]";
if(count++ % 3 == 0) if(count++ % 3 == 0)
result += "\n"; result += "\n";
return result; return result;
} }
@Override @Override public int compareTo(CompType rv) {
public int compareTo(CompType rv) {
return (i < rv.i ? -1 : (i == rv.i ? 0 : 1)); return (i < rv.i ? -1 : (i == rv.i ? 0 : 1));
} }
private static SplittableRandom r = private static SplittableRandom r =

View File

@ -1,5 +1,5 @@
// arrays/ComparatorTest.java // arrays/ComparatorTest.java
// (c)2020 MindView LLC: see Copyright.txt // (c)2021 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose. // We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information. // Visit http://OnJava8.com for more book information.
// Implementing a Comparator for a class // Implementing a Comparator for a class

View File

@ -1,5 +1,5 @@
// arrays/ComparingArrays.java // arrays/ComparingArrays.java
// (c)2020 MindView LLC: see Copyright.txt // (c)2021 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose. // We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information. // Visit http://OnJava8.com for more book information.
// Using Arrays.equals() // Using Arrays.equals()

View File

@ -1,5 +1,5 @@
// arrays/CountUpward.java // arrays/CountUpward.java
// (c)2020 MindView LLC: see Copyright.txt // (c)2021 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose. // We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information. // Visit http://OnJava8.com for more book information.
import java.util.*; import java.util.*;

View File

@ -1,5 +1,5 @@
// arrays/FillingArrays.java // arrays/FillingArrays.java
// (c)2020 MindView LLC: see Copyright.txt // (c)2021 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose. // We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information. // Visit http://OnJava8.com for more book information.
// Using Arrays.fill() // Using Arrays.fill()

View File

@ -1,5 +1,5 @@
// arrays/IceCreamFlavors.java // arrays/IceCreamFlavors.java
// (c)2020 MindView LLC: see Copyright.txt // (c)2021 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose. // We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information. // Visit http://OnJava8.com for more book information.
// Returning arrays from methods // Returning arrays from methods

View File

@ -1,5 +1,5 @@
// arrays/ModifyExisting.java // arrays/ModifyExisting.java
// (c)2020 MindView LLC: see Copyright.txt // (c)2021 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose. // We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information. // Visit http://OnJava8.com for more book information.
import java.util.*; import java.util.*;
@ -11,7 +11,7 @@ public class ModifyExisting {
double[] da = new double[7]; double[] da = new double[7];
Arrays.setAll(da, new Rand.Double()::get); Arrays.setAll(da, new Rand.Double()::get);
show(da); show(da);
Arrays.setAll(da, n -> da[n] / 100); // [1] Arrays.setAll(da, n -> da[n] / 100); // [1]
show(da); show(da);
} }
} }

View File

@ -1,5 +1,5 @@
// arrays/MultiDimWrapperArray.java // arrays/MultiDimWrapperArray.java
// (c)2020 MindView LLC: see Copyright.txt // (c)2021 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose. // We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information. // Visit http://OnJava8.com for more book information.
// Multidimensional arrays of "wrapper" objects // Multidimensional arrays of "wrapper" objects

View File

@ -1,5 +1,5 @@
// arrays/MultidimensionalObjectArrays.java // arrays/MultidimensionalObjectArrays.java
// (c)2020 MindView LLC: see Copyright.txt // (c)2021 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose. // We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information. // Visit http://OnJava8.com for more book information.
import java.util.*; import java.util.*;

View File

@ -1,5 +1,5 @@
// arrays/MultidimensionalPrimitiveArray.java // arrays/MultidimensionalPrimitiveArray.java
// (c)2020 MindView LLC: see Copyright.txt // (c)2021 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose. // We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information. // Visit http://OnJava8.com for more book information.
import java.util.*; import java.util.*;

View File

@ -1,5 +1,5 @@
// arrays/ParallelPrefix1.java // arrays/ParallelPrefix1.java
// (c)2020 MindView LLC: see Copyright.txt // (c)2021 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose. // We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information. // Visit http://OnJava8.com for more book information.
import java.util.*; import java.util.*;

View File

@ -1,5 +1,5 @@
// arrays/ParallelPrefix2.java // arrays/ParallelPrefix2.java
// (c)2020 MindView LLC: see Copyright.txt // (c)2021 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose. // We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information. // Visit http://OnJava8.com for more book information.
import java.util.*; import java.util.*;

View File

@ -1,5 +1,5 @@
// arrays/ParallelPrefix3.java // arrays/ParallelPrefix3.java
// (c)2020 MindView LLC: see Copyright.txt // (c)2021 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose. // We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information. // Visit http://OnJava8.com for more book information.
// {ExcludeFromTravisCI} // {ExcludeFromTravisCI}

View File

@ -1,5 +1,5 @@
// arrays/ParallelSetAll.java // arrays/ParallelSetAll.java
// (c)2020 MindView LLC: see Copyright.txt // (c)2021 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose. // We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information. // Visit http://OnJava8.com for more book information.
import java.util.*; import java.util.*;

View File

@ -1,5 +1,5 @@
// arrays/ParameterizedArrayType.java // arrays/ParameterizedArrayType.java
// (c)2020 MindView LLC: see Copyright.txt // (c)2021 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose. // We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information. // Visit http://OnJava8.com for more book information.

View File

@ -1,5 +1,5 @@
# arrays/PythonLists.py # arrays/PythonLists.py
# (c)2020 MindView LLC: see Copyright.txt # (c)2021 MindView LLC: see Copyright.txt
# We make no guarantees that this code is fit for any purpose. # We make no guarantees that this code is fit for any purpose.
# Visit http://OnJava8.com for more book information. # Visit http://OnJava8.com for more book information.
@ -20,7 +20,7 @@ class MyList(list): # Inherit from list
reversed.reverse() # Built-in list method reversed.reverse() # Built-in list method
return reversed return reversed
# No 'new' necessary for object creation: # No 'new' necessary for object creation: {#24-no-new-necessary-for-object-creation}
list2 = MyList(aList) list2 = MyList(aList)
print(type(list2)) # <class '__main__.MyList'> print(type(list2)) # <class '__main__.MyList'>
print(list2.getReversed()) # [8, 7, 6, 5, 4, 3, 2, 1] print(list2.getReversed()) # [8, 7, 6, 5, 4, 3, 2, 1]

View File

@ -1,5 +1,5 @@
// arrays/RaggedArray.java // arrays/RaggedArray.java
// (c)2020 MindView LLC: see Copyright.txt // (c)2021 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose. // We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information. // Visit http://OnJava8.com for more book information.
import java.util.*; import java.util.*;
@ -14,7 +14,7 @@ public class RaggedArray {
a[i] = new int[rand.nextInt(5)][]; a[i] = new int[rand.nextInt(5)][];
for(int j = 0; j < a[i].length; j++) { for(int j = 0; j < a[i].length; j++) {
a[i][j] = new int[rand.nextInt(5)]; a[i][j] = new int[rand.nextInt(5)];
Arrays.setAll(a[i][j], n -> val++); // [1] Arrays.setAll(a[i][j], n -> val++); // [1]
} }
} }
System.out.println(Arrays.deepToString(a)); System.out.println(Arrays.deepToString(a));

View File

@ -1,5 +1,5 @@
// arrays/Reverse.java // arrays/Reverse.java
// (c)2020 MindView LLC: see Copyright.txt // (c)2021 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose. // We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information. // Visit http://OnJava8.com for more book information.
// The Collections.reverseOrder() Comparator // The Collections.reverseOrder() Comparator

View File

@ -1,5 +1,5 @@
// arrays/SimpleSetAll.java // arrays/SimpleSetAll.java
// (c)2020 MindView LLC: see Copyright.txt // (c)2021 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose. // We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information. // Visit http://OnJava8.com for more book information.
import java.util.*; import java.util.*;
@ -8,8 +8,9 @@ import static onjava.ArrayShow.*;
class Bob { class Bob {
final int id; final int id;
Bob(int n) { id = n; } Bob(int n) { id = n; }
@Override @Override public String toString() {
public String toString() { return "Bob" + id; } return "Bob" + id;
}
} }
public class SimpleSetAll { public class SimpleSetAll {
@ -22,13 +23,13 @@ public class SimpleSetAll {
int[] ia = new int[SZ]; int[] ia = new int[SZ];
long[] la = new long[SZ]; long[] la = new long[SZ];
double[] da = new double[SZ]; double[] da = new double[SZ];
Arrays.setAll(ia, n -> n); // [1] Arrays.setAll(ia, n -> n); // [1]
Arrays.setAll(la, n -> n); Arrays.setAll(la, n -> n);
Arrays.setAll(da, n -> n); Arrays.setAll(da, n -> n);
show(ia); show(ia);
show(la); show(la);
show(da); show(da);
Arrays.setAll(ia, n -> val++); // [2] Arrays.setAll(ia, n -> val++); // [2]
Arrays.setAll(la, n -> val++); Arrays.setAll(la, n -> val++);
Arrays.setAll(da, n -> val++); Arrays.setAll(da, n -> val++);
show(ia); show(ia);
@ -36,11 +37,11 @@ public class SimpleSetAll {
show(da); show(da);
Bob[] ba = new Bob[SZ]; Bob[] ba = new Bob[SZ];
Arrays.setAll(ba, Bob::new); // [3] Arrays.setAll(ba, Bob::new); // [3]
show(ba); show(ba);
Character[] ca = new Character[SZ]; Character[] ca = new Character[SZ];
Arrays.setAll(ca, SimpleSetAll::getChar); // [4] Arrays.setAll(ca, SimpleSetAll::getChar); // [4]
show(ca); show(ca);
} }
} }

View File

@ -1,5 +1,5 @@
// arrays/StreamFromArray.java // arrays/StreamFromArray.java
// (c)2020 MindView LLC: see Copyright.txt // (c)2021 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose. // We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information. // Visit http://OnJava8.com for more book information.
import java.util.*; import java.util.*;

View File

@ -1,5 +1,5 @@
// arrays/StringSorting.java // arrays/StringSorting.java
// (c)2020 MindView LLC: see Copyright.txt // (c)2021 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose. // We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information. // Visit http://OnJava8.com for more book information.
// Sorting an array of Strings // Sorting an array of Strings

View File

@ -1,5 +1,5 @@
// arrays/TestConvertTo.java // arrays/TestConvertTo.java
// (c)2020 MindView LLC: see Copyright.txt // (c)2021 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose. // We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information. // Visit http://OnJava8.com for more book information.
import java.util.*; import java.util.*;

View File

@ -1,5 +1,5 @@
// arrays/TestCount.java // arrays/TestCount.java
// (c)2020 MindView LLC: see Copyright.txt // (c)2021 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose. // We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information. // Visit http://OnJava8.com for more book information.
// Test counting generators // Test counting generators

View File

@ -1,5 +1,5 @@
// arrays/TestRand.java // arrays/TestRand.java
// (c)2020 MindView LLC: see Copyright.txt // (c)2021 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose. // We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information. // Visit http://OnJava8.com for more book information.
// Test random generators // Test random generators

View File

@ -1,5 +1,5 @@
// arrays/ThreeDWithNew.java // arrays/ThreeDWithNew.java
// (c)2020 MindView LLC: see Copyright.txt // (c)2021 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose. // We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information. // Visit http://OnJava8.com for more book information.
import java.util.*; import java.util.*;

View File

@ -1,5 +1,5 @@
// arrays/jmh/ParallelSort.java // arrays/jmh/ParallelSort.java
// (c)2020 MindView LLC: see Copyright.txt // (c)2021 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose. // We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information. // Visit http://OnJava8.com for more book information.
package arrays.jmh; package arrays.jmh;

View File

@ -1,5 +1,5 @@
// collections/AdapterMethodIdiom.java // collections/AdapterMethodIdiom.java
// (c)2020 MindView LLC: see Copyright.txt // (c)2021 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose. // We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information. // Visit http://OnJava8.com for more book information.
// The "Adapter Method" idiom uses for-in // The "Adapter Method" idiom uses for-in
@ -15,10 +15,12 @@ class ReversibleArrayList<T> extends ArrayList<T> {
public Iterator<T> iterator() { public Iterator<T> iterator() {
return new Iterator<T>() { return new Iterator<T>() {
int current = size() - 1; int current = size() - 1;
public boolean hasNext() { @Override public boolean hasNext() {
return current > -1; return current > -1;
} }
@Override
public T next() { return get(current--); } public T next() { return get(current--); }
@Override
public void remove() { // Not implemented public void remove() { // Not implemented
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
} }

View File

@ -1,5 +1,5 @@
// collections/AddingGroups.java // collections/AddingGroups.java
// (c)2020 MindView LLC: see Copyright.txt // (c)2021 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose. // We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information. // Visit http://OnJava8.com for more book information.
// Adding groups of elements to Collection objects // Adding groups of elements to Collection objects

View File

@ -1,5 +1,5 @@
// collections/ApplesAndOrangesWithGenerics.java // collections/ApplesAndOrangesWithGenerics.java
// (c)2020 MindView LLC: see Copyright.txt // (c)2021 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose. // We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information. // Visit http://OnJava8.com for more book information.
import java.util.*; import java.util.*;

View File

@ -1,5 +1,5 @@
// collections/ApplesAndOrangesWithoutGenerics.java // collections/ApplesAndOrangesWithoutGenerics.java
// (c)2020 MindView LLC: see Copyright.txt // (c)2021 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose. // We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information. // Visit http://OnJava8.com for more book information.
// Simple collection use (suppressing compiler warnings) // Simple collection use (suppressing compiler warnings)

View File

@ -1,5 +1,5 @@
// collections/ArrayIsNotIterable.java // collections/ArrayIsNotIterable.java
// (c)2020 MindView LLC: see Copyright.txt // (c)2021 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose. // We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information. // Visit http://OnJava8.com for more book information.
import java.util.*; import java.util.*;

View File

@ -1,5 +1,5 @@
// collections/AsListInference.java // collections/AsListInference.java
// (c)2020 MindView LLC: see Copyright.txt // (c)2021 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose. // We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information. // Visit http://OnJava8.com for more book information.
import java.util.*; import java.util.*;

View File

@ -1,5 +1,5 @@
// collections/CollectionDifferences.java // collections/CollectionDifferences.java
// (c)2020 MindView LLC: see Copyright.txt // (c)2021 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose. // We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information. // Visit http://OnJava8.com for more book information.
import onjava.*; import onjava.*;

View File

@ -1,5 +1,5 @@
// collections/CollectionSequence.java // collections/CollectionSequence.java
// (c)2020 MindView LLC: see Copyright.txt // (c)2021 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose. // We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information. // Visit http://OnJava8.com for more book information.
import typeinfo.pets.*; import typeinfo.pets.*;
@ -7,15 +7,13 @@ import java.util.*;
public class CollectionSequence public class CollectionSequence
extends AbstractCollection<Pet> { extends AbstractCollection<Pet> {
private Pet[] pets = Pets.array(8); private Pet[] pets = new PetCreator().array(8);
@Override @Override
public int size() { return pets.length; } public int size() { return pets.length; }
@Override @Override public Iterator<Pet> iterator() {
public Iterator<Pet> iterator() { return new Iterator<Pet>() { // [1]
return new Iterator<Pet>() { // [1]
private int index = 0; private int index = 0;
@Override @Override public boolean hasNext() {
public boolean hasNext() {
return index < pets.length; return index < pets.length;
} }
@Override @Override

View File

@ -1,5 +1,5 @@
// collections/CrossCollectionIteration.java // collections/CrossCollectionIteration.java
// (c)2020 MindView LLC: see Copyright.txt // (c)2021 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose. // We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information. // Visit http://OnJava8.com for more book information.
import typeinfo.pets.*; import typeinfo.pets.*;
@ -14,7 +14,7 @@ public class CrossCollectionIteration {
System.out.println(); System.out.println();
} }
public static void main(String[] args) { public static void main(String[] args) {
List<Pet> pets = Pets.list(8); List<Pet> pets = new PetCreator().list(8);
LinkedList<Pet> petsLL = new LinkedList<>(pets); LinkedList<Pet> petsLL = new LinkedList<>(pets);
HashSet<Pet> petsHS = new HashSet<>(pets); HashSet<Pet> petsHS = new HashSet<>(pets);
TreeSet<Pet> petsTS = new TreeSet<>(pets); TreeSet<Pet> petsTS = new TreeSet<>(pets);

View File

@ -1,5 +1,5 @@
// collections/CrossCollectionIteration2.java // collections/CrossCollectionIteration2.java
// (c)2020 MindView LLC: see Copyright.txt // (c)2021 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose. // We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information. // Visit http://OnJava8.com for more book information.
import typeinfo.pets.*; import typeinfo.pets.*;
@ -15,7 +15,7 @@ public class CrossCollectionIteration2 {
System.out.println(); System.out.println();
} }
public static void main(String[] args) { public static void main(String[] args) {
List<Pet> pets = Pets.list(8); List<Pet> pets = new PetCreator().list(8);
LinkedList<Pet> petsLL = new LinkedList<>(pets); LinkedList<Pet> petsLL = new LinkedList<>(pets);
HashSet<Pet> petsHS = new HashSet<>(pets); HashSet<Pet> petsHS = new HashSet<>(pets);
TreeSet<Pet> petsTS = new TreeSet<>(pets); TreeSet<Pet> petsTS = new TreeSet<>(pets);

View File

@ -1,5 +1,5 @@
// collections/EnvironmentVariables.java // collections/EnvironmentVariables.java
// (c)2020 MindView LLC: see Copyright.txt // (c)2021 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose. // We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information. // Visit http://OnJava8.com for more book information.
// {VisuallyInspectOutput} // {VisuallyInspectOutput}

View File

@ -1,5 +1,5 @@
// collections/ForInCollections.java // collections/ForInCollections.java
// (c)2020 MindView LLC: see Copyright.txt // (c)2021 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose. // We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information. // Visit http://OnJava8.com for more book information.
// All collections work with for-in // All collections work with for-in

View File

@ -1,5 +1,5 @@
// collections/GenericsAndUpcasting.java // collections/GenericsAndUpcasting.java
// (c)2020 MindView LLC: see Copyright.txt // (c)2021 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose. // We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information. // Visit http://OnJava8.com for more book information.
import java.util.*; import java.util.*;
@ -21,8 +21,8 @@ public class GenericsAndUpcasting {
} }
} }
/* Output: /* Output:
GrannySmith@15db9742 GrannySmith@19e0bfd
Gala@6d06d69c Gala@139a55
Fuji@7852e922 Fuji@1db9742
Braeburn@4e25154f Braeburn@106d69c
*/ */

View File

@ -1,5 +1,5 @@
// collections/InterfaceVsIterator.java // collections/InterfaceVsIterator.java
// (c)2020 MindView LLC: see Copyright.txt // (c)2021 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose. // We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information. // Visit http://OnJava8.com for more book information.
import typeinfo.pets.*; import typeinfo.pets.*;
@ -19,7 +19,7 @@ public class InterfaceVsIterator {
System.out.println(); System.out.println();
} }
public static void main(String[] args) { public static void main(String[] args) {
List<Pet> petList = Pets.list(8); List<Pet> petList = new PetCreator().list(8);
Set<Pet> petSet = new HashSet<>(petList); Set<Pet> petSet = new HashSet<>(petList);
Map<String, Pet> petMap = new LinkedHashMap<>(); Map<String, Pet> petMap = new LinkedHashMap<>();
String[] names = ("Ralph, Eric, Robin, Lacey, " + String[] names = ("Ralph, Eric, Robin, Lacey, " +

View File

@ -1,5 +1,5 @@
// collections/IterableClass.java // collections/IterableClass.java
// (c)2020 MindView LLC: see Copyright.txt // (c)2021 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose. // We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information. // Visit http://OnJava8.com for more book information.
// Anything Iterable works with for-in // Anything Iterable works with for-in
@ -9,12 +9,10 @@ public class IterableClass implements Iterable<String> {
protected String[] words = ("And that is how " + protected String[] words = ("And that is how " +
"we know the Earth to be banana-shaped." "we know the Earth to be banana-shaped."
).split(" "); ).split(" ");
@Override @Override public Iterator<String> iterator() {
public Iterator<String> iterator() {
return new Iterator<String>() { return new Iterator<String>() {
private int index = 0; private int index = 0;
@Override @Override public boolean hasNext() {
public boolean hasNext() {
return index < words.length; return index < words.length;
} }
@Override @Override

View File

@ -1,5 +1,5 @@
// collections/LinkedListFeatures.java // collections/LinkedListFeatures.java
// (c)2020 MindView LLC: see Copyright.txt // (c)2021 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose. // We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information. // Visit http://OnJava8.com for more book information.
import typeinfo.pets.*; import typeinfo.pets.*;
@ -8,7 +8,7 @@ import java.util.*;
public class LinkedListFeatures { public class LinkedListFeatures {
public static void main(String[] args) { public static void main(String[] args) {
LinkedList<Pet> pets = LinkedList<Pet> pets =
new LinkedList<>(Pets.list(5)); new LinkedList<>(new PetCreator().list(5));
System.out.println(pets); System.out.println(pets);
// Identical: // Identical:
System.out.println( System.out.println(
@ -27,9 +27,9 @@ public class LinkedListFeatures {
System.out.println(pets); System.out.println(pets);
pets.addFirst(new Rat()); pets.addFirst(new Rat());
System.out.println("After addFirst(): " + pets); System.out.println("After addFirst(): " + pets);
pets.offer(Pets.get()); pets.offer(new PetCreator().get());
System.out.println("After offer(): " + pets); System.out.println("After offer(): " + pets);
pets.add(Pets.get()); pets.add(new PetCreator().get());
System.out.println("After add(): " + pets); System.out.println("After add(): " + pets);
pets.addLast(new Hamster()); pets.addLast(new Hamster());
System.out.println("After addLast(): " + pets); System.out.println("After addLast(): " + pets);
@ -47,8 +47,8 @@ pets.removeFirst(): Manx
pets.poll(): Cymric pets.poll(): Cymric
[Mutt, Pug] [Mutt, Pug]
After addFirst(): [Rat, Mutt, Pug] After addFirst(): [Rat, Mutt, Pug]
After offer(): [Rat, Mutt, Pug, Cymric] After offer(): [Rat, Mutt, Pug, Rat]
After add(): [Rat, Mutt, Pug, Cymric, Pug] After add(): [Rat, Mutt, Pug, Rat, Rat]
After addLast(): [Rat, Mutt, Pug, Cymric, Pug, Hamster] After addLast(): [Rat, Mutt, Pug, Rat, Rat, Hamster]
pets.removeLast(): Hamster pets.removeLast(): Hamster
*/ */

View File

@ -1,5 +1,5 @@
// collections/ListFeatures.java // collections/ListFeatures.java
// (c)2020 MindView LLC: see Copyright.txt // (c)2021 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose. // We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information. // Visit http://OnJava8.com for more book information.
import typeinfo.pets.*; import typeinfo.pets.*;
@ -8,7 +8,7 @@ import java.util.*;
public class ListFeatures { public class ListFeatures {
public static void main(String[] args) { public static void main(String[] args) {
Random rand = new Random(47); Random rand = new Random(47);
List<Pet> pets = Pets.list(7); List<Pet> pets = new PetCreator().list(7);
System.out.println("1: " + pets); System.out.println("1: " + pets);
Hamster h = new Hamster(); Hamster h = new Hamster();
pets.add(h); // Automatically resizes pets.add(h); // Automatically resizes
@ -54,7 +54,7 @@ public class ListFeatures {
pets.clear(); // Remove all elements pets.clear(); // Remove all elements
System.out.println("19: " + pets); System.out.println("19: " + pets);
System.out.println("20: " + pets.isEmpty()); System.out.println("20: " + pets.isEmpty());
pets.addAll(Pets.list(4)); pets.addAll(new PetCreator().list(4));
System.out.println("21: " + pets); System.out.println("21: " + pets);
Object[] o = pets.toArray(); Object[] o = pets.toArray();
System.out.println("22: " + o[3]); System.out.println("22: " + o[3]);
@ -87,7 +87,7 @@ sub: [Mouse, Pug]
18: false 18: false
19: [] 19: []
20: true 20: true
21: [Manx, Cymric, Rat, EgyptianMau] 21: [Rat, Manx, Cymric, Mutt]
22: EgyptianMau 22: Mutt
23: 14 23: 14
*/ */

View File

@ -1,5 +1,5 @@
// collections/ListIteration.java // collections/ListIteration.java
// (c)2020 MindView LLC: see Copyright.txt // (c)2021 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose. // We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information. // Visit http://OnJava8.com for more book information.
import typeinfo.pets.*; import typeinfo.pets.*;
@ -7,7 +7,7 @@ import java.util.*;
public class ListIteration { public class ListIteration {
public static void main(String[] args) { public static void main(String[] args) {
List<Pet> pets = Pets.list(8); List<Pet> pets = new PetCreator().list(8);
ListIterator<Pet> it = pets.listIterator(); ListIterator<Pet> it = pets.listIterator();
while(it.hasNext()) while(it.hasNext())
System.out.print(it.next() + System.out.print(it.next() +
@ -22,7 +22,7 @@ public class ListIteration {
it = pets.listIterator(3); it = pets.listIterator(3);
while(it.hasNext()) { while(it.hasNext()) {
it.next(); it.next();
it.set(Pets.get()); it.set(new PetCreator().get());
} }
System.out.println(pets); System.out.println(pets);
} }
@ -32,6 +32,5 @@ Rat, 1, 0; Manx, 2, 1; Cymric, 3, 2; Mutt, 4, 3; Pug,
5, 4; Cymric, 6, 5; Pug, 7, 6; Manx, 8, 7; 5, 4; Cymric, 6, 5; Pug, 7, 6; Manx, 8, 7;
7 6 5 4 3 2 1 0 7 6 5 4 3 2 1 0
[Rat, Manx, Cymric, Mutt, Pug, Cymric, Pug, Manx] [Rat, Manx, Cymric, Mutt, Pug, Cymric, Pug, Manx]
[Rat, Manx, Cymric, Cymric, Rat, EgyptianMau, Hamster, [Rat, Manx, Cymric, Rat, Rat, Rat, Rat, Rat]
EgyptianMau]
*/ */

View File

@ -1,5 +1,5 @@
// collections/MapOfList.java // collections/MapOfList.java
// (c)2020 MindView LLC: see Copyright.txt // (c)2021 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose. // We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information. // Visit http://OnJava8.com for more book information.
// {java collections.MapOfList} // {java collections.MapOfList}

View File

@ -1,5 +1,5 @@
// collections/ModifyingArraysAsList.java // collections/ModifyingArraysAsList.java
// (c)2020 MindView LLC: see Copyright.txt // (c)2021 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose. // We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information. // Visit http://OnJava8.com for more book information.
import java.util.*; import java.util.*;

View File

@ -1,5 +1,5 @@
// collections/MultiIterableClass.java // collections/MultiIterableClass.java
// (c)2020 MindView LLC: see Copyright.txt // (c)2021 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose. // We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information. // Visit http://OnJava8.com for more book information.
// Adding several Adapter Methods // Adding several Adapter Methods
@ -11,12 +11,13 @@ public class MultiIterableClass extends IterableClass {
public Iterator<String> iterator() { public Iterator<String> iterator() {
return new Iterator<String>() { return new Iterator<String>() {
int current = words.length - 1; int current = words.length - 1;
public boolean hasNext() { @Override public boolean hasNext() {
return current > -1; return current > -1;
} }
public String next() { @Override public String next() {
return words[current--]; return words[current--];
} }
@Override
public void remove() { // Not implemented public void remove() { // Not implemented
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
} }

View File

@ -1,20 +1,19 @@
// collections/NonCollectionSequence.java // collections/NonCollectionSequence.java
// (c)2020 MindView LLC: see Copyright.txt // (c)2021 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose. // We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information. // Visit http://OnJava8.com for more book information.
import typeinfo.pets.*; import typeinfo.pets.*;
import java.util.*; import java.util.*;
class PetSequence { class PetSequence {
protected Pet[] pets = Pets.array(8); protected Pet[] pets = new PetCreator().array(8);
} }
public class NonCollectionSequence extends PetSequence { public class NonCollectionSequence extends PetSequence {
public Iterator<Pet> iterator() { public Iterator<Pet> iterator() {
return new Iterator<Pet>() { return new Iterator<Pet>() {
private int index = 0; private int index = 0;
@Override @Override public boolean hasNext() {
public boolean hasNext() {
return index < pets.length; return index < pets.length;
} }
@Override @Override

View File

@ -1,5 +1,5 @@
// collections/PetMap.java // collections/PetMap.java
// (c)2020 MindView LLC: see Copyright.txt // (c)2021 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose. // We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information. // Visit http://OnJava8.com for more book information.
import typeinfo.pets.*; import typeinfo.pets.*;

View File

@ -1,5 +1,5 @@
// collections/PrintingCollections.java // collections/PrintingCollections.java
// (c)2020 MindView LLC: see Copyright.txt // (c)2021 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose. // We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information. // Visit http://OnJava8.com for more book information.
// Collections print themselves automatically // Collections print themselves automatically

View File

@ -1,5 +1,5 @@
// collections/PriorityQueueDemo.java // collections/PriorityQueueDemo.java
// (c)2020 MindView LLC: see Copyright.txt // (c)2021 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose. // We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information. // Visit http://OnJava8.com for more book information.
import java.util.*; import java.util.*;

View File

@ -1,5 +1,5 @@
// collections/QueueDemo.java // collections/QueueDemo.java
// (c)2020 MindView LLC: see Copyright.txt // (c)2021 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose. // We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information. // Visit http://OnJava8.com for more book information.
// Upcasting to a Queue from a LinkedList // Upcasting to a Queue from a LinkedList

View File

@ -1,5 +1,5 @@
// collections/SetOfInteger.java // collections/SetOfInteger.java
// (c)2020 MindView LLC: see Copyright.txt // (c)2021 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose. // We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information. // Visit http://OnJava8.com for more book information.
import java.util.*; import java.util.*;

View File

@ -1,5 +1,5 @@
// collections/SetOfString.java // collections/SetOfString.java
// (c)2020 MindView LLC: see Copyright.txt // (c)2021 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose. // We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information. // Visit http://OnJava8.com for more book information.
import java.util.*; import java.util.*;

View File

@ -1,5 +1,5 @@
// collections/SetOperations.java // collections/SetOperations.java
// (c)2020 MindView LLC: see Copyright.txt // (c)2021 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose. // We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information. // Visit http://OnJava8.com for more book information.
import java.util.*; import java.util.*;

View File

@ -1,5 +1,5 @@
// collections/SimpleCollection.java // collections/SimpleCollection.java
// (c)2020 MindView LLC: see Copyright.txt // (c)2021 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose. // We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information. // Visit http://OnJava8.com for more book information.
import java.util.*; import java.util.*;

View File

@ -1,5 +1,5 @@
// collections/SimpleIteration.java // collections/SimpleIteration.java
// (c)2020 MindView LLC: see Copyright.txt // (c)2021 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose. // We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information. // Visit http://OnJava8.com for more book information.
import typeinfo.pets.*; import typeinfo.pets.*;
@ -7,7 +7,7 @@ import java.util.*;
public class SimpleIteration { public class SimpleIteration {
public static void main(String[] args) { public static void main(String[] args) {
List<Pet> pets = Pets.list(12); List<Pet> pets = new PetCreator().list(12);
Iterator<Pet> it = pets.iterator(); Iterator<Pet> it = pets.iterator();
while(it.hasNext()) { while(it.hasNext()) {
Pet p = it.next(); Pet p = it.next();

View File

@ -1,5 +1,5 @@
// collections/SortedSetOfString.java // collections/SortedSetOfString.java
// (c)2020 MindView LLC: see Copyright.txt // (c)2021 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose. // We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information. // Visit http://OnJava8.com for more book information.
import java.util.*; import java.util.*;

View File

@ -1,5 +1,5 @@
// collections/StackCollision.java // collections/StackCollision.java
// (c)2020 MindView LLC: see Copyright.txt // (c)2021 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose. // We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information. // Visit http://OnJava8.com for more book information.

View File

@ -1,5 +1,5 @@
// collections/StackTest.java // collections/StackTest.java
// (c)2020 MindView LLC: see Copyright.txt // (c)2021 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose. // We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information. // Visit http://OnJava8.com for more book information.
import java.util.*; import java.util.*;

Some files were not shown because too many files have changed in this diff Show More