Javatpoint Logo
Javatpoint Logo

Java Convert Array to Collection

In Java, changing an array to a collection may be finished using diverse techniques supplied by way of the Java Collections Framework. The Collections Framework presents a hard and fast of interfaces and training to manipulate collections of objects. To convert an array to a set, you may comply with those steps:

  • Create an array of any type and populate it with factors.
  • Use the Arrays.asList() method to transform the array to a List series.

Note that Arrays.asList() returns a fixed-length list backed by way of the unique array. Any modifications to the listing will without delay have an effect on the array and vice versa.

  • If we want a different type of series, which includes a Set or Queue, we could create a new series and skip the array as a parameter to the constructor.

Here, the HashSet constructor accepts a Collection as a parameter, so we skip the end result of Arrays.AsList().

  • If we require a mutable collection that may be modified independently from the authentic array, we can create a new instance of the desired collection kind and add the factors separately.

The Collections.addAll() approach lets in you to feature the elements of an array to a set.

  • If we choose to use a general series type, along with Collection or Iterable, we can use the Arrays.stream() approach in aggregate with the collect() method to transform the array.

Here, Arrays.movement() converts the array to a flow of factors, and acquire() collects the factors right into a List using the Collectors.toList() collector.

  • Using the Apache Commons Collections library: If we have got Apache Commons Collections library brought in your task, we can use the CollectionUtils.addAll() approach to feature array factors to a group.

ArrayToCollectionExample.java

Output:

1
2
3
4
5
  • Using Java 8 streams: Java 8 brought the Stream API, which offers powerful practical-fashion operations on collections. You can convert an array to a group the usage of streams as follows:

ArrayToCollectionExample.java

Output:

1
2
3
4
5

This technique permits you to take benefit of numerous movement operations like filtering, mapping, and sorting if wanted.

  • Converting to an unmodifiable series: If we need to create an unmodifiable series, we can wrap the transformed series the usage of Collections.UnmodifiableCollection() or Collections.UnmodifiableList() techniques.

ArrayToUnmodifiableCollectionExample.java

Output:

Error: null
1
2
3
4
5
  • Using a loop: we can iterate over the array factors the usage of a loop and add them in my opinion to a collection.

ArrayToCollectionWithLoopExample.java

Output:

1
2
3
4
5

This approach offers us flexibility in appearing extra operations or adjustments on every detail before including it to the collection.

  • Using third-party libraries: There are several third-party libraries to be had, together with Guava and Eclipse Collections, that provide handy strategies for array-to-collection conversion. We can explore the documentation of those libraries for their unique conversion methods.

ArrayToCollectionWithGuavaExample.java

Output:

1
2
3
4
5
  • Using the Stream API with ArrayUtils from Apache Commons Library: If we had Apache Commons Lang library added on your undertaking, we can combine the Stream API with ArrayUtils.toObject() approach to transform an array to a set.

ArrayToCollectionExample.java

Output:

1
2
3
4
5
  • Using the Stream API with the StreamSupport class: We can use the StreamSupport.Movement() technique along with an Iterable implementation to convert an array to a set.

ArrayToCollectionExample.java

Output:

1
2
3
4
5

The approach is beneficial when managing custom array kinds or when we had a custom Iterable implementation.

  • Using the LinkedList elegance: If we particularly need a LinkedList, we can convert the array with the aid of iterating over the elements and adding them to the list for my part.

ArrayToCollectionWithLinkedListExample.java

Output:

1
2
3
4
5
  • Using the addAll() approach of a collection: If we had already got an empty collection, you could use the addAll() technique to feature the elements of the array to the collection.

ArrayToCollectionWithAddAllExample.java

Output:

1
2
3
4
5

These extra topics provide us more options for changing an array to a collection in Java. We can choose the technique that aligns along with our specific use case and takes benefit of the available Java versions and libraries.

In Summary, converting an array to a collection in Java can be accomplished using several different techniques. The Java Collections Framework provides various methods and interfaces to manage collections of objects efficiently.

The choice of method depends on the specific use case, desired collection type, and whether a mutable or unmodifiable collection is needed. It's important to understand the characteristics and behaviour of each method to make the right decision for the particular situation.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA