Found 4336 Articles for Java 8

Find minimum element of ArrayList with Java Collections

Chandu yadav
Updated on 25-Jun-2020 14:37:35

3K+ Views

In order to compute minimum element of ArrayList with Java Collections, we use the Collections.min() method. The java.util.Collections.min() returns the minimum element of the given collection. All elements must be mutually comparable and implement the comparable interface. They shouldn’t throw a ClassCastException.Declaration −The Collections.min() method is declared as follows −public static T min(Collection c)where c is the collection object whose minimum is to be found.Let us see a program to find the minimum element of ArrayList with Java collections −Example Live Demoimport java.util.*; public class Example {    public static void main (String[] args) {       List list ... Read More

Copy Elements of One ArrayList to Another ArrayList with Java Collections Class

Arjun Thakur
Updated on 25-Jun-2020 14:38:08

8K+ Views

In order to copy elements of ArrayList to another ArrayList, we use the Collections.copy() method. It is used to copy all elements of a collection into another.Declaration −The java.util.Collections.copy() method is declared as follows −public static void copy(List

How to copy elements of ArrayList to Java Vector

Chandu yadav
Updated on 25-Jun-2020 14:39:50

293 Views

In order to copy elements of ArrayList to Java Vector, we use the Collections.copy() method. It is used to copy all elements of a collection into another.Declaration −The java.util.Collections.copy() method is declared as follows −public static void copy(List

Sort Elements in an ArrayList in Java

George John
Updated on 25-Jun-2020 14:40:21

4K+ Views

In order to sort elements in an ArrayList in Java, we use the Collections.sort() method in Java. This method sorts the elements available in the particular list of the Collection class in ascending order.Declaration −The java.util.Collections.sort() method is declared as follows −public static void sort(List list)where list is an object on which sorting needs to be performed.Let us see a program to sort elements in an ArrayList in Java −Example Live Demoimport java.util.*; public class Example {    public static void main (String[] args) {       List zoo = new ArrayList();       zoo.add("Zebra");       zoo.add("Lion"); ... Read More

Dump the content of an array in Java

Ankith Reddy
Updated on 25-Jun-2020 14:40:53

238 Views

An array can be easily printed by using the method java.util.Arrays.toString() in Java. This method returns a string representation of the array contents that is enclosed in square brackets. If the array is null, then this method returns null.A program that demonstrates this is given as follows −Example Live Demoimport java.util.Arrays; public class Demo {    public static void main(String args[]) {       String str[] = {"John", "Harry", "Sally", "Emma", "Peter"};       System.out.println("The array content is:");       System.out.println(Arrays.toString(str));    } }OutputThe array content is: [John, Harry, Sally, Emma, Peter]Now let us understand the above program.The ... Read More

Java Program to sort an array in case-insensitive order

George John
Updated on 25-Jun-2020 14:42:02

2K+ Views

An array can be sorted in case-insensitive order using the java.util.Arrays.sort() method. Also the java.text.Collator class is required as Collator.getInstance() is used to obtain the Collator object for the required locale.A program that demonstrates this is given as follows −Example Live Demoimport java.text.Collator; import java.util.Arrays; public class Demo {    public static void main(String args[]) {       String[] arr = new String[] { "apple", "mango", "Banana", "Melon", "orange" };       System.out.print("The unsorted array is: ");       System.out.println(Arrays.toString(arr));       Arrays.sort(arr, Collator.getInstance());       System.out.print("The sorted array in case-insensitive order is: ");     ... Read More

Get Synchronized List from ArrayList in java

Ankith Reddy
Updated on 12-Mar-2024 17:51:24

3K+ Views

In order to get a synchronized list from an ArrayList, we use the synchronizedList(List ) method in Java. The Collections.synchronizedList(List ) method accepts the ArrayList as an argument and returns a thread safe list.Declaration −The Collections.synchronizedList(List ) method is declared as follows −public static List synchronizedList(List list)Let us see a program to get a synchronized List from ArrayList −Example import java.util.*; public class Example { public static void main (String[] args) { List list = new ArrayList(); list.add("Hello"); ... Read More

How to schedule tasks in Java to run for repeated fixed-delay execution, beginning after the specified delay

Arjun Thakur
Updated on 25-Jun-2020 14:43:41

991 Views

One of the methods in the Timer class is the void schedule(TimerTask task, long delay, long period) method. This method schedules tasks for repeated fixed-delay execution, beginning after the specified delay.In fixed-delay execution, each execution is scheduled with respect to the original execution time of the preceding execution. If an execution is delayed for a particular reason (case in point, garbage collection), the subsequent executions will be delayed as well.Declaration −The java.util.Timer.schedule(TimerTask task, long delay, long period) is declared as follows −public void schedule(TimerTask task, long delay, long period)Here, task is the task to be scheduled, delay is the delay ... Read More

Java Program to implement Binary Search on double array

Arjun Thakur
Updated on 25-Jun-2020 14:45:34

219 Views

Binary search on a double array can be implemented by using the methodjava.util.Arrays.binarySearch(). This method returns the index of the required double element if it is available in the array, otherwise it returns (-(insertion point) - 1) where the insertion point is the position at which the element would be inserted into the array.A program that demonstrates this is given as follows −Example Live Demoimport java.util.Arrays; public class Demo {    public static void main(String[] args) {       double d_arr[] = { 5.2, 7.5, 9.7, 1.8, 4.0 };       Arrays.sort(d_arr);       System.out.print("The sorted array is: ... Read More

Check if the String contains only unicode letters, digits or space in Java

Chandu yadav
Updated on 25-Jun-2020 14:47:31

740 Views

To check if a given String contains only unicode letters, digits or space, we use the isLetterOrDigit() and charAt() methods with decision making statements.The isLetterOrDigit(char ch) method determines whether the specific character (Unicode ch) is either a letter or a digit. It returns a boolean value, either true or false.Declaration −The java.lang.Character.isLetter() method is declared as follows −public static boolean isLetter(char ch)The charAt() method returns a character value at a given index. It belongs to the String class in Java. The index must be between 0 to length()-1.Declaration −The java.lang.String.charAt() method is declared as follows −public char charAt(int index)Let us ... Read More

Advertisements