Found 34494 Articles for Programming

Find max and min values in array of primitives using Java

Ankith Reddy
Updated on 26-Jun-2020 07:37:59

547 Views

This example shows how to search the minimum and maximum element in an array by using Collection.max() and Collection.min() methods of Collection class.Example Live Demoimport java.util.Arrays; import java.util.Collections; public class Main {    public static void main(String[] args) {       Integer[] numbers = { 8, 2, 7, 1, 4, 9, 5};       int min = (int) Collections.min(Arrays.asList(numbers));       int max = (int) Collections.max(Arrays.asList(numbers));       System.out.println("Min number: " + min);       System.out.println("Max number: " + max);    } }OutputThe above code sample will produce the following result.Min number: 1 Max number: 9Another ... Read More

Find frequency of each word in a string in Java

Chandu yadav
Updated on 26-Jun-2020 07:38:47

1K+ Views

In order to get frequency of each in a string in Java we will take help of hash map collection of Java.First convert string to a character array so that it become easy to access each character of string.Now compare for each character that whether it is present in hash map or not in case it is not present than simply add it to hash map as key and assign one as its value.And if character is present than find its value which is count of occurrence of this character in the string (initial we put as 1 when it ... Read More

Fibonacci of large number in java

George John
Updated on 26-Jun-2020 07:39:23

514 Views

Fibonacci numbers of Fibonacci series grows exponentially and can be very large for large numbers like 500 or 1000. To handle such number, long data type is not sufficient. BigInteger can handle large number easily. BigInteger is useful in scenarios where calculations results in data which is out of limit for available primitive data types. See the example below of getting Fibonacci number of 100 and 1000.Example Live Demoimport java.math.BigInteger; public class Tester {    public static void main(String args[]) {       System.out.println("Fibonacci of 100: ");       System.out.println(fibonacci(100));       System.out.println("Fibonacci of 1000: ");     ... Read More

Does JVM creates object of Main class in Java?

Ankith Reddy
Updated on 26-Jun-2020 07:40:55

388 Views

As we know that Java needs main() method to be static in the public class to make it executable. The prime reason for this requirement is to make JVM enable to call the main() method without creating an object. That simply means that JVM does not create the object of the Main class which contains the main() method. To justify the same, we can make the Main class containing the main method as abstract and program still runs.Following example showcases the same. Here we have made the main class abstract.Exampleabstract public class Tester {    public static void main(String args[]) ... Read More

Difference between x++ and x= x+1 in Java programming

Arjun Thakur
Updated on 26-Jun-2020 07:41:18

538 Views

x++ automatically handles the type casting where as x= x + 1 needs typecasting in case of x is not an int variable. See the example below.Example Live Demopublic class Tester {    public static void main(String args[]) {       byte b = 2;       //Type casting is required       //as 1 is int and b is byte variable       b = (byte) (b + 1);       System.out.println(b);       byte b1 = 2;       //Implcit type casting by the compiler       b1++;       System.out.println(b1);    } }Output3 3

Difference between TreeMap, HashMap and LinkedHashMap in Java programming

George John
Updated on 26-Jun-2020 07:25:35

505 Views

HashMap, TreeMap and LinkedHashMap all implements java.util.Map interface and following are their characteristics.HashMapHashMap has complexity of O(1) for insertion and lookup.HashMap allows one null key and multiple null values.HashMap does not maintain any order.TreeMapTreeMap has complexity of O(logN) for insertion and lookup.TreeMap does not allow null key but allow multiple null values.TreeMap maintains order. It stores keys in sorted and ascending order.LinkedHashMapLinkedHashMap has complexity of O(1) for insertion and lookup.LinkedHashMap allows one null key and multiple null values.LinkedHashMap maintains order in which key-value pairs are inserted.Example Live Demoimport java.util.HashMap; import java.util.Hashtable; import java.util.LinkedHashMap; import java.util.Map; import java.util.TreeMap; public class Tester { ... Read More

How to create your own helper class in Java?

Arjun Thakur
Updated on 26-Jun-2020 07:26:54

4K+ Views

A helper class serve the following purposes.Provides common methods which are required by multiple classes in the project.Helper methods are generally public and static so that these can be invoked independently.Each methods of a helper class should work independent of other methods of same class.Following example showcases one such helper class.Examplepublic class Tester {    public static void main(String[] args) {       int a = 37;       int b = 39;       System.out.println(a + " is prime: " + Helper.isPrime(a));       System.out.println(b + " is prime: " + Helper.isPrime(b));    } } ... Read More

How to create the immutable class in Java?

George John
Updated on 26-Jun-2020 07:27:35

224 Views

An immutable class object's properties cannot be modified after initialization. For example String is an immutable class in Java. We can create a immutable class by following the given rules below.Make class final − class should be final so that it cannot be extended.Make each field final − Each field should be final so that they cannot be modified after initialization.Create getter method for each field. − Create a public getter method for each field. fields should be private.No setter method for each field. − Do not create a public setter method for any of the field.Create a parametrized constructor ... Read More

Count the Number of matching characters in a pair of Java string

Chandu yadav
Updated on 26-Jun-2020 07:28:27

2K+ Views

In order to find the count of matching characters in two Java strings the approach is to first create character arrays of both the strings which make comparison simple.After this put each unique character into a Hash map.Compare each character of other string with created hash map whether it is present or not in case if present than put that character into other hash map this is to prevent duplicates.In last get the size of this new created target hash map which is equal to the count of number of matching characters in two given strings.Example Live Demoimport java.util.HashMap; public class ... Read More

CopyOnWriteArrayList Class in Java programming

Ankith Reddy
Updated on 26-Jun-2020 07:29:45

171 Views

Class declarationpublic class CopyOnWriteArrayList extends Object implements List, RandomAccess, Cloneable, SerializableCopyOnWriteArrayList is a thread-safe variant of Arraylist where operations which can change the arraylist (add, update, set methods) creates a clone of the underlying array.CopyOnWriteArrayList is to be used in Thread based environment where read operations are very frequent and update operations are rare.Iterator of CopyOnWriteArrayList will never throw ConcurrentModificationException.Any type of modification to CopyOnWriteArrayList will not reflect during iteration since the iterator was created.List modification methods like remove, set and add are not supported in iteration. These method will throw UnsupportedOperationException.null can be added to the list.CopyOnWriteArrayList MethodsFollowing is ... Read More

Advertisements