-
public protected package private same class yes yes yes yes class in same package yes yes yes no subclass in different package yes yes no no non-subclass, different package yes no no no
-
for-each loops can be used on a
Collection
or on an arrayfinal private Vector<PhotoListMetaDataListener> a_listOfMetaDataListeners;
…
for (PhotoListMetaDataListener l : a_listOfMetaDataListeners) l.photoListMetaDataChanged(f);final private Vector<PhotoListMetaDataListener> a_listOfMetaDataListeners;
…
for (int i = 0; i < a_listOfMetaDataListeners.size(); i++) a_listOfMetaDataListeners.get(i).photoListMetaDataChanged(f);
-
Autoboxing allows to use an
int
in place of anInteger
and vice-versa.
Supported types areBoolean
,Byte
,Character
,Short
,Integer
,Long
,Float
, andDouble
.
If the program tries to autounboxnull
, it will throw aNullPointerException
.
The==
operator performs reference identity comparisons onInteger
expressions and value equality comparisons onint
expressions.
The performance costs associated with boxing and unboxing must not be forgotten.
The static import construct allows unqualified access to static members without inheriting from the type containing the static members. Instead, the program imports the members, either individually:
import static java.lang.Math.PI;
|
import static java.lang.Math.*; |
double r = cos(PI * theta); |
double r = Math.cos(Math.PI * theta); |
This should be used sparingly since it pollutes the namespace of the class.
Modifiers
-
The Java Language Specification suggests ordering the modifiers as:
- public / protected / private
- abstract
- default
- static
- final
- transient / volatile
- synchronized
- native
- strictfp
-
strictfp
strictfp
is a keyword to force Java to store intermediary results in floating point calculation using IEEE 754 (instead of a possible greater precision provided by the machine).
-
String
: immutable sequence of characters -
StringBuffer
: thread-safe, mutable sequence of characters (Java 1.0) -
StringBuilder
: non thread-safe, mutable sequence of characters, faster thanStringBuffer
(Java 1.5)
-
example
void foo(int... x) {
for (int i = 0; i < x.length; i++) {
System.out.println(x[i]);
}
}
-
Class hierarchy
-
Throwable
-
Error
: indicates serious problems that a reasonable application should not try to catch -
Exception
: conditions that a reasonable application might want to catch-
RuntimeException
: represents exceptions that a program shouldn't generally expect to occur, but could potentially recover from, it is likely to be a programming error rather than an error due to invalid user input or a badly configured environment
-
-
-
to get the stacktrace as a string
private static String getExceptionDescription(final Throwable e) {
final StringWriter sw = new StringWriter();
final PrintWriter pw = new PrintWriter(sw);
e.printStackTrace(pw);
return sw.toString();
}
-
-
Error
s andRuntimeException
s are unchecked exceptions (methods don't explicitly have to declare that they can throw an unchecked exception; callers don't have to handle them explicitly)
-
A method reference can be of four types:
Reference to a static method ContainingClass::staticMethodName
Reference to an instance method of a particular object containingObject::instanceMethodName
Reference to an instance method of an arbitrary object of a particular type ContainingType::methodName
Reference to a constructor ClassName::new
-
\p{…}
can be used to match a character of a given Unicode category.
For example\p{Z}
matches separators, I believe this is the same as Character.isSpaceChar().
-
@FunctionalInterface
is an informative annotation type used to indicate that an interface with exactly one abstract method. -
Supplier<T>
Represents a supplier of results.
T get()
BooleanSupplier
Represents a supplier of Boolean-valued results. DoubleSupplier
Represents a supplier of double-valued results. IntSupplier
Represents a supplier of int-valued results. LongSupplier
Represents a supplier of long-valued results. Predicate<T>
Represents a predicate (Boolean-valued function) of one argument.
boolean test(T t)
DoublePredicate
Represents a predicate with one double-valued argument. IntPredicate
Represents a predicate with one int-valued argument. LongPredicate
Represents a predicate with one long-valued argument. BiPredicate<T,U>
Represents a predicate with two arguments.
boolean test(T t, U u)
Consumer<T>
Represents an operation that accepts a single input argument and returns no result.
void accept(T t)
DoubleConsumer
Represents an operation that accepts a single double-valued argument and returns no result. IntConsumer
Represents an operation that accepts a single int-valued argument and returns no result. LongConsumer
Represents an operation that accepts a single long-valued argument and returns no result. BiConsumer<T,U>
Represents an operation that accepts two input arguments and returns no result.
void accept(T t, U u)
ObjDoubleConsumer<T>
Represents an operation that accepts an object-valued and a double-valued argument, and returns no result. ObjIntConsumer<T>
Represents an operation that accepts an object-valued and an int-valued argument, and returns no result. ObjLongConsumer<T>
Represents an operation that accepts an object-valued and a long-valued argument, and returns no result. Function<T,R>
Represents a function that accepts one argument and produces a result.
R apply(T t)
static <T> Function<T,T> identity()
returns a function that always returns its input argument.DoubleFunction<R>
Represents a function that accepts a double-valued argument and produces a result. DoubleToIntFunction
Represents a function that accepts a double-valued argument and produces an int-valued result. DoubleToLongFunction
Represents a function that accepts a double-valued argument and produces a long-valued result. IntToDoubleFunction
Represents a function that accepts an int-valued argument and produces a double-valued result. IntFunction<R>
Represents a function that accepts an int-valued argument and produces a result. IntToLongFunction
Represents a function that accepts an int-valued argument and produces a long-valued result. LongFunction<R>
Represents a function that accepts a long-valued argument and produces a result. LongToDoubleFunction
Represents a function that accepts a long-valued argument and produces a double-valued result. LongToIntFunction
Represents a function that accepts a long-valued argument and produces an int-valued result. BiFunction<T,U,R>
Represents a function that accepts two arguments and produces a result.
R apply(T t, U u)
ToDoubleFunction<T>
Represents a function that produces a double-valued result. ToIntFunction<T>
Represents a function that produces an int-valued result. ToLongFunction<T>
Represents a function that produces a long-valued result. ToDoubleBiFunction<T,U>
Represents a function that accepts two arguments and produces a double-valued result. ToIntBiFunction<T,U>
Represents a function that accepts two arguments and produces an int-valued result. ToLongBiFunction<T,U>
Represents a function that accepts two arguments and produces a long-valued result. UnaryOperator<T>
Represents an operation on a single operand that produces a result of the same type as its operand.
T apply(T t)
DoubleUnaryOperator
Represents an operation on a single double-valued operand that produces a double-valued result. IntUnaryOperator
Represents an operation on a single int-valued operand that produces an int-valued result. LongUnaryOperator
Represents an operation on a single long-valued operand that produces a long-valued result. BinaryOperator<T>
Represents an operation upon two operands of the same type, producing a result of the same type as the operands.
T apply(T t, T u)
DoubleBinaryOperator
Represents an operation upon two double-valued operands and producing a double-valued result. IntBinaryOperator
Represents an operation upon two int-valued operands and producing an int-valued result. LongBinaryOperator
Represents an operation upon two long-valued operands and producing a long-valued result.
static <T> Optional<T> of(T value) | Returns an Optional describing the given non-null value. |
|
static <T> Optional<T> ofNullable(T value) | Returns an Optional describing the given value, if non-null, otherwise returns an empty Optional . |
|
static <T> Optional<T> empty() | Returns an empty Optional instance. |
|
boolean isEmpty() | If a value is not present, returns true, otherwise false. | |
boolean isPresent() | If a value is present, returns true, otherwise false. | |
T get() | If a value is present, returns the value, otherwise throws NoSuchElementException . |
|
T orElse(T other) | If a value is present, returns the value, otherwise returns other. | |
T orElseGet(Supplier<? extends T> supplier) | If a value is present, returns the value, otherwise returns the result produced by the supplying function. | |
Optional<T> or(Supplier<? extends Optional<? extends T>> supplier) | If a value is present, returns an Optional describing the value, otherwise returns an Optional produced by the supplying function. | |
T orElseThrow() | If a value is present, returns the value, otherwise throws NoSuchElementException . |
|
<X extends Throwable> T orElseThrow(Supplier<? extends X> exceptionSupplier) | If a value is present, returns the value, otherwise throws an exception produced by the exception supplying function. | |
void ifPresentOrElse(Consumer<? super T> action, Runnable emptyAction) | If a value is present, performs the given action with the value, otherwise performs the given empty-based action.
|
|
Optional<T> filter(Predicate<? super T> predicate) | If a value is present, and the value matches the given predicate, returns an Optional describing the value, otherwise returns an empty Optional . |
|
<U> Optional<U> map(Function<? super T,? extends U> mapper) | If a value is present, returns an Optional describing (as if by ofNullable(T) ) the result of applying the given mapping function to the value, otherwise returns an empty Optional . |
|
<U> Optional<U> flatMap(Function<? super T,? extends Optional<? extends U>> mapper) | If a value is present, returns the result of applying the given Optional -bearing mapping function to the value, otherwise returns an empty Optional . |
-
OptionalDouble
: A container object which may or may not contain a double value. -
OptionalInt
: A container object which may or may not contain an int value. -
OptionalLong
: A container object which may or may not contain a long value.
static <T> Stream<T> concat(Stream<? extends T> a, Stream<? extends T> b) | Creates a lazily concatenated stream whose elements are all the elements of the first stream followed by all the elements of the second stream. | |
boolean allMatch(Predicate<? super T> predicate) | Returns whether all elements of this stream match the provided predicate. | |
boolean anyMatch(Predicate<? super T> predicate) | Returns whether any elements of this stream match the provided predicate. | |
<R> Stream<R> map(Function<? super T,? extends R> mapper) | Returns a stream consisting of the results of applying the given function to the elements of this stream. | |
<R> Stream<R> flatMap(Function<? super T,? extends Stream<? extends R>> mapper) | Returns a stream consisting of the results of replacing each element of this stream with the contents of a mapped stream produced by applying the provided mapping function to each element.
|
|
default <R> Stream<R> mapMulti(BiConsumer<? super T,? super Consumer<R>> mapper) | Returns a stream consisting of the results of replacing each element of this stream with multiple elements, specifically zero or more elements.
|
|
Optional<T> reduce(BinaryOperator<T> accumulator) | Performs a reduction on the elements and returns an Optional describing the reduced value. If there is no value, an empty Optional is returned. |
|
T reduce(T identity, BinaryOperator<T> accumulator) | Performs a reduction on the elements of this stream, using the provided identity value and an associative accumulation function, and returns the reduced value. | |
<U> U reduce(U identity, BiFunction<U,? super T,U> accumulator, BinaryOperator<U> combiner) | Performs a reduction on the elements of this stream, using the provided identity, accumulation and combining functions.
The combiner is used to combine two partial reductions from two different threads; it must respect combiner.apply(u, accumulator.apply(identity, t)) == accumulator.apply(u, t) . |
|
stringStream.collect( Collectors.joining( "," ) ) | Create a comma-separated list from a stream of strings. | |
Stream<T> peek(Consumer<? super T> action) | Return a stream consisting of the elements of this stream, additionally performing the provided action on each element as elements are consumed from the resulting stream. |
-
to create a test suite as a list of test classes:
package data.internet.test;
import org.junit.platform.runner.JUnitPlatform;
import org.junit.platform.suite.api.SelectClasses;
import org.junit.runner.RunWith;
@RunWith(JUnitPlatform.class)
@SelectClasses({SynchronousSiteDataRetrieverTest.class,
AsynchronousSiteDataRetrieverTest.class,
CachedSiteDataRetrieverTest.class})
public class AllSiteRetrieverTests {
} -
to create a test suite as a list of test packages:
package test;
import org.junit.platform.runner.JUnitPlatform;
import org.junit.platform.suite.api.SelectPackages;
import org.junit.runner.RunWith;
@RunWith(JUnitPlatform.class)
@SelectPackages({"data.internet.test",
"data.jsongenerator.test",
"data.test"})
public class AllTests {
}
Test suites do not exist in JUnit 5, see here.