Programming Articles - Page 2172 of 3366

How to 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

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

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

681 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

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

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

366 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

Windows registry access using Python (winreg)

Pradeep Elance
Updated on 07-Jan-2020 06:53:49

6K+ Views

As a versatile language and also availability of very large number of user supported modules, we find that python is also good at OS level programming. In this article we will see how python can access the registry of a windows operating system.We need to import the module named winreg into the python environment.In the below example we use the winreg module to first connect to the registry using the ConnectRegistry function and then access the registry using OpenKey function. Finally we design a for loop to print the result of the keys accessed.Exampleimport winreg #connecting to key in registry ... Read More

Python - Filter the negative values from given dictionary

Pradeep Elance
Updated on 07-Jan-2020 06:48:05

542 Views

As part of data analysis, we will come across scenarios to remove the negative values form a dictionary. For this we have to loop through each of the elements in the dictionary and use a condition to check the value. Below two approaches can be implemented to achieve this.Using for loopW simply loop through the elements of the list using a for loop. In every iteration we use the items function to compare the value of the element with the 0 for checking negative value.Example Live Demodict_1 = {'x':10, 'y':20, 'z':-30, 'p':-0.5, 'q':50} print ("Given Dictionary :", str(dict_1)) final_res_1 ... Read More

Python - Filter even values from a list

Pradeep Elance
Updated on 07-Jan-2020 06:46:30

1K+ Views

As part of data analysis require to filter out values from a list meeting certain criteria. In this article we'll see how to filter out only the even values from a list.We have to go through each element of the list and divide it with 2 to check for the remainder. If the remainder is zero then we consider it as an even number. After fetching these even numbers from a list we will put a condition to create a new list which excludes this even numbers. That new list is the result of the filtering condition we applied.Using for ... Read More

Multiplication of two Matrices in Single line using Numpy in Python

Pradeep Elance
Updated on 07-Jan-2020 06:41:16

743 Views

Matrix multiplication is a lengthy process where each element from each row and column of the matrixes are to be multiplied and added in a certain way. For matrix multiplication, the number of columns in the first matrix must be equal to the number of rows in the second matrix. The result matrix has the number of rows of the first and the number of columns of the second matrix.For smaller matrices we may design nested for loops and find the result. For bigger matrices we need some built in functionality in python to tackle this. We will see both ... Read More

All possible permutations of N lists in Python

Pradeep Elance
Updated on 07-Jan-2020 06:34:45

1K+ Views

If we have two lists and we need to combine each element of the first element with each element of the second list, then we have the below approaches.Using For LoopIn this straight forward approach we create a list of lists containing the permutation of elements from each list. we design a for loop within another for loop. The inner for loop refers to the second list and Outer follow refers to the first list.Example Live DemoA = [5, 8] B = [10, 15, 20] print ("The given lists : ", A, B) permutations = [[m, n] for m in ... Read More

Add leading Zeros to Python string

Pradeep Elance
Updated on 18-Feb-2020 11:22:50

650 Views

We may sometimes need to append zeros as string to various data elements in python. There may the reason for formatting and nice representation or there may be the reason for some calculations where these values will act as input. Below are the methods which we will use for this purpose.Using format()Here we take a DataFrame and apply the format function to the column wher we need to append the zeros as strings. The lambda method is used to apply the function repeatedly.Example Live Demoimport pandas as pd string = {'Column' : ['HOPE', 'FOR', 'THE', 'BEST']} dataframe=pd.DataFrame(string) print("given column is ") ... Read More

How to populate a Map using a 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

Advertisements