Home » Core Java » util » Collections

Collections

Java Collections Sort Example

In this example, I will show how to use the java.util.Collections class in order to perform sorting. The Collections class consists of static methods that return or operate on collections, such as Lists, Maps, Sets, etc. All the methods throw a NullPointerException if the collection(s) passed as a parameter is/are null. ArraySortExample Create a class named ArraySortExample with the following ...

Read More »

java.util.Collections Example

In this article we will discuss about the Collections class and some of its important methods. The class java.util.Collections is a utility class that resides in java.util package, it consists entirely of static methods which are used to operate on collections like List, Set. Common operations like sorting a List or finding an element from a List can easily be done ...

Read More »

Swap List elements example

This is an example of how to swap a List’s elements . We are using the swap(List list, int i, int j) method of the Collections Class. Collections provides static methods that operate on or return collections. We are also using the ArrayList as a List implementation, but the same API applies to any type of List implementation classes e.g. ...

Read More »

Shuffle List elements example

In this example we shall show you how to shuffle a List’s elements. This is provided by the shuffle(List list) API method of the Collections class. The Collections class provides static methods that operate on or return collections. The ArrayList is used as a List implementation, but the same API applies to any type of List implementation classes e.g. Vector ...

Read More »

Reverse order of List example

With this example we are going to demonstrate how to reverse the order of a List. This is provided by the reverse(List list) API method of the Collections class. The ArrayList is used as a List implementation, but the same API applies to any type of List implementation classes e.g. Vector etc. In short, to reverse the order of a ...

Read More »

Replace specific element of List example

This is an example of how to replace a specific element of a List. We will use the replaceAll(List list, Object oldVal, Object newVal) API method of the Collections class. The ArrayList is used as a List implementation, but the same API applies to any type of List implementation classes e.g. Vector etc. Replacing a specific element of a List ...

Read More »

Replace all elements of List example

In this example we shall show you how to replace all elements of a List. We will use the fill(List list, Object element) API method of the Collections class. Collections provides static methods that operate on or return collections. The ArrayList is used as a List implementation, but the same API applies to any type of List implementation classes e.g. ...

Read More »

Binary search List example

With this example we are going to demonstrate how to Binary search a List. We will use the binarySearch(List list, T key) API method of the Collections class. Collections provides static methods that operate on or return collections. The ArrayList is used as a List implementation, but the same API applies to any type of List implementation classes e.g. Vector ...

Read More »

Get unmodifiable Collection

This is an example of how to get an unmodifiable Collection. The same API applies to any type of Collection implementation classes e.g. HashSet, TreeSet, LinkedHashSet, LinkedList etc. Nevertheless Java util API provides separate methods for getting immutable Collection views based on the following Collection implementation classes: – static Collection unmodifiableCollection(Collection) – static List unmodifiableList(List) – static Set unmodifiableSet(Set) – ...

Read More »

Get Synchronized Set example

In this example we shall show you how to get a Synchronized Set. We are using a HashSet, but the same API applies to any type of Set implementation. The Collections API provides us with methods so as to get synchronized (thread-safe) Collections from specific Collections. To get a synchronized Set one should perform the following steps: Create a new ...

Read More »

Want to take your Java skills to the next level?

Grab our programming books for FREE!

Here are some of the eBooks you will get:

  • Spring Interview QnA
  • Multithreading & Concurrency QnA
  • JPA Minibook
  • JVM Troubleshooting Guide
  • Advanced Java
  • Java Interview QnA
  • Java Design Patterns