Posts

Showing posts from October, 2018

Java 8: Remedies for What cannot be done in Generics

Image
Generic programming the way to reuse code for Objects of many different types. The unbounded wild card is "?" (which is actually "? extends Object"). You can read from it, but you cannot write. If your type variable is T then you can use only extends (never super ) with T then, the upper-bounded wildcard is "? extends T". you can define read from but can not add (only super support with wildcards) The term covariant preserves the ordering of types from more specific to more general. Collections are covariant when they use extends wildcard. The lower-bounded wildcard is "? super T". Collections are contravariant when they use super with a wildcard. The term contravariant preserves the ordering of types from more general to more specific. Here supplied element must be itself or above of the T . The rule of thumb is "producer => extends and consumer => super": PECS.  As shown in the above, you can copy the eleme

Java 8: Exception Handling

Image
According to Oracle 1 , Java SE 8 class called java.util.Optional<T> that is inspired from the ideas of Haskell and Scala. In the functional programming, exceptions are side effects and need to avoid. Optional in a Nutshell The Optional class includes methods to explicitly deal with the cases where a value is present or absent. You can create variable Optional<Something> sc = Optional.empty() or Optional<Something> sc = Optional.ofNullable(Something); for null object. Otherwise the Optional<Something> sc = Optional.of(Something) create the Something object. Patterns of using Optional This blog is the summary of the video 2 I went through. The problem is very simply demonstrated in the following code: package au.com.blogspot.ojitha.trainings.exception; import java.nio.file.Files; import java.nio.file.Paths; import java.util.stream.Stream; public class ExExample { public static void main(String[] args) { Stream.of("a.txt"

Learn to Learn

Image
Recently I have been through the 1 and 2 . Here is some notable things to remember as an effective learner. The idea is to study more is not necessarily effective. Instead, it may worst the performance. One of the underlying theory is that things are reinforced when we tend to do more off. Things that are punished or ignored when manage to do less off.  Rules It is worth to note down few rules 1. Rule the moment you started to slide, you're shovelling against the tide.: take a break. Pomodoro technique there are two modes in your brain _focus mode_: tight pattern of Knowledge control your thinking. There is very less opportunity to grasp the new things or being creative. _diffuse mode_: the relax mode. Models of knowledge are weakened. Ready for the new concepts.  This technique needs to be trained. However, most difficult is getting to the focus mode again. But it is important to switch between these modes as shown as above. 2. Rule Create a stu