Programming Articles - Page 2913 of 3363

Create and demonstrate an immutable collection in Java

Anvi Jain
Updated on 29-Jun-2020 06:41:47

232 Views

In order to create and demonstrate an immutable collection in Java, we use the unmodifiableCollection() method. This method returns an unmodifiable and immutable view of the collection.Declaration − The java.util.Collections.unmodifiableCollection() method is declared as follows -public static Collection unmodifiableCollection(Collection

Sort items of an ArrayList with Collections.reverseOrder() in Java

Smita Kapse
Updated on 25-Jun-2020 13:19:27

576 Views

In order to sort items of an ArrayList with Collections.reverseOrder() in Java, we need to use the Collections.reverseOrder() method which returns a comparator which gives the reverse of the natural ordering on a collection of objects that implement the Comparable interface.Declaration − The java.util.Collections.reverseOrder() method is declared as follows -public static Comparator reverseOrder()Let us see a program to sort an ArrayList with Collections.reverseOrder() in Java -Example Live Demoimport java.util.*; public class Example {    public static void main (String[] args) {       ArrayList list = new ArrayList();       list.add(10);       list.add(50);       ... Read More

Rotate elements of a collection in Java

Nancy Den
Updated on 25-Jun-2020 13:19:58

437 Views

To rotate elements of a collection in Java, we use the Collections.rotate() method. The rotate method rotates the elements specified in the list by a specified distance. When this method is invoked, the element at index x will be the element previously at index (x - distance) mod list.size(), for all values of i between 0 and list.size()-1, inclusive.Declaration − The java.util.Collections.rotate() is declared as follows -public static void rotate(List list, int distance)Let us see a program to rotate elements of a collection in Java -Example Live  Demoimport java.util.*; public class Example {    public static void main(String[] args) {   ... Read More

Java program to fill elements in a long array

Krantik Chavan
Updated on 05-Nov-2024 22:03:17

593 Views

In this article, we will learn how to fill elements in a long array in Java using the Arrays.fill() method. This method allows us to assign a specific long value to all elements of the array. The Arrays.fill() function is a simple way to initialize or overwrite the values in an array. It requires two parameters: the array to be filled and the value to be stored in each element. Problem StatementGiven a long array of size 5, write a Java program to fill each element of the array with the value 125 using the Arrays.fill() method.Input longArray = new ... Read More

Sort ArrayList in Descending order using Comparator with Java Collections

Anvi Jain
Updated on 29-Jun-2020 06:42:21

5K+ Views

In order to sort ArrayList in Descending order using Comparator, we need to use the Collections.reverseOrder() method which returns a comparator which gives the reverse of the natural ordering on a collection of objects that implement the Comparable interface.Declaration − The java.util.Collections.reverseOrder() method is declared as follows -public static Comparator reverseOrder()Let us see a program to sort an ArrayList in Descending order using Comparator  with Java Collections −Example Live Demoimport java.util.*; public class Example {    public static void main (String[] args) {       ArrayList list = new ArrayList();       list.add(10);       list.add(50);     ... Read More

List the Interfaces That a Class Implements in Java

Krantik Chavan
Updated on 25-Jun-2020 12:26:04

703 Views

The interfaces that are implemented by a class that is represented by an object can be determined using the java.lang.Class.getInterfaces() method. This method returns an array of all the interfaces that are implemented by the class.A program that demonstrates this is given as follows −Example Live Demopackage Test; import java.lang.*; import java.util.*; public class Demo {    public static void main(String[] args) {       listInterfaces(String.class);    }    public static void listInterfaces(Class c) {       System.out.println("The Class is: " + c.getName());       Class[] interfaces = c.getInterfaces();       System.out.println("The Interfaces are: " + Arrays.asList(interfaces)); ... Read More

Validate the first name and last name with Java Regular Expressions

George John
Updated on 25-Jun-2020 13:25:18

6K+ Views

In order to match the first name and last name using regular expression, we use the matches method in Java. The java.lang.String.matches() method returns a boolean value which depends on the matching of the String with the regular expression.Declaration −The java.lang.String.matches() method is declared as follows −public boolean matches(String regex)Let us see a program to validate the first name and last name with regular expressions −Example Live Demopublic class Example {    public static void main( String[] args ) {       System.out.println(firstName("Tom"));       System.out.println(lastName("hanks"));    }    // validate first name    public static boolean firstName( String ... Read More

Using reflection to check array type and length in Java

Ankith Reddy
Updated on 25-Jun-2020 13:27:34

2K+ Views

The array type can be checked using the java.lang.Class.getComponentType() method. This method returns the class that represents the component type of the array. The array length can be obtained in int form using the method java.lang.reflect.Array.getLength().A program that demonstrates this is given as follows −Example Live Demoimport java.lang.reflect.Array; public class Demo {    public static void main (String args[]) {       int[] arr = {6, 1, 9, 3, 7};       Class c = arr.getClass();       if (c.isArray()) {          Class arrayType = c.getComponentType();          System.out.println("The array is of type: ... Read More

Create new instance of a Two-Dimensional array with Java Reflection Method

Arjun Thakur
Updated on 25-Jun-2020 13:47:13

2K+ Views

A new instance of a two dimensional array can be created using the java.lang.reflect.Array.newInstance() method. This method basically creates the two-dimensional array with the required component type as well as length.A program that demonstrates the creation of a two-dimensional array using the Array.newInstance() method is given as follows −Example Live Demoimport java.lang.reflect.Array; public class Demo {    public static void main (String args[]) {       int size[] = {3, 3};       int arr[][] = (int[][])Array.newInstance(int.class, size);       System.out.println("The two-dimensional array is:");       for(int[] i: arr) {          for(int j: i ... Read More

Get the fully-qualified name of an inner class in Java

Chandu yadav
Updated on 25-Jun-2020 13:47:50

6K+ Views

A fully-qualified class name in Java contains the package that the class originated from. Also, an inner class is a class that is another class member. So, the fully-qualified name of an inner class can be obtained using the getName() method.A program that demonstrates this is given as follows −Example Live Demopublic class Main {    public static void main(String[] argv) throws Exception {       Class c = java.lang.Character.Subset.class;       String innerClassName = c.getName();       System.out.println("The fully qualified name of the inner class is: " + innerClassName);    } }OutputThe fully qualified name of the ... Read More

Advertisements