Found 4348 Articles for Java 8

Non-generic Vs Generic Collection in Java

AmitDiwan
Updated on 14-Sep-2020 09:03:49

1K+ Views

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

Method Overloading and null error in Java

AmitDiwan
Updated on 14-Sep-2020 08:55:40

600 Views

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

689 Views

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

226 Views

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

2K+ Views

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

519 Views

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

204 Views

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

191 Views

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

Java program to swap first and last characters of words in a sentence

AmitDiwan
Updated on 14-Sep-2020 08:31:31

1K+ Views

Following is the Java program to swap first and last characters of word in a sentence −Example Live Demopublic class Demo {    static String swap_chars(String my_str) {       char[] my_ch = my_str.toCharArray();       for (int i = 0; i < my_ch.length; i++) {          int k = i;          while (i < my_ch.length && my_ch[i] != ' ')             i++;             char temp = my_ch[k];             my_ch[k] = my_ch[i - 1];         ... Read More

Java program to merge two files into a third file

AmitDiwan
Updated on 14-Sep-2020 08:29:28

3K+ Views

Following is the Java program to merge two files into a third file −Exampleimport java.io.*; public class Demo {    public static void main(String[] args) throws IOException {       PrintWriter my_pw = new PrintWriter("path to third .txt file");       BufferedReader my_br = new BufferedReader(new FileReader("path to first .txt file"));       String my_line = my_br.readLine();       while (my_line != null) {          my_pw.println(my_line);          my_line = my_br.readLine();       }       my_br = new BufferedReader(new FileReader("path to second .txt file"));       my_line ... Read More

Advertisements