Java-201 Flashcards
What is a type-safe?
It preserves a method arg to assign illegal values
Are enums case senstives?
Yes
How can we get the reference of enums?
Volume level = Volume.LOW
Volume level = Volume.valueOf(“LOW”);
How many ways we can create threads
Two ways
By implementing the runnable in interface
And by extension to the athread class
Does the time of execution of thread is guaranteed
Nope
How many executors are defined by concurrency package
Executor
Executor service
Scheduled executor service
Does - is allowed in identifiers?
Nope
What is the regex pattern for space
\s+ (one or many)
What are Streams?
They are wrapper for Collections and arrays
Does Streams don’t store its elements.
No
Are Streams immutable ?
Yes
Are Streams reusable?
No, Once traversed , Need to create a new one
Does Stream support indexes access its elements?
No
Can stream execute its operations concurrently?
Yes need not to write MT
Does Stream operations are lazy when possible?
Yes , when someone calls(Subscribe virtually)
Tell me the IF for Stream?
java.util.stream.Stream interface
Is java.util.stream.Stream interface works with primitives?
No
Which streams is to work with primitives?
IntStream, LongStream,DoubleStream,
How many ways the streams are created?
Using stream() List list = Arrays.asList(new String[]{"dsd","sdsd"});
Stream stream = words.stream();
Using Stream.of()
Stream stream = Stream.of(“hello”,”hola”, “hallo”, “ciao”);
String[] words = {“hello”, “hola”, “hallo”, “ciao”};
Stream stream = Stream.of(words);
How can we create IntStream?
int[] nums = {1,2,3,4}
Stream.of(nums)
If we do Stream.of(nums).count(), why it prints 1?
It the no of objects inside
What is the difference it makes to call a public nested class with the hosted class object?
that way nested class will have its value existed to hosted class, else without hosted object it will have its own existence
Does Arrays class also has stream functions?
YEs int nums[] = {1,2,34,4}
Arrays.stream(nums).count()
Or
Use the primitive version
IntStream.of(nums).count()
How can we generate a stream with Supplier?
With the Stream.generate function
static Stream generate(Supplier s)
Stream s = Stream.generate(new Supplier() { public Double get() { return Math.random(); } }).limit(5);
Stream s = Stream.generate(() -> Math.random()).limit(5);
Or just:
Stream s = Stream.generate(Math::random).limit(5);