Programming Articles - Page 2916 of 3363

Replace All Elements Of ArrayList with with Java Collections

Chandu yadav
Updated on 25-Jun-2020 14:34:55

2K+ Views

In order to replace all elements of ArrayList with Java Collections, we use the Collections.fill() method. The static void fill(List list, Object element) method replaces all elements in the list with the specified element in the argument.Declaration −The java.util.Collections.fill() method is declared as follows −public static void fill(List

Perform Binary Search on ArrayList with Java Collections

George John
Updated on 25-Jun-2020 14:35:34

2K+ Views

In order to perform Binary Search on ArrayList with Java Collections, we use the Collections.binarySearch() method.Declaration −The java.util.Collections.binarySearch() method is declared as follows −public static int binarySearch(List list, T key)The above method returns the position of the key in the list sorted in ascending order. If we use a Comparator c to sort the list, the binarySearch() method will be declared as follows −public static int binarySearch(List list, T key, Comparator c)If key is not present, the it returns ((insertion point) + 1) *(-1).Let us see a program to perform binarySearch() on ArrayList −Example Live Demoimport java.util.*; public class Example { ... Read More

Compare two short arrays in a single line in Java

Ankith Reddy
Updated on 25-Jun-2020 14:36:14

339 Views

Two short arrays can be compared in Java using the java.util.Arrays.equals() method. This method returns true if the arrays are equal and false otherwise. The two arrays are equal if they contain the same number of elements in the same order.A program that compares two short arrays using the Arrays.equals() method is given as follows −Example Live Demoimport java.util.Arrays; public class Demo {    public static void main(String[] argv) throws Exception {       boolean flag = Arrays.equals(new short[] { 2, 8, 5 }, new short[] { 2, 8, 5 });       System.out.println("The two short arrays are equal? ... Read More

Get Enumeration over ArrayList with Java Collections

Arjun Thakur
Updated on 25-Jun-2020 14:36:46

636 Views

In order to get enumeration over ArrayList with Java Collections, we use the java.util.Collections.enumeration() method.Declaration −The java.util.Collections.enumeration() method is declared is as follows −public static Enumeration enumeration(Collection c)where c is the collection object for which an enumeration is returnedLet us see a program to get enumeration over ArrayList −Example Live Demoimport java.util.*; public class Example {    public static void main (String[] args) {       ArrayList list = new ArrayList();       list.add(14);       list.add(2);       list.add(73);       Enumeration en = Collections.enumeration(list); // getting enumeration over ArrayList list       ... Read More

Find minimum element of ArrayList with Java Collections

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

4K+ 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

472 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

5K+ 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

371 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

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

Advertisements