Copying objects and processing data in pipelines.
Key points, notes and past questions
Cloning and Copy Constructors
Shallow and deep copies, seen as arrows between objects.
- A second name or a second object
b = amakes no object.super.clone()makes one new object and copies each field's value or arrow.- No constructor runs during
super.clone(). - A clone holds the values that existed at the moment of the call.
- What a shallow clone shares
super.clone()copies references, never the objects behind them.- Writing into a shared object is seen from both sides.
- Assigning a field changes one object only.
array.clone()andnew ArrayList<>(list)are shallow.
- Cutting the shared arrows: deep cloning
- Deep clone =
super.clone()plus a new object for each mutable field. - A skipped field stays shared.
- Copying a list or a 2-D array does not copy its elements or rows.
- A
finalreference field cannot be replaced aftersuper.clone(). - Guard
nullfields before callingclone()on them.
- Deep clone =
- Who may be cloned: the contract and inheritance
Cloneablehas no methods; it is tested while the program runs.Object.cloneisprotectedand declares a checked exception.- The clone has the runtime class of the original.
- A subclass inherits
Cloneableand the parent'sclone. - A child's new mutable field is shared unless the child's code copies it.
- Copy constructors
- Java writes no copy constructor for you.
- Each assignment decides: value, shared arrow or new object.
new Parent(child)builds aParent.- A constructor may fill
finalfields;clone()cannot reassign them.
Notes9 past questions in this topic
Streams and Functional Interfaces
Lazy pipelines built from lambdas.
- Lambdas and functional interfaces
- A lambda needs a functional interface as its target.
- Exactly one abstract method;
defaultandstaticmethods do not count. - An expression body returns its value; a block body needs
return. - Writing a lambda runs nothing.
- Pipelines and laziness
- Middle steps record a plan; the terminal operation runs it.
- Elements travel one at a time through the whole chain.
skipandlimitcount what reaches them.- A stream can be consumed only once.
countreturns along.
- filter versus map
filterkeeps or drops; the element stays the same.mapreplaces; the count stays the same.- Two filters in a row mean both tests (
&&). - After
map, only the new value exists in the flow.
- iterate and size control
- Two-argument
iteratehas no end. - Three-argument
iteratestops at the first value that fails its condition. limitbeforefilterbounds candidates; after, it bounds results.filternever stops a stream.intoverflow wraps to negative values.
- Two-argument
- takeWhile, dropWhile and filter
filtertests every element.takeWhilestops at the first failure and drops it.dropWhilestarts keeping at the first failure and never tests again.- If the first element fails,
takeWhilegives nothing anddropWhilegives everything.
- Equivalent pipelines
- Compare values, order, layout and whether the code ends.
limit, a source condition andtakeWhilecan all cut an endless series.- A method reference behaves like the lambda that calls the same method.
- Check boundaries:
<against<=.
Notes17 past questions in this topic
Week 7: Exceptions, assertions, packages, loggingAll Java topics and weeks