Skip to main content

Posts

Showing posts from November, 2018

Java 8 - Streams - flatMap()

Greetings! With stream API it is easy to iterate over a collection and do whatever operation we like to do. Let's say we have a number list which we would like to print power of 2 of each. List<Integer> attempt1 = Arrays.asList(1, 2, 3, 4, 5); attempt1.stream().map(x -> x * x).forEach(System.out::println); // 1, 4, 9, 16, 25 What if we have a List of List? let's try that out. List<List<Integer>> attempt2 = Arrays.asList(Arrays.asList(1, 2, 3), Arrays.asList(2, 5)); Stream<List<Integer>> attempt2Stream = attempt2.stream(); attempt2Stream.map(x -> x * x).forEach(System.out::println); // compile error attempt2Stream.map(x -> x.stream().map(y -> y * y)).forEach(System.out::println); // java.util.stream.ReferencePipeline$3@4c873330, java.util.stream.ReferencePipeline$3@119d7047 As you can see Stream.map() doesn't give us the expected result for the type Stream<List<Integer>>. This is because map() operation ...

Java 8 - Streams - reduce()

Greetings! Stream API provides several terminal operations for common tasks like count, min, max, sum and average.  These operations return a single value and do specific task. Stream.collect() operation is also a terminal operations but it return a Collection. reduce() is a more-general purpose operations which can be used to combine the content of stream. These terminal operations are called reduction operations. Stream.reduce() This method mainly has two forms. With initial value. Without initial value. T reduce(T identity, BinaryOperator<T> accumulator); Optional<T> reduce(BinaryOperator<T> accumulator); reduce uses BinaryOperator (which extends BiFunction) functional interface which has the form below. R apply(T t, U u); How Does It Work Let's consider below sum operation. List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5); int result = 0; for (Integer number : numbers) { result = result + number; }...

Hibernate - Basic Annotations

Greetings! @Entity - Add in class level. Declares that this class is an entity @Entity public class Employee { // ... } @Table - Add in class level. Define table, schema, catalog. Use name property to add the name of the table unless class name is used as the table name. @Entity @Table(name = "tbl_employee") public class Employee { // ... } @Column - Add column properties. @Column(name = "first_name") private String firstName; @Id - Declares property as the identifier of this class. @Id private Long id; @GeneratedValue - use along with @Id to generate primary key automatically. JPA defines 5 strategies. @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; AUTO - select depending on the underlying database. IDENTITY SEQUENCE TABLE identity copy - identity is copied form another entity. @Version - Adding this will add optimistic locking capability. For an update query Hibernate will automatically ...

Hibernate - Value Types

Greetings! When we design our domain models we can have more important classes and less important classes like Address, String, etc. In other word we have fine-grained object model which means more classes than tables. For an example User can have an Address. We can create separate class for Address fields and add it as a property in User using composition. public class User { private Long id; private String name; private Integer age; private Address address; } Here User is an entity. Name, Age, Address are value types. Entity classes need an identifier. Value type doesn't have an identifier because instances are identified through owning entity. What is an Entity? Has a database identity. Has it's own life cycle. Object reference is persisted in the database. What is a Value Type? Doesn't have own identity. Embedded into owning entity. Represent table columns. How to map? Basic types can be directly mapped using @C...

Hibernate - Identity

Greetings! Identity is the fact that being who or what a thing is. In Java two objects are identical if they are referring to the same memory location. object1 == object2 Equals means that they have the same value but may not have same memory location. object1.equals(object2) In relational databases, objects are identical if they share the same table and have same primary key. This is known as database identity in Java side. @Id @GeneratedValue private Long id; Because @Id on the field, hibernate use field access. If we don't provide @GeneratedValue, JPA provider doesn't provide a key for us. We have to assign ourselves. These are called application-assigned identifiers. Candidate Key Column(s) we could use to identify a database row is called candidate keys. Never null. Unique. Never changes. Natural Key A key with a business meaning is called a natural key. For an Employee table can have a employee number to represent employee's identit...

Hibernate - Introduction

Greetings! Problem in hand Almost all the applications need to save data. This persistence storage most probably is a database. This storage is independence system which is called data independence. Here comes the problem. How do we convert relational data to Java objects? Sure we can convert one by one which is not convenient due to; It takes lot of time to write code. Difficult to maintain. Need to write all SQL queries. Need to handle transactions. Have to maintain database connections. Not portable. To solve these kind of problems, Object Relational Mapping frameworks are born. Hibernate is the most popular among them. In an era of Spring dominating most part of the application, Hibernate is almost hidden. Thanks to Spring Data most of the time we do not have write any SQL query. Spring help us to generate those. JPA, Hibernate, Spring Data JPA Java Persistence API is the specification. Hibernate is one of the implementations of JPA. Spring Data provides us anoth...

Java Concurrency - ThreadLocal

Greetings! Protecting shared, mutable data is difficult. Easiest way to guarantee thread safety is not to share. This technique is called thread confinement. One way to achieve this is using ThreadLocal class. This uses internal thread-local variable to store data by the executing thread and provides get() and set() methods to access it. It internally get the current thread (Thread.currentThread()) and a custom Map to assign data directly in Thread class. This behaves like Map<Thread, T> object (though this is not how it is done). ThreadLocal stores per-thread value and provides set(), get() methods to access it. public class TimeHolder { private static ThreadLocal<Long> threadLocal = ThreadLocal.withInitial(() -> System.currentTimeMillis()); public Long get() { return threadLocal.get(); } public void set() { threadLocal.set(System.currentTimeMillis()); } public void print() { System.out.println...

Java Concurrency - Immutable

Greetings! Problem with shared object is any thread can modify it. What if we have an object type which can not be changed? This is where immutable objects come into play. Immutable object is an object that once created it's state can not be changed. We can construct an immutable object by protecting the inner state by not sharing any setter method to change inner state. How to construct an immutable class? Do not provide setter methods. Make all fields final and private. Make the constructor private and use a factory method to create objects. Do not share mutable objects. If mutable objects need to be shared, share a copy. If the constructor accepts external mutable objects as parameters create a copy of them. Make the class final to avoid sub classing. public final class Counter { private final int count; private Counter(int initialCount) { this.count = initialCount; } public static Counter newCounter(int initialCount) ...

Java Concurrency - Visibility

Greetings! Synchronization is not only protecting critical sections. It also provide memory visibility. That means, when one thread modify the state other threads should be able to see the changes without any issue. We should not share objects without proper synchronization. Unless it will lead to stale data which causes; Unexpected exceptions. Infinite loops. Corrupted data. Inaccurate results. Out of thin air safety A thread can see some value of shared data which is updated by another thread (even it is stale) is called out-of-thin-air safety. All threads should see most up-to-date values of shared mutable variables. Volatile It is a weaker form of synchronization. Do not re-order. Put in shared memory (not in cpu's cache). Good for simple situations like maintaining a flag, completion flag. Does not guarantee atomicity. Guarantee only visibility. Publishing Make an object available outside of its current scope. public variables. non p...

Java Concurrency - Volatile

Greetings! volatile is a keyword in Java. When we declare a variable as volatile it doesn't store in CPU cache. Instead it is put in shared memory. Because of that multiple threads can access the same memory location and get the updated value. Hence this is considered as a weaker synchronization mechanism. volatile is not suitable for atomic operations. It is best suited for single operations like a flag. private volatile boolean completed; Store in main memory. never store in thread locally. Guarantee visibility. Not suitable for atomic operations like read-modify-write. No lock involved. Never block other threads. Suitable for operations like having a flag. Can be used if the value doesn't depend on previous value. Have a performance hit because it doesn't store in cache. Weaker form of synchronization.

Java Concurrency - Intrinsic Lock & Reentrancy

Greetings! Any Java object can implicitly act as a lock for purposes of synchronization. These built-in locks are intrinsic locks. Java provides synchronized block as a built-in locking mechanism to enforce atomicity. Java built in object lock is called Intrinsic Lock. synchronized (lock) { // shared data } When use in method level, invoking object is the lock. Act as a mutex (only one thread at a time) Lock is acquired by the executing thread before entering a synchronized block. Reentrancy Thread owning a lock can again acquire the lock is called reentrant lock. Can claim the lock multiple time without blocking itself. Without reentrant lock, there will be a dead lock. Intrinsic locks are re-entrant. Locks are acquired on a per-thread basis rather than per-invocation basis. Every shared, mutable state should be guarded by the same lock.

Java Concurrency - join()

Greeting! Let's see the output of below program first. public class ThreadJoin { public static void main(String[] args) { System.out.println("start"); Thread t1 = new Thread(() -> System.out.println("in t1")); Thread t2 = new Thread(() -> System.out.println("in t2")); t1.start(); t2.start(); System.out.println("end"); } } start end in t1 in t2 Notice that start and end print before it prints the messages from the threads. This is because thread are running separately hence main thread executes immediately. What if we want to print end after executing t1 and t2? We can ask main thread to wait for finishing t1 and t2. It is like a relay race. Second runner is waiting for the first runner to come and hand over the flag to him. join - execution thread put on wait until this thread is finished. public class ThreadJoin { public static void main(String[] args) { ...

Java Concurrency - Race Conditions - Read-Modify-Write

Greetings! When two or more threads access shared data at the same time race condition occurred. These operation should be atomic to thread-safe and we call them "compound actions". read-modify-write - read a value, modify it, write back (ex: increment a value). check-then-act - check a condition and act accordingly; common situation is lazy initialization (ex: singleton pattern). In this blog post we are going to examine read-modify-write race condition. Let's say we need a counter and we designed below class. public class Counter { private int count = 0; public int getAndIncrement() { return count++; } public int get() { return count; } } Can you spot a possible bug here? Well, for a single threaded application this is working fine. But for a multi-threaded application this will give unexpected results. Problem here is count++ is not an atomic operation. Let's create few threads and see the results. imp...

Java Concurrency - How To Create A Thread

Greetings! It is easy to create a thread in Java using 2 well known methods. extends Thread class. implement Runnable interface. Then we create a Thread object and start it! It is as simple as that. public class ExtendThread extends Thread { @Override public void run() { System.out.println("hello from extended thread"); } } Thread thread = new ExtendThread(); thread.start(); public class ImplmentedRunnable implements Runnable { @Override public void run() { System.out.println("hello from runnable implementation"); } } Thread thread = new Thread(new ImplmentedRunnable()); thread.start(); In Java 8; Thread thread = new Thread(() -> System.out.println("hello from java 8")); thread.start(); It is not a good practice to extend the Thread class. Thread states. A thread exist in several states. New - when we create an instance of Thread class it is in New state. Run...

Java Concurrency - Thread Safety

Greetings! What is thread safety? When we have multiple threads accessing piece of code simultaneously without unwanted side effects we can say our code is thread safe. Correctness!!! Synchronization Synchronization allows to control the program flow and access to shared data for concurrently executing threads. It is tempting to think synchronized is the only way to do this in Java but it is the primary mechanism which provides exclusive locking. volatile variable, explicit locks and atomic variables are other types. We can synchronize using; mutex lock - allow only one thread at a time to execute a specific section of code (critical section !!) read/write lock - permit concurrent reads and exclusive writes to a protected shared resource. condition variable - block threads until a particular condition is true. semaphore - simply a variable which is a count (counting semaphore) or 0/1 switch (binary semaphore). How to share mutable state between threads? Don't...

Java Concurrency - Introduction

Greetings! We can sing while having a bath. We can listen to song while jogging. We are multitasking!. Can a computer to do that? How can a computer do that? As a developer how do we do that?   Process vs Thread Let's take word processor as an example. In a word processor there are spell checker, auto correct, etc. Which means within Word application there are sub tasks are running behind the scene. And those tasks share the same address space as the process. We name those sub tasks as Threads and main task as a Process. Back in old days, a computer had only one CPU. On the other hand computer could handle only one task at a time. It switch between tasks very quickly so that we can't see the difference. ( Time Slice ) Modern computers come with multiple processors allowing multiple tasks to run at the same time. When dealing with multiple threads we have to think; Two threads update a variable simultaneously. One threads depends on another thread. Two threads ...

Java Collections - Lists

Greetings! It is probably Lists are the most used Collections. Can have duplicates. Maintain the element order. Can iterate in added order. Lists maintain the added order. Hence it can be accessed by given position. List interface have additional methods to fulfill this requirement. // add, remove in given position void add(int index, E e) E get(int index) E remove(int index) E set(int index, E e) // search position by object int indexOf(Object o) int lastIndexOf(Object o) // get a sub list by given positions (toIndex exclusive) List<E> subList(int fromIndex, int toIndex) // get listiterator ListIterator<E> listIterator() subList doesn't return a new list. It returns a view of the existing list by given positions. Because of this changing the original list changes the sub list. But changing the sub list doesn't affect the original list. ConcurrentModificationException can be occurred if the original list is changed and try access the sub list. Li...

Java Collections - Interfaces

Greetings! There are number of data structures. To handle varies kind of collections, Collection framework has different interfaces. Collection - fundamental interface for most of the collections Set  - no duplicates. no order. List - ordered collection. contain duplicates. can access using position. Queue - typically has first-in-first-out structure. Deque - double-ended queue. can behave both as first-in-first-out and last-in-last-out. can insert and remove from both ends. SortedSet - maintain sort order Map - contains key-value pairs. no duplicates allowed. SortedMap - maintain sorted key order Collection Interface Map Interface Iterator Other than that, there is RandomAccess interface.

Java Collections - Collection Interface

Greetings! Collection interface is the base interface for most of the classes in Collection Framework. It mainly contains methods for add, remove and iteration. public interface Collection<E> { boolean add(E element); Iterator<E> iterator(); boolean remove(Object o); // .... } Other than that this contains a lot of common utility methods. int size() boolean isEmpty() boolean contains(Object obj) boolean containsAll(Collection<?> c) boolean equals(Object other) boolean addAll(Collection<? extends E> from) boolean remove(Object obj) boolean removeAll(Collection<?> c) void clear() boolean retainAll(Collection<?> c) Object[] toArray() <T> T[] toArray(T[] arrayToFill) Java 8 has introduced new default methods to convert the Collection into a stream. default Stream<E> stream() default Stream<E> parallelStream() default boolean removeIf(Predicate<? super E> filter) There is no concrete impleme...

Java Collections - ConcurrentModificationException

Greetings! When you use a collection and suddenly encounter ConcurrentModificationException it is very confusing. Why are we getting this when we are not using multi-threading? Actually this doesn't mean you are using multi-threading. This means that the collection in used is modified (add/remove). For an example let's say we want remove multiples of 3 from an array list. List<Integer> numbers = IntStream.range(1, 20).boxed().collect(toList()); for (Integer number : numbers) { if (number % 3 == 0) { numbers.remove(number - 1); } } This little program will throw java.util.ConcurrentModificationException because we are modifying the collection directly while iterating. Underline iterator is implemented so that it checks for any modification. If it found any modification it throws new ConcurrentModificationException. Instead of removing items from the collection, we need to remove it from the iterator. List<Integer> numbers = IntStre...

Java Collections - Iterable and Iterator

Greetings! It is easily misunderstand that Collection interface is the super interface in JCF. Actually, Collection interface extends Iterable interface. public interface Iterable<T> { Iterator<T> iterator(); } Idea of this is to give a common way traverse the underline collection using the Iterator. public interface Iterator<E> { boolean hasNext(); E next(); void remove() } Prior to Java 5, for loop was used as below, for (Iterator iterator = collection.iterator(); iterator.hasNext(); ) { iterator.next(); } With Java 5 foreach loop, iterators are used internally by the compiler and it is simplified. for (SomeObject object : collection) { // .... } With Java 8 lambda expressions both Iterable and Iterator interfaces are updated to simplify this even more. iterator.forEachRemaining(element -> { // do something }); collection.foreach(element -> { // do something }); next() and remove()...

Java Collections - Introduction

Greetings! We have to use different kind of data structures and algorithms in our code. Luckily for us, Java provide Java Collection Framework (JCF) for that purpose. Prior to Java 1.2 Java had only few data structures like Vector, Stack, Hashtable, Enumeration. Java 2 introduced proper collection API to address all data structures issues. Easy to use collections. High performance. Easy to extend. There is no "one for all" implementation. Common Interfaces Iterable - provide iteration through a collection. (this is outside of the api) Collection - main interface for the most of the collections. contains most of the core functionalities. Set - can not have duplicate. contains unique elements. doesn't maintain the insertion order. List - maintain the order. can have duplicates. Queue - define first in first out data structure. Map - key, value pairs. In Collection API, interfaces provide the necessary contract but it doesn't give us an idea abou...

Java 8 - Streams - How to Create a Stream?

Greetings! It is easy to guess that we can create a Stream using a collection. Actually it is one of the ways to create a Stream. Stream from collections Stream<Employee> stream = employees.stream(); Stream from values Using static Stream.of() we can create a stream using given values. Stream<String> countryStream = Stream.of("Sri Lanka", "India", "Pakistan"); Stream<Integer> integerStream = Stream.of(1, 2, 3); Stream<Employee> employeeStream = Stream.of(new Employee()); Stream from arrays Arrays class has Arrays.stream() static method to convert given array into a stream. int[] intArray = { 1, 2, 3, 4, 5 }; IntStream intStream = Arrays.stream(intArray); String[] stringArray = { "Hello", "World" }; Stream<String> stringStream = Arrays.stream(stringArray); Stream from files Files class has static methods to open a file as a stream. // from a file try (Stream<String> linesStr...

Java 8 - Streams - Replace Traditional for Loops

Greetings! It is very common to have numeric iterations like below; for (int i = 1; i <= 100; i++) { // do something } Java 8 has static methods for this common task in IntStream and LongStream. IntStream intStream = IntStream.rangeClosed(1, 100); // Inclusive. Up to 100: 1, 2, ..., 99, 100 IntStream.rangeClosed(1, 100).forEach(System.out::println); // Exclusive. Up to 99: 1, 2, ..., 98, 99 IntStream.range(1, 100).forEach(System.out::println); // With Steps: 1, 2, ..., 98, 100 IntStream.iterate(0, i -> i + 2).limit(50).forEach(System.out::println); Note that iterate produces an infinite stream.

Java 8 - Streams - Numeric Streams

Greetings! It is common to have numeric operations min, max, sum in our code. Stream interface have min and max methods but doesn't have sum method because it make no sense to have it. Even though we can have sum operation using reduce() it has unboxing cost. This is same for min, max operations. Considering those factors, Stream API provides separate interfaces for primitives. IntStream, LongStream and DoubleStream. Convert to numeric stream stream.mapToInt() - return IntStream stream.mapToLong() - return LongStream stream.mapToDoulbe() - return DoubleStream IntStream intStream = stream.mapToInt() Convert back to stream of objects numericStream.boxed() String<Integer> = intStream.boxed() Optional values For primitive operations Optional also have corresponding primitive optionals. OptionalInt, OptionalLong, OptionalDouble There are corresponding functional interfaces like IntPredicate, IntConsumer, IntUnaryOperator for numeric operations. Exa...

Java 8 - Streams - sum()

Greetings! Let's say we want to calculate total marks for a student for all subjects. public class Subject { private String name; private Integer marks; } int totalMarks = subjects.stream().map(Subject::getMarks).sum(); This is not working because stream doesn't have sum() operation. That make sense because stream is generic and it doesn't have any meaning for total (sum) operation for an object. And also if we use Integer, behind the scene it will covert to int (unboxing). For a large data set this will be a costly operation. So, to get our desired result we to need to convert our stream into IntStream which offers the sum operation. int totalMarks = subjects.stream().mapToInt(Subject::getMarks).sum(); mapToInt - this intermediate operation returns IntStream

Java 8 - Streams - Terminal Operations

Greetings! Terminal operations are used to close the stream and generate a result. This is the last operation in a stream pipeline hence eagerly executed. Can return concrete value types like (Integer, String, List, Optional), primitive value (int, long) or void. Ex:- forEach, collect, count, anyMatch, nonMatch, allMatch, findAny, findFirst, reduce, min, max, sum are terminal operations. Let's use employee data set for all operations. ( Complete code is here ) public class Employee { enum Gender { MALE, FEMALE }; private String name; private int age; private Gender gender; private Employee(String name, int age, Gender gender) { this.name = name; this.age = age; this.gender = gender; } public static Employee newInstance(String name, int age, Gender gender) { return new Employee(name, age, gender); } // getters } private static List<Employee> employees = Arrays.asList( ...

Java 8 - Streams - Intermediate Operations

Greetings! Intermediate operations are used to transform the stream into another. These operations can be chained and merge internally as necessary. Lazily process and process only when a terminal operations is called. Ex:- map, flatMap, filter, distinct, limit, skip, sorted are intermediate operations Note that forEach is a terminal operation which close the stream. map() Convert the iteration element into another. public static void main(String[] args) { List<String> countries = Arrays.asList("Sri Lanka", "India", "Pakistan", "England", "Australia"); countries.stream().map(String::toUpperCase).forEach(System.out::println); countries.stream().map(country -> { // create any object and return int length = country.length(); return "Length of ".concat(country).concat(" string is ").concat(String.valueOf(length)); }).forEach(System.out::println); } fil...

Java 8 - Streams - Introduction

Greetings! Let's have a look at below code. List<String> countries = Arrays.asList("Sri Lanka", "India", "Pakistan", "England", "Australia"); for (String country : countries) { if (country.length() > 7) { String upperCase = country.toUpperCase(); System.out.println(upperCase); } } This code has several problems. Process sequentially. Difficult to process parallel. Complex codes. Instruct how to do it, not what to do. External loop. (developer has to create the loop). To overcome these kind of problems and get the maximum from multi-core processors Java 8 introduces Stream API. The same code can be re-written using stream as below. countries.stream() .filter(country -> country.length() > 7) .map(country -> country.toUpperCase()) .forEach(System.out::println); As you can see it works like builder pattern. We chain several operations using a d...

Java 8 - Constructor References

Greetings! This is same as the method references, except the method name is new. Class::new When we have multiple constructors which constructor will be selected? It depends on the context. (firstName) -> new User(firstName) => User::new (firstName, lastName) -> new User(firstName, lastName) => User::new This can also be used with arrays with exact one parameter. n -> new int[n] int[]::new This helps to create arrays with generic types. Object[] array = stream.toArray(); User[] users = stream.toArray(User[]::new);

Java 8 - Method References

Greetings! In Java we have object references. We pass objects around. What if we can pass reference to a method? In Java 8 if we are executing only one method we can do this using lambda expressions. For an example these are equal.          (x) -> System.out.print(x) System.out::print As you can see, :: separates the name of the method from the class/ object. There are 3 ways we can use method references. object::method - object.method(parameters) Class::staticMethod - Class.staticMethod(parameters) Class::instanceMethod - firstParameter.method(parameters) In first 2 cases, parameters become the methods parameters. As you can see in System.out::print. If we have more than 1 parameters it is still same, (x, y) -> Math.pow(x, y) Math::pow Third case is different. First parameter becomes the target object of the method and rest become the parameters. For example, // (object, arguments) -> object.method(arguments) name -> name.toU...

Java 8 - Functional Interfaces

Greetings! Functional interface is an interface which has only one abstract method. Runnable, Comparable, ActionListener are few existing examples. interface with only one abstract method. You can have many methods in the interface but should have only one abstract method. To ensure that an interface has only one abstract method, Java8 comes with @FunctionalInterface annotation. It is not mandatory to have this, but with this compiler warns you when you break the contract. We can avoid inner classes' and use lambda expression in place of these interfaces. For an example we can create a thread like this; Thread thread = new Thread(() -> System.out.println("Hello functional interface")); thread.start(); Let's say we have fruits names and we need to print them according to given condition. With prior to Java 8, we can do it like this; public interface Testable { boolean test(String name); } import java.util.Arrays; import java.util.List; ...