Home » Core Java » util » Collections » Page 2

Collections

Get Synchronized Map example

Bipartite Graph

With this example we are going to demonstrate how to get a synchronized Map. We are using a HashMAp as example, but the same API applies to any type of Map implementation class e.g. TreeMap etc. The Collections API provides methods that return synchronized (thread-safe) collections (Lists, Sets, Maps). In short, to get a synchronized Map you should: Create a ...

Read More »

Get Synchronized List from ArrayList example

Bipartite Graph

This is an example of how to get a synchronized List from an ArrayList. The Collections class provides us with synchronizedList(List list) API method, that returns a synchronized (thread-safe) list from the provided ArrayList. Getting a synchronized List from an ArrayList implies that you should: Create an ArrayList. Populate the arrayList with elements, with add(E e) API method of ArrayList. ...

Read More »

Get Enumeration from Collection example

Bipartite Graph

In this example we shall show you how to get an Enumeration from a Collection. We are using an ArrayList, but the same API applies to any type of Collection implementation class e.g. HashSet, TreeSet, LinkedHashSet, LinkedList, Vector etc. To get an Enumeration from an ArrayList one should perform the following steps: Create an ArrayList. Populate the arrayList with elements, ...

Read More »

Find minimum and maximum elements of Collection example

Bipartite Graph

With this example we are going to demonstrate how to find minimum and maximum elements of a Collection. We are using an ArrayList, but the same API applies to any type of Collection implementation class e.g. HashSet, TreeSet, LinkedHashSet, LinkedList, Vector etc. In short, to find minimum and maximum elements of a Collection you should: Create an ArrayList. Populate the ...

Read More »

Create List of n copies of an Object example

Bipartite Graph

This is an example of how to create a List of n copies of an Object. We will use the nCopies(int n, T o) API method of Collections. Creating a List of n copies of an Object implies that you should: Invoke the nCopies(int n, T o) API method of Collections. It returns an immutable List containing n copies of ...

Read More »

Create ArrayList from Enumeration example

Bipartite Graph

In this example we shall show you how to create an ArrayList from an Enumeration. We will use a Vector to get the Enumeration from. To create an ArrayList from an Enumeration one should perform the following steps: Create a new Vector. Populate the vector with elements, with the add(E e) API method of the Vector. Invoke the elements() API ...

Read More »

Copy Collection to another Collection example

Bipartite Graph

With this example we are going to demonstrate how to copy a Collection to another Collection. In particular, we will use an ArrayList to be copied to a Vector, but the same API applies to any type of List implementation classes e.g. LinkedList etc. In short, to copy a list to another list you should: Create an ArrayList. Populate the ...

Read More »