Programming Articles - Page 2234 of 3363

Type Inference in Lambda expression in Java?

raja
Updated on 11-Jul-2020 06:05:19

1K+ Views

Type Inference states that the data type of any expression. For instance, a method return type or argument type can be understood automatically by the compiler. Types in the argument list can be omitted since java already know the types of the expected parameters for the single abstract method of functional interface.Syntax(var1, var2 …) -> { method body }In the below example, we can sort a String[] array by its last character.Exampleimport java.util.Arrays; public class TypeInferencingLambdaTest {    public static void main(String[] args) {       String[] names = {"Raja", "Jai", "Adithya", "Surya", "Chaitanya", "Ravi", "Krishna"};       Arrays.sort(names, (s1, ... Read More

Is it mandatory to mark a functional interface with @FunctionalInterface annotation in Java?

raja
Updated on 10-Jul-2020 14:11:42

2K+ Views

An interface defined with only one abstract method is known as Functional Interface. It's not mandatory to mark the functional interface with @FunctionalInterface annotation, the compiler doesn't throw any error. But it’s good practice to use @FunctionalInterface annotation to avoid the addition of extra methods accidentally. If an interface annotated with @FunctionalInterface annotation and more than one abstract method then it throws a compile-time error.Syntax@FunctionalInterface interface interface_name {    // only one abstarct method declaration }Example@FunctionalInterface interface Shape {    void printArea(int x); } public class SquareTest {    public static void main (String args[]) {       Shape square ... Read More

Total number of elements in a specified dimension of an Array in C#

AmitDiwan
Updated on 16-Dec-2019 10:28:34

139 Views

To get the total number of elements in a specified dimension of an array, the code is as follows −Example Live Demousing System; public class Demo {    public static void Main() {       string[] products = new string[] { "Andy", "Mark", "Gary", "Andre"};       Console.WriteLine("One or more name begins with 'A'? = {0}",       Array.Exists(products, ele => ele.StartsWith("A")));       Console.WriteLine("Is the array having fixed size? = " + products.IsFixedSize);       Console.WriteLine("Is the array read only? = " + products.IsReadOnly);       Console.WriteLine("Is the array synchronized? = " + products.IsSynchronized); ... Read More

Setting the capacity to the actual number of elements in a SortedList object in C#?

AmitDiwan
Updated on 16-Dec-2019 12:37:04

154 Views

To set the capacity to the actual number of elements in a SortedList object, the code is as follows −Example Live Demousing System; using System.Collections; public class Demo {    public static void Main(String[] args) {       SortedList sortedList = new SortedList();       sortedList.Add("A", "1");       sortedList.Add("B", "2");       sortedList.Add("C", "3");       sortedList.Add("D", "4");       sortedList.Add("E", "5");       sortedList.Add("F", "6");       sortedList.Add("G", "7");       sortedList.Add("H", "8");       sortedList.Add("I", "9");       sortedList.Add("J", "10");       Console.WriteLine("SortedList elements...");     ... Read More

Set the capacity to the actual number of elements in the ArrayList in C#?

AmitDiwan
Updated on 16-Dec-2019 10:18:04

139 Views

To set the capacity to the actual number of elements in the ArrayList, the code is as follows −Example Live Demousing System; using System.Collections; public class Demo {    public static void Main(String[] args) {       ArrayList list1 = new ArrayList();       list1.Add("A");       list1.Add("B");       list1.Add("C");       list1.Add("D");       list1.Add("E");       list1.Add("F");       list1.Add("G");       list1.Add("H");       list1.Add("I");       Console.WriteLine("Elements in ArrayList1...");       foreach (string res in list1) {          Console.WriteLine(res);     ... Read More

Search for an element matching the conditions and return the zero-based index of the last occurrence within the entire List in C#

AmitDiwan
Updated on 16-Dec-2019 10:13:38

227 Views

To search for an element matching the conditions and return the zero-based index of the last occurrence within the entire List, the code is as follows −Example Live Demousing System; using System.Collections.Generic; public class Demo {    private static bool demo(int i) {       return ((i % 10) == 0);    }    public static void Main(String[] args) {       List list = new List();       list.Add(200);       list.Add(215);       list.Add(310);       list.Add(500);       list.Add(600);       Console.WriteLine("List elements...");       foreach (int i in ... Read More

Searching the index of specified object in Collection in C#

AmitDiwan
Updated on 16-Dec-2019 10:10:07

170 Views

To search the index of specified object in Collection, the code is as follows −Example Live Demousing System; using System.Collections.Specialized; public class Demo {    public static void Main() {       StringCollection strCol = new StringCollection();       strCol.Add("Accessories");       strCol.Add("Books");       strCol.Add("Electronics");       strCol.Add("Books");       Console.WriteLine("StringCollection elements...");       foreach (string res in strCol) {          Console.WriteLine(res);       }       strCol.Insert(2, "Headphone");       Console.WriteLine("StringCollection elements...UPDATED");       foreach (string res in strCol) {          Console.WriteLine(res); ... Read More

Number of elements contained in the BitArray in C#?

AmitDiwan
Updated on 16-Dec-2019 10:04:35

114 Views

To get the number of elements contained in the BitArray, the code is as follows −Example Live Demousing System; using System.Collections; public class Demo {    public static void Main() {       BitArray arr1 = new BitArray(5);       BitArray arr2 = new BitArray(5);       arr1[0] = false;       arr1[1] = false;       arr2[0] = false;       arr2[1] = true;       Console.WriteLine("BitArray1 elements...");       foreach (bool res in arr1) {          Console.WriteLine(res);       }       Console.WriteLine("BitArray2 elements...");     ... Read More

How to check whether a thread is alive or not in C#

AmitDiwan
Updated on 16-Dec-2019 10:00:54

612 Views

To check whether a thread is alive or not, the code is as follows −Example Live Demousing System; using System.Threading; public class Demo {    public static void Main() {       Thread thread = new Thread(new ThreadStart(demo1));       thread = Thread.CurrentThread;       Console.WriteLine("Is the thread alive? = "+thread.IsAlive);       ThreadPool.QueueUserWorkItem(new WaitCallback(demo2));       Console.WriteLine("Current state of Thread = "+thread.ThreadState);       Console.WriteLine("ManagedThreadId = "+thread.ManagedThreadId);       thread.IsBackground = true;       Console.WriteLine("Is the Thread a background thread? = "+thread.IsBackground);    }    public static void demo1() {     ... Read More

Search in a SortedList object in C#

AmitDiwan
Updated on 16-Dec-2019 09:57:40

221 Views

To search in a SortedList object, the code is as follows −Example Live Demousing System; using System.Collections; public class Demo {    public static void Main() {       SortedList list = new SortedList();       list.Add("1", "One");       list.Add("2", "Two");       list.Add("3", "Three");       list.Add("4", "Four");       list.Add("5", "Five");       list.Add("6", "Six");       list.Add("7", "Seven");       list.Add("8", "Eight");       Console.WriteLine("Key and Value of SortedList....");       foreach(DictionaryEntry k in list )       Console.WriteLine("Key: {0}, Value: {1}", k.Key , k.Value ... Read More

Advertisements