Java Collection related Flashcards
What is the diamond problem? How to solve it?
The diamond problem happens when a class implements two or more other interfaces and the interfaces have 1. same abstract method names (with different return types). So the class will not know which to override.
public interface Phone { void showName(); } public interface Application { String showName(); }
public class GalaxyPhone implements Phone, Application{ // conflict }
or
- interfaces have the same default methods. So the class will not know which to override. The class has to override the duplicated default method
public interface Phone { default void showName() {} } public interface Application { default void showName(){} }
public class GalaxyPhone implements Phone, Application{ @override void showName(){} }
Can an interface private method call the default method? How About the default calling the private method?
Yes, they can call each other. they can also call the static method.
Does Map implement the collection interface?
No, Map is a collection but it doesn’t implement the Collection interface.
What does the List.of() static method does?
Creates a List.of the provided parameters. Can be passed to the ArrayList() constructor.
What are the two types of streams in Java and how do they differ?
Intermediate and terminal.
- intermediate operations return a stream as a result
1.1 they are lazily loaded. When you call intermediate operations, they are actually not executed. They are just stored in the memory and executed when the terminal operation is called on the stream.
map(), filter(), distinct(), sorted(), limit(), skip() - terminal operations return non-stream values like primitive or object or collection or may not return anything.
forEach(), toArray(), reduce(), collect(), min(), max(), count(), anyMatch(), allMatch(), noneMatch(), findFirst(), findAny()