Articles on Trending Technologies

Technical articles with clear explanations and examples

Static Finger Theorem in Data Structure

Arnab Chakraborty
Arnab Chakraborty
Updated on 13-Jul-2020 293 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

How to populate a Map using a lambda expression in Java?

raja
raja
Updated on 13-Jul-2020 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 ...

Read More

How to implement the ObjIntConsumer interface using lambda expression in Java?

raja
raja
Updated on 13-Jul-2020 476 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) -> { ...

Read More

How to implement ToIntFunction using lambda and method reference in Java?

raja
raja
Updated on 13-Jul-2020 797 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[] ...

Read More

How to reverse a string using lambda expression in Java?

raja
raja
Updated on 13-Jul-2020 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

How to serialize a lambda function in Java?

raja
raja
Updated on 13-Jul-2020 966 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

How to use IntStream in lambdas and method references in Java?

raja
raja
Updated on 13-Jul-2020 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 ...

Read More

How to use BooleanSupplier in lambda expression in Java?

raja
raja
Updated on 13-Jul-2020 3K+ Views

BooleanSupplier is a functional interface defined in the "java.util.function" package. This interface can be used as an assignment target for a lambda expression or method reference. BooleanSupplier interface has only one method getAsBoolean() and returns a boolean result, true or false.Syntax@FunctionalInterface public interface BooleanSupplier {    boolean getBoolean(); }Exampleimport java.util.function.BooleanSupplier; public class BooleanSupplierLambdaTest {    public static void main(String[] args) {       BooleanSupplier Obj1 = () -> true;       BooleanSupplier Obj2 = () -> 5 < 50; // lambda expression       BooleanSupplier Obj3 = () -> "tutorialspoint.com".equals("tutorix.com");       System.out.println("Result of Obj1: " + Obj1.getAsBoolean());       ...

Read More

How to use FileFilter interface in lambda expression in Java?

raja
raja
Updated on 13-Jul-2020 990 Views

A FileFilter is a functional interface from the "java.io" package. It can be used as the assignment target for a lambda expression or method reference. An instance of the FileFilter interface passed to the listFiles() method of the File class. FileFilter interface having one abstract method accept() and it tests whether or not the specified abstract pathname has included in a pathname list.Syntax@FunctionalInterface public interface FileFilterExampleimport java.io.File; import java.io.FileFilter; public class FileFilterTest { public static void main(String[] args) { File dir = new File("C:/Program Files/Java/jdk1.8.0_211"); ...

Read More

How to implement PropertyChangeListener using lambda expression in Java?

raja
raja
Updated on 13-Jul-2020 5K+ Views

A PropertyChangeListener is a functional interface from java.beans package. It has one abstract method propertyChange() and gets called when a bound property is changed. This method takes a PropertyChangeEvent argument that has details about an event source and the property that has changed. A PropertyChangeSupport can be used by beans that support bound properties. It can manage a list of listeners and dispatches property change events. The PropertyChangeListener's implementer does the role of an Observer and bean wrapping the PropertyChangeSupport is Observable.Syntaxvoid propertyChange(PropertyChangeEvent evt)Exampleimport java.beans.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.border.*; public class PropertyChangeListenerLambdaTest {    public static void main(String [] args) {   ...

Read More
Showing 52041–52050 of 61,299 articles
Advertisements