Found 2617 Articles for Java

Java Program for Anagram Substring Search

AmitDiwan
Updated on 04-Jul-2020 09:44:12

164 Views

Following is an example for Anagram Substring Search in Java −Example Live Demopublic class Demo{    static final int max_val = 256;    static boolean compare_vals(char my_arr_1[], char my_arr_2[]){       for (int i = 0; i < max_val; i++)       if (my_arr_1[i] != my_arr_2[i])       return false;       return true;    }    static void search_subs(String my_pattern, String my_text){       int pat_len = my_pattern.length();       int txt_len = my_text.length();       char[] count_pat = new char[max_val];       char[] count_txt = new char[max_val];       for ... Read More

Java Program for Comb Sort

AmitDiwan
Updated on 04-Jul-2020 09:41:16

178 Views

The Comb Sort in Java eliminates the smaller values situated to the end of the list and the inversions are removed oby by one. Let us see an example −Example Live Demoimport java.util.Arrays; public class Demo{    void comb_sort(int nums[]){       int len_gap = nums.length;       float shrink_val = 1.3f;       boolean swap = false;       while (len_gap > 1 || swap) {          if (len_gap > 1) {             len_gap = (int)(len_gap / shrink_val);          }          swap ... Read More

Java Program for Counting Sort

AmitDiwan
Updated on 04-Jul-2020 09:39:15

125 Views

The Counting Sort counts the number of objects having distinct key values. Let us see an example −Note − The below code can be used with negative numbers as well.Example Live Demoimport java.util.*; public class Demo{    static void count_sort(int[] arr){       int max_val = Arrays.stream(arr).max().getAsInt();       int min_val = Arrays.stream(arr).min().getAsInt();       int range = max_val - min_val + 1;       int count[] = new int[range];       int result[] = new int[arr.length];       for (int i = 0; i < arr.length; i++){          count[arr[i] - min_val]++; ... Read More

Java Program for Binary Insertion Sort

AmitDiwan
Updated on 02-Jul-2024 17:29:47

3K+ Views

Binary insertion sort uses the binary search to find the right position to insert an element at a specific index at every iteration. First, the location where the element needs to be inserted is found. Then, the elements are shifted to the next right location. Now, the specific element is placed in the position. Problem Statement Given an integer of array implement binary insertion sort using Java to sort it. The algorithm should use binary search to determine the correct position for each element and to maintain the sorted order it will shift elements accordingly. Steps for Binary Insertion ... Read More

Java Program for Cocktail Sort

AmitDiwan
Updated on 04-Jul-2020 09:29:41

212 Views

Cocktail Sort works in contrast to bubble sort, wherein elements are iterated from left to right, and the largest element is first brought to its correct position and so on. In cocktail sort, elements are iterated over in both the directions (left and right) in an alternating fashion.Following is the program for Cocktail Sort −Example Live Demopublic class Demo{    static int temp;    static void Cocktail(int a[], int n){       boolean swap = true;       int begin = 0, i;       int end = n - 1;       while (swap) {   ... Read More

Java Numeric Promotion in Conditional Expression

AmitDiwan
Updated on 04-Jul-2020 09:20:21

96 Views

The conditional operator (? :) leverages the output of one value (which is a bool) to decide which expression has to be evaluated next. Let us see an example −Example Live Demoimport java.io.*; public class Demo{    public static void main (String[] args){       Object my_obj = true ? new Integer(91) : new Float(89);       System.out.println(my_obj);    } }Output91.0A class named Demo contains the main function. Here, an object instance is defined and if it is true, an integer value is displayed otherwise a float value is displayed. Next, they are printed on the console.When promotional expression ... Read More

Java multiplyExact() in Math

AmitDiwan
Updated on 10-Aug-2023 12:10:00

184 Views

In Java, multiplyExact() is an in-built static method of the Math class that accepts two arguments and returns their product as a result. This method can accept values of either integer or long type. One important point to note is that it may throw an exception if the produced result overflows the size of integer. After knowing the functionality of multiplyExact() in Java, one question that may pop into one's mind is that we can also use '*' operator to multiply two operands then why do we need multiplyExact() method. We are going to answer this question and also, explore ... Read More

Java Lambda Expression with Collections

AmitDiwan
Updated on 04-Jul-2020 09:16:54

666 Views

Sorting the elements of a list using lambda expression −Example Live Demoimport java.util.*; public class Demo{    public static void main(String[] args){       ArrayList my_arr = new ArrayList();       my_arr.add(190);       my_arr.add(267);       my_arr.add(12);       my_arr.add(0);       System.out.println("Before sorting, elements in the array list are : " + my_arr);       Collections.sort(my_arr, (o1, o2) -> (o1 > o2) ? -1 : (o1 < o2) ? 1 : 0);       System.out.println("After sorting, elements in the array list are : " + my_arr);    } }OutputBefore sorting, elements ... Read More

Java Interview Questions on Constructors

AmitDiwan
Updated on 10-Aug-2023 12:08:16

495 Views

In most of the Java interviews, the interviewers always start by asking basic questions. They can test one's knowledge in just a few minutes of the interviews. Therefore, it is essential to get thorough with the fundamental concepts of Java such as class, object and constructor. In this article, we are going to discuss interesting interview questions related to constructors. It will not just help to clear the job interviews but also improve one's knowledge as well as skills. Java Interview Questions on Constructor There could be numerous interview questions on Constructors, it is not possible to cover all ... Read More

Java ConcurrentHashMap - clear()

AmitDiwan
Updated on 04-Jul-2020 09:06:38

234 Views

The clear function is used to clear up the mapping between the key value pairs. This way, the ConcurrentHashMap mappings would be cleared.Syntaxpublic void clear()Let us see an example −Example Live Demoimport java.util.concurrent.ConcurrentHashMap; import java.util.*; public class Demo{    public static void main(String[] args){       Map my_map = new ConcurrentHashMap();       my_map.put("This", "35");       my_map.put("is", "78");       my_map.put("sample", "99");       System.out.println("The map contains the below elements " + my_map);       my_map.clear();       System.out.println("The elements after the clear function is called on it " + my_map);    } ... Read More

Advertisements