Programming Articles - Page 2239 of 3366

How to get all elements of a List that match the conditions specified by the predicate in C#?

AmitDiwan
Updated on 16-Dec-2019 06:22:50

642 Views

To get all elements of a List that match the conditions specified by the predicate, the code is as follows −Exampleusing System; using System.Collections.Generic; public class Demo {    private static bool demo(int i) {       return ((i % 3) == 0);    }    public static void Main(String[] args) {       List list = new List();       list.Add(9);       list.Add(15);       list.Add(20);       list.Add(40);       list.Add(50);       list.Add(60);       Console.WriteLine("List elements...");       foreach (int i in list) {   ... Read More

How to get hash code for the specified key of a Hashtable in C#?

AmitDiwan
Updated on 16-Dec-2019 06:19:21

269 Views

To get hash code for the specified key of a Hashtable, the code is as follows −Example Live Demousing System; using System.Collections; public class HashCode : Hashtable {    public static void Main(string[] args) {       HashCode hash = new HashCode();       hash.Add("A", "Jacob");       hash.Add("B", "Mark");       hash.Add("C", "Tom");       hash.Add("D", "Nathan");       hash.Add("E", "Tim");       hash.Add("F", "John");       hash.Add("G", "Gary");       Console.WriteLine("Key and Value pairs...");       foreach(DictionaryEntry entry in hash) {          Console.WriteLine("{0} and {1} ", ... Read More

How to get Synchronize access to the ListDictionary in C#?

AmitDiwan
Updated on 16-Dec-2019 06:15:05

176 Views

To get synchronize access to the ListDictionary, the code is as follows −Example Live Demousing System; using System.Collections; using System.Collections.Specialized; public class Demo {    public static void Main() {       ListDictionary dict = new ListDictionary();       dict.Add("1", "SUV");       dict.Add("2", "Sedan");       dict.Add("3", "Utility Vehicle");       dict.Add("4", "Compact Car");       dict.Add("5", "SUV");       dict.Add("6", "Sedan");       dict.Add("7", "Utility Vehicle");       dict.Add("8", "Compact Car");       dict.Add("9", "Crossover");       dict.Add("10", "Electric Car");       Console.WriteLine("ListDictionary elements...");       ... Read More

What are the in-built functional interfaces in Java?

raja
Updated on 10-Jul-2020 14:00:23

8K+ Views

The java.util.function package defines several in-built functional interfaces that can be used when creating lambda expressions or method references.Inbuilt functional interfaces:1) Function InterfaceThe Function interface has only one single method apply(). It can accept an object of any data type and returns a result of any datatype.Exampleimport java.util.*; import java.util.function.*; public class FunctionTest {    public static void main(String args[]) {       String[] countries = {"India", "Australia", "England", "South Africa", "Srilanka", "Newzealand", "West Indies", "Scotland"};       Function converter = (all) -> {  // lambda expression          String names = "";       ... Read More

What are the constructor references in Java?

raja
Updated on 10-Jul-2020 13:55:56

345 Views

A constructor reference is just like a method reference except that the name of the method is "new". It can be created by using the "class name" and the keyword "new" with the following syntax.Syntax :: newIn the below example, we are using java.util.function.Function. It is a functional interface whose single abstract method is the apply(). The Function interface represents an operation that takes single argument T and returns a result R.Exampleimport java.util.function.*; @FunctionalInterface interface MyFunctionalInterface {    Employee getEmployee(String name); } class Employee {    private String name;    public Employee(String name) {       this.name = name;    }    public String ... Read More

How to implement the listeners using lambda expressions in Java?

raja
Updated on 10-Jul-2020 13:50:30

2K+ Views

When we are using a lambda expression for java listener, we do not have to explicitly implement the ActionListener interface. Instead, we can use the below syntax.Syntaxbutton.addActionListener(e -> { // some statements });An ActionListener interface defines only one method actionPerformed(). It is a functional interface which means that there's a place to use lambda expressions to replace the code.Exampleimport java.awt.*; import java.awt.event.*; import javax.swing.*; public class LambdaListenerTest extends JFrame {    public static void main(String args[]) {       new LambdaListenerTest();    }    private JButton button;    public ClickMeLambdaTest() {       setTitle("Lambda Expression Test");       button = ... Read More

How can we pass lambda expression in a method in Java?

raja
Updated on 10-Jul-2020 12:50:58

3K+ Views

A lambda expression passed in a method that has an argument of type of functional interface. If we need to pass a lambda expression as an argument, the type of parameter receiving the lambda expression argument must be of a functional interface type.In the below example, the lambda expression can be passed in a method which argument's type is "TestInterface". Exampleinterface TestInterface {    boolean test(int a); } class Test {    // lambda expression can be passed as first argument in the check() method    static boolean check(TestInterface ti, int b) {       return ti.test(b);    } } public class ... Read More

What kind of variables can we access in a lambda expression in Java?

raja
Updated on 11-Dec-2019 12:30:52

1K+ Views

The lambda expressions consist of two parts, one is parameter and another is an expression and these two parts have separated by an arrow (->) symbol. A lambda expression can access a variable of it's enclosing scope. A Lambda expression has access to both instance and static variables of it's enclosing class and also it can access local variables which are effectively final or final.Syntax( argument-list ) -> expressionExampleinterface TestInterface {    void print(); } public class LambdaExpressionTest {    int a; // instance variable    static int b; // static variable    LambdaExpressionTest(int x) {    // constructor to initialise instance variable       this.a = ... Read More

Fetch the maximum value from a MySQL column?

AmitDiwan
Updated on 11-Dec-2019 11:49:37

218 Views

Let us first create a table −mysql> create table DemoTable    -> (    -> Value int    -> ); Query OK, 0 rows affected (0.63 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(78); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable values(89); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values(98); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable values(58); Query OK, 1 row affected (0.25 sec)Display all records from the table using select statement −mysql> select *from DemoTable;This will produce the following output ... Read More

ORDER BY rand() and keep them grouped in MySQL?

AmitDiwan
Updated on 11-Dec-2019 11:48:38

276 Views

Let us first create a table −mysql> create table DemoTable    -> (    -> StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> StudentMarks int    -> ); Query OK, 0 rows affected (0.58 sec)Insert some records in the table using insert command. We have also inserted duplicate records −mysql> insert into DemoTable(StudentMarks) values(98); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable(StudentMarks) values(98); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable(StudentMarks) values(78); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable(StudentMarks) values(78); Query OK, 1 row affected (0.29 sec) mysql> insert ... Read More

Advertisements