Category Archives: Java

java.util.Arrays.copyof() in Java with examples

java.util.Arrays.copyof() method is in java.util.Arrays class. It copies the specified array, truncating or padding with false (if necessary) so the copy has the specified length. Syntax: copyOf(int[] original, int newLength) original – original array newLength – copy of original array Output: Original Array 1 2 3 New array copy after modifications: 1 2 3 11… Read More »

Collections.reverseOrder() in Java with Examples

java.util.Collections.reverseOrder() method is a java.util.Collections class method. // Returns a comparator that imposes the reverse of // the natural ordering on a collection of objects // that implement the Comparable interface. // The natural ordering is the ordering imposed by // the objects’ own compareTo method public static Comparator reverseOrder() We can the comparator returned… Read More »

Collections.shuffle() in Java with Examples

java.util.Collections.shuffle() is a java.util.Collections class method. // Shuffles mylist public static void shuffle(List mylist) This method throws UnsupportedOperationException if the given list or its list-iterator does not support the set operation. Output: Original List : [code, quiz, geeksforgeeks, quiz, practice, qa] Shuffled List : [qa, quiz, practice, code, quiz, geeksforgeeks]   shuffle(mylist, rndm) It shuffles… Read More »

ArrayList to Array Conversion in Java : toArray() Methods

Following methods can be used for converting ArrayList to Array: Method 1: Using Object[] toArray() method Syntax: public Object[] toArray() It is specified by toArray in interface Collection and interface List It overrides toArray in class AbstractCollection It returns an array containing all of the elements in this list in the correct order. Output: 10… Read More »

Deque interface in Java with Example

The java.util.Deque interface is a subtype of the java.util.Queue interface. The Deque is related to the double-ended queue that supports addition or removal of elements from either end of the data structure, it can be used as a queue (first-in-first-out/FIFO) or as a stack (last-in-first-out/LIFO). Methods of deque: add(element): Adds an element to the tail.… Read More »

Collections.sort() in Java with Examples

java.util.Collections.sort() method is present in java.util.Collections class. It is used to sort the elements present in the specified list of Collection in ascending order. It works similar to java.util.Arrays.sort() method but it is better then as it can sort the elements of Array as well as linked list, queue and many more present in it.… Read More »

NavigableMap Interface in Java with Example

NavigableMap is an extension of SortedMap which provides convenient navigation method like lowerKey, floorKey, ceilingKey and higherKey, and along with these popular navigation method it also provide ways to create a Sub Map from existing Map in Java e.g. headMap whose keys are less than specified key, tailMap whose keys are greater than specified key… Read More »