Professional Documents
Culture Documents
My Java Learnings
My Java Learnings
1. Java Collections
I. Comparator and comparable:
Comparable and Comparator both are interfaces and can be used
to sort collection elements.
If any class implements Comparable interface in Java then collection
of that object either List or Array can be sorted automatically by
using Collections.sort() or Arrays.sort() method and objects will be
sorted based on their natural order defined by CompareTo method.
Comparable Comparator
3) Affects the original class, i.e., the Comparator doesn't affect the original class,
actual class is modified. i.e., the actual class is not modified.
5) java.lang package. java.util package.
6) Sort the list elements of Comparable Sort the list elements of Comparator type
type by Collections.sort(List) method. by Collections.sort(List,Comparator) method.
Although it's much more concise than other solutions, it can be a victim of
integer overflows in Java
Stream<String> stream
= Stream.of("Geeks", "For",
"Geeks", "A",
"Computer",
"Portal");
IntStream
LongStream
DoubleStream
.collect(Collectors.toList());*/
To make the above collecting process work, we must box the stream items first.
.boxed()
.collect(Collectors.toList());
Polymorphism:
1. Overriding:
Run-time (or dynamic) polymorphism
A subclass (or derived class) provides a specific implementation
of a method in superclass (or base class) at runtime.
Overloading:
Compile-time (or static) polymorphism
Allows different methods to have same name, but different
signatures, especially number of input parameters and type of
input parameters.