Posts

Java 8 - Map(Enhancements of existing methods) and Annotations

Hi, I am Malathi Boggavarapu working at Volvo Group and i live in Gothenburg, Sweden. I have been working on Java since several years and had vast experience and knowledge across various technologies. Map - Enhancements of existing methods forEach() - This is the same kind of method that is defined on iterable interface which is available for all instances of Collection.  forEach is also available for Map and takes BiConsumer as parameter. BiConsumer is a regular Consumer which takes two parameters instead of one and those two parameters are ofcourse Key and value pair of the Map Map<String, Person> map = ...; map.forEach((key, person) -> System.out.println(key + " " +person)); getOrDefault() - get() method in earlier versions may return sometimes null value. null value will be returned if either the key is not present in the Map or the key is associated with null value. In java8, getorDefault method is used to specify the default v

Java 8 - Strings, IO's and other bits and pieces

Hi, I am Malathi Boggavarapu working at Volvo Group and i live in Gothenburg, Sweden. I have been working on Java since several years and had vast experience and knowledge across various technologies. In this course we will discuss about Strings, IO enhancements, Comparators and many more. So let's get started Strings, IO's and other bits and pieces Java 8 is not only about lambda expressions and Streams. String class Java I/O package Collection interface Comparators - New way of writing them in Java8. Numbers Map Annotations Strings in Java 8 String s = "Hello world!" IntStream stream = s.chars(); // creates a stream on letters of s stream.mapToObj(letter -> (char)letter)            .map(Character::toUpperCase)                       .forEach(System.out::print); Output will be HELLO WORLD! StringJoiner String s1 = "Hello" String s2 = "World" String s = s1 + s2; The above way of string conca

Java 8 - Date API

Hi, I am Malathi Boggavarapu working at Volvo Group and i live in Gothenburg, Sweden. I have been working on Java since several years and had vast experience and knowledge across various technologies. This course teach you about Date API in Java8. Date API in java8 java.time - completly replaces java.util and Calendar classes and still keeps inter-operability with legacy API Instant - It is a class and is a point on the timeline Timeline - Assume it is line of time with dates on it and it has given precision and precision is the nanosecond. Conventions of instant class Instant 0 is the January the 1st, 1970 at midnight GMT Instant.MIN is 1 billion years ago Instant.MAX is Dec 31 of the year 1,000,000,000 Instant.now() is the current instant The timeline begins from 1 billion years ago and end with 1 billion years from now. Precision of the time would be in the nanosecond Instant object is immutable Instant start = Instant.now(); ....... Insta

Java 8 - Streams and Collectors (With examples)

Hi, I am Malathi Boggavarapu working at Volvo Group and i live in Gothenburg, Sweden. I have been working on Java since several years and had vast experience and knowledge across various technologies. This course will teach you about Java 8 feature - Streams and Collectors Stream and Collectors Stream : It is a new concept. It gives ways to efficiently process large amounts of data and also smaller amounts of data. Data can be processed automatically in parallel to leverage the computing power of multi core CPU's. What is a Stream? - Object on which one can define operations.Operations can be Map, Filter or Reduce operations - An object that does not hold any data. For example, in Collection i have data and do basic operations on it. On stream i don't hold any data. - An object that should not change the data it processes. Because it is distributed across multiple CPU's and since we don't want to be bothered with any visibility issues with a

Java 8 - Lambda expressions (Introduction and purpose) with examples

Hi, I am Malathi Boggavarapu working at Volvo Group and i live in Gothenburg, Sweden. I have been working on Java since several years and had vast experience and knowledge across various technologies. This post teach you about Java 8 - Lambda expressions using some practical examples Introduction to Lambda expressions To understand it easily, lets start with FileFilter example. public interface Filefilter{     boolean  accept(File file); } public class JavaFileFilter implements FileFilter{     boolean accept(File file){          return file.getName().endsWith(".java")    } } JavaFileFilter fileFilter = new JavaFileFilter(); File dir = new File("d:/tmp"); File[] files = dir.listFiles(fileFilter); 2) FileFilter fileFilter = new FileFilter(){       boolean accept(File file){          file.getName().endsWith(".java");      } }; File dir = new File("d:/tmp"); File[] files = dir.listFiles(fileFilter);

Queue, Deque, Stacks, SortedMap, NavigableMap, Java 8 enhancements related to Map

Hi, I am Malathi Boggavarapu working at Volvo Group and i live in Gothenburg, Sweden. I have been working on Java since several years and had vast experience and knowledge across various technologies. Queues FIFO - First in first out add() - Colletions have a method add to add an element to the queue. General functionality of add method returns true if the element is added and false if the element has not been added. In case of queue, it returns true when the element has been added and throws exception when the queue is full Offer() - This method returns true if the element has been added to the queue and returns false when the queue is full. In case of adding elements to the queue, offer method should be used. E remove() - Removes the element from the queue. remove throws exception when the queue is empty. E poll() - returns null when the queue is empty E element() - Just read elements without actually removing. Throws exception when the queue is empty

Differences between Map implementations - HashMap, TreeMap, LinkedHashMap, WeakHashMap, IdentityHashMap, EnumMap

Hi, I am Malathi Boggavarapu working at Volvo Group and i live in Gothenburg, Sweden. I have been working on Java since several years and had vast experience and knowledge across various technologies. HashMap, TreeMap, LinkedHashMap, WeakHAshMap, IdentityHashMap, EnumMap     HashMap is implemented as a hash table, and there is no ordering on keys or values.  TreeMap is implemented based on red-black tree structure, and it is ordered by the key. Key object should implement Comparable interface inorder to compare with other keys and maintain sorted order.  TreeMap is not synchronized. We can make it synchronized by using Collections.synchronizedMap method. Ex: Simple example which outputs the natural ordering of key elements.  Map treeMap = new TreeMap(); treeMap.put("f",10); treeMap.put("d", 4); treeMap.put("a", 1); treeMap.put("b", 2); output :  a=1, b=2, d=4, f=10