How to get current date time (today’s date) in Java

In this article, we will discuss how to get current date and time using the legacy Date and Calendar apis alongwith the Java 8 LocalDate, LocalTime, LocalDateTime and ZonedDateTime apis. Current date using java.util.Date instance The Date class constructor creates a Date object that represents the time at which it was allocated. Date d1 = […]

Java 8

4 ways to sort a List using Stream or Lambda Expression in Java 8

This articles discusses various approaches to sort a List in Java 8 using Streams and Lambda Expressions. Sorting using Stream sorted() method Sorting in reverse using Stream sorted() method Sorting using lambda expression and Stream sorted() method Sorting using lambda expression with Collections.sort()   1. Sorting a List using Stream sorted() method Stream sorted() returns […]

Java 8

Java 8 Map : compute()

compute() Map compute() function computes a mapping for the specified key and its current mapped value. It uses a BiFunction for the mapping. Syntax : default V compute(K key, BiFunction<? super K,? super V,? extends V> remappingFunction) Java8 compute() example package com.topjavatutorial; import java.util.HashMap; import java.util.Map; public class TestMap5 {   public static void main(String[] args) […]

Java 8

Java 8 Map : merge()

Map merge() Syntax : default V merge(K key, V value, BiFunction<? super V,? super V,? extends V> remappingFunction) If the specified key is not already associated with a value or is associated with null, associates it with the given non-null value. Otherwise, replaces the associated value with the results of the given remapping function, or […]

Java 8

Java 8 Map : computeIfAbsent

computeIfAbsent Syntax : default V computeIfAbsent(K key, Function<? super K,? extends V> mappingFunction) If the specified key is not already associated with a value (or is mapped to null), attempts to compute its value using the given mapping function and enters it into this map unless null. computeIfAbsent() uses a Function . The Function runs […]