Found 4747 Articles for Java 8

Non-generic Vs Generic Collection in Java

AmitDiwan
Updated on 14-Sep-2020 09:03:49
Generic CollectionErrors appear at compile time than at run time.Code reusability: Generics help in reusing the code already written, thereby making it usable for other types (for a method, or class, or an interface).If a data structure is generic, say a list, it would only take specific type of objects and return the same specific type of object as output. This eliminates the need to typecast individually.Algorithms can be implemented with ease, since they can be used to work with different types of objects, and maintaining type safety as well as code reusability.ExampleFollowing is an example − Live Demoimport java.util.*; public ... Read More

Fetching the name of the current thread in Java

AmitDiwan
Updated on 14-Sep-2020 09:01:34
The name of the current thread can be obtained using the below code −Example Live Demoimport java.io.*; class name_a_thread extends Thread {    @Override    public void run() {       System.out.println("Fetching the name of the current thread-");       System.out.println(Thread.currentThread().getName());    } } public class Demo {    public static void main (String[] args) {       name_a_thread thr_1 = new name_a_thread();       name_a_thread thr_2 = new name_a_thread();       thr_1.start();       thr_2.start();    } }OutputFetching the name of the current thread- Thread-0 Fetching the name of the current thread- Thread-1A class ... Read More

Naming a thread in Java

AmitDiwan
Updated on 14-Sep-2020 08:59:24
Java provides methods to change the name of the thread. These methods are defined in the ‘java.lang.Thread’ class. The name of the thread can be set directly by passing the name as an argument to the function. It has been demonstrated below −Example Live Demoimport java.io.*; class name_a_thread extends Thread {    name_a_thread(String name) {       super(name);    }    @Override    public void run() {       System.out.println("Thread is currently running ");    } } public class Demo {    public static void main (String[] args) {       name_a_thread thr_1 = new name_a_thread("Joe");     ... Read More

Method Overloading and null error in Java

AmitDiwan
Updated on 14-Sep-2020 08:55:40
When method is overloaded in Java, the functions have the same name, and the number of parameters to the function is same. In such cases, if the parameters are non-primitive and have the ability to accept null values, the compiler gets confused when the function is called with a null value, since it can’t choose either of them, because both of them have the ability to accept null values. This results in a compile time error.ExampleBelow is an example showing the same − Live Demopublic class Demo {    public void my_function(Integer i) {       System.out.println("The function with integer ... Read More

Multiset Interface – Java

AmitDiwan
Updated on 14-Sep-2020 08:53:29
Multiset is a collection in Java, that helps in order-independent equality, similar to Set structure. But the only difference is that a multiset can also contain duplicate elements.If multiset is visualized as a list, then this wouldn’t be the case since lists can’t hold duplicate values, and list elements are always in a specific order.Multiset can be thought of as a collection that lies somewhere between a list and a set structure. In multiset, the duplicates values are allowed, and there is no guarantee that the elements in a multiset would occur in a specific order. Multiset is also known ... Read More

MultiMap in Java

AmitDiwan
Updated on 14-Sep-2020 08:51:49
Multimap is a general method to bind the keys with random multiple values. The Multimap framework in Guava has methods that help in dealing with mapping keys to multiple values. Multimap can be visualized as a framework that −Is a collection of mapping from one key to one specific valueIs a collection of mapping from unique key to multiple values, i.e. collection of values.It can be implemented in places that use Map.Advantages of MultimapAn empty collection need not be populated before added a key value pair with the help of the function ‘put’.The ‘get’ method doesn’t return a null, except ... Read More

Multidimensional Collections in Java

AmitDiwan
Updated on 14-Sep-2020 08:48:08
Multidimensional collections are also known as Nested collections. It is a group of objects wherein every group has any number of objects that can be created dynamically. They can be stored in any position as well. In case of arrays, the user would be bound to a specific number of rows and columns, hence multidimensional structure helps create and add elements dynamically.Syntax of multidimensional arraylist in JavaArrayList object_name = new ArrayList();ExampleFollowing is an example of multidimensional collections in Java −Import java.util.*; public class Demo {    static List multi_dimensional() {       ArrayList x = new ArrayList();     ... Read More

Searching characters and substring in a String in Java

AmitDiwan
Updated on 14-Sep-2020 08:44:41
Following is the Java program to search characters and substring in a string −Example Live Demoimport java.io.*; import java.lang.*; public class Demo {    public static void main (String[] args) {       String test_str = "Hello";       CharSequence seq = "He";       boolean bool_1 = test_str.contains(seq);       System.out.println("Was the substring found? " + bool_1);       boolean bool_2 = test_str.contains("Lo");       System.out.println("Was the substring found? " + bool_2);    } }OutputWas the substring found? true Was the substring found? FalseA class named Demo contains the main function. Here, a string ... Read More

Local Variable Type Inference or LVTI in Java 10

AmitDiwan
Updated on 14-Sep-2020 08:36:17
Type inference in Java refers to the automatic detection of the variable’s datatype. This automatic detection usually happens at compile time. It is a feature of Java 10 and it allows the developer to skip declaring the type that is associated with the local variables. Local variables are those which are defined inside a method, initialization block, in for-loops, etc. The type would be usually identified by JDK.Upto Java 9, the following syntax was used to define local variable of a class type −class_name variable_name = new class_name(Arguments);This way, the type of the object would be specified on the right ... Read More

Java Program to Split the array and add the first part to the end

AmitDiwan
Updated on 14-Sep-2020 08:33:18
Following is the Java program to split the array and add the first part to the end −Example Live Demoimport java.util.*; import java.lang.*; public class Demo {    public static void split_array(int my_arr[], int arr_len, int k) {       for (int i = 0; i < k; i++) {          int x = my_arr[0];          for (int j = 0; j < arr_len - 1; ++j)          my_arr[j] = my_arr[j + 1];          my_arr[arr_len - 1] = x;       }    }    public static ... Read More
Advertisements