Sort a List Using Comparator with Method Reference in Java 8

raja
Updated on 13-Jul-2020 09:23:06

2K+ Views

Java 8 introduced changes in the Comparator interface that allows us to compare two objects. These changes help us to create comparators more easily. The first important method added is the comparing() method. This method receives as a parameter Function that determines the value to be compared and creates Comparator. Another important method is the thenComparing() method. This method can be used to compose Comparator.In the below example, we can sort a list by using the first name with comparing() method and then the last name with the thenComparing() method of Comparator interface.Exampleimport java.util.*; public class MethodReferenceSortTest {    public static void main(String[] ... Read More

Static Finger Theorem in Data Structure

Arnab Chakraborty
Updated on 13-Jul-2020 09:16:10

184 Views

STATIC FINGER THEOREM − Let f is treated as a specific element called the finger.Then the below expression is a bound on the cost of splaying a sequenceO(m + n log(n) + Σ Sum log (|f - i[j]| + 1))jNOTE − |f-i| is denoted as the distance in the symmetric ordering of the items between the finger and item i.Where m is denoted as number of update or access operations on a tree having at most n nodes.Observe that, at least in amortized sense, the time taken for first m operations on a tree that never exceeds more than n ... Read More

Populate a Map Using Lambda Expression in Java

raja
Updated on 13-Jul-2020 09:11:10

2K+ Views

A Map is a collection object that maps keys to values in Java. The data can be stored in key/value pairs and each key is unique. These key/value pairs are also called map entries.In the below example, we can populate a Map using a lambda expression. We have passed Character and Runnable arguments to Map object and pass a lambda expression as the second argument in the put() method of Map class. We need to pass command-line arguments whether the user enters 'h' for Help and 'q' for quit with the help of Scanner class.Exampleimport java.util.*; public class PopulateUsingMapLambdaTest {    public static void main(String[] args) {       Map map = new ... Read More

Implement ObjIntConsumer Interface Using Lambda Expression in Java

raja
Updated on 13-Jul-2020 09:09:29

356 Views

The ObjIntConsumer interface is a kind of functional interface and defined in java.util.function package. This functional interface expects an object-valued and int-valued argument as input and does not produce any output. It holds only one functional method, accept(Object, int).Syntax@FunctionalInterface    public interface ObjIntConsumer {       void accept(T t, int value) }In the below examples, we can implement the ObjIntConsumer interface by using a lambda expression.Example-1import java.util.function.*; public class ObjIntConsumerInterfaceTest1 {    public static void main(String args[]) {       ObjIntConsumer objIntConsumberObj = (t, value) -> {   // lambda expression          if(t.length() > value) {         ... Read More

Implement toIntFunction Using Lambda and Method Reference in Java

raja
Updated on 13-Jul-2020 09:07:52

676 Views

ToIntFunction is the built-in functional interface defined in java.util.function package. This functional interface expects an argument as input and produces an int-valued result. It can be used as an assignment target for a lambda expression or method reference. The ToIntFunction interface holds only one method, applyAsInt(). This method performs an operation on the given argument and returns the int-valued result.Syntax@FunctionalInterface public interface ToIntFunction {    int applyAsInt(T value); }In the below example, we can implement ToIntFunction by using lambda expression and method reference.Exampleimport java.util.function.ToIntFunction; import java.time.LocalDate; public class ToIntFunctionInterfaceTest {    public static void main(String[] args) {       ToIntFunction lambdaObj = value -> ... Read More

Reverse a String Using Lambda Expression in Java

raja
Updated on 13-Jul-2020 09:03:02

3K+ Views

A String is an object that represents a sequence of characters and immutable in Java. We can reverse a string entered by the user using the charAt() method of String class to extract characters from the string and append them in reverse order to reverse the entered string.In the below example, we need to reverse a string using lambda expression with the help of the Scanner class.Exampleimport java.util.Scanner; interface StringFunc {    String func(String n); } public class StringFuncLambdaTest {    public static void main(String args[]) {       Scanner sc = new Scanner(System.in);       StringFunc reverse = (str) -> {   // ... Read More

Serialize a Lambda Function in Java

raja
Updated on 13-Jul-2020 08:45:42

810 Views

The Serialization is a process for writing the state of an object into a byte stream so that we can transfer it over the network. We can serialize a lambda expression if its target type and its captured arguments have serialized. However, like inner classes, the serialization of lambda expressions is strongly discouraged.In the below example, we can serialize and deserialize a lambda function using a Function interface.Exampleimport java.io.*; import java.util.function.Function; interface MyInterface {    void hello(String name); } class MyImpl implements MyInterface {    public void hello(String name) {       System.out.println("Hello " + name);    } } public class SerializeDeSerializeLambdaTest {    public static void main(String[] args) ... Read More

Use IntStream in Lambdas and Method References in Java

raja
Updated on 13-Jul-2020 08:42:07

6K+ Views

An IntStream interface extends the BaseStream interface in Java 8. It is a sequence of primitive int-value elements and a specialized stream for manipulating int values. We can also use the IntStream interface to iterate the elements of a collection in lambda expressions and method references.Syntaxpublic interface IntStream extends BaseStreamExampleimport java.util.stream.IntStream; public class StringToIntegerStreamTest {    public static void main(String[] args) {       String str = "Tutorials Point";       IntStream stream = str.chars();       stream.forEach(element -> System.out.println(((char)element))); // using lambda expression    } }OutputT u t o r i a l s P o i n tExampleimport java.util.*; ... Read More

Use IntSupplier in Lambda Expression in Java

raja
Updated on 13-Jul-2020 08:41:18

975 Views

An IntSupplier is a functional interface defined in "java.util.function" package. This interface represents an operation that takes without arguments and returns the result of int type. IntSupplier interface has only one method, getAsInt() and returns a result. This functional interface can be used as an assignment target for lambda expressions or method references.Syntax@FunctionalInterface public interface IntSupplier {    int getAsInt(); }Exampleimport java.util.function.IntSupplier; public class IntSupplierTest {    public static void main(String[] args) {       IntSupplier intSupplier1 = () -> Integer.MAX_VALUE;  // lamba expression       System.out.println("The maximum value of an Integer is: " + intSupplier1.getAsInt());       IntSupplier intSupplier2 = () -> ... Read More

Timer in C++ Using System Calls

Arnab Chakraborty
Updated on 13-Jul-2020 08:40:17

933 Views

Here we will see how to design timer in C++ using a system call. We will not use any graphics or animations. Here timer means the stopwatch, that is up-counting the time. The used system calls are −sleep(n) − This will help the program to sleep for n number of secondssystem() − This is used to execute the system command by passing command as an argument to this function.Example Live Demo#include #include #include #include using namespace std; int hrs = 0; int mins = 0; int sec = 0; void showClk() {    system("cls");    cout

Advertisements