Discussing approaches to convert array to mutable and immutable list
Introduction
In this article, we will see various approaches to convert fixed-size arrays in java to List.
We will also cover how to convert arrays to mutable and immutable lists
Consider primitive array as shown below:
Input
Our input array contains an array of primitive int types.
int[] x = { 1,2,3,4 };
Converting to Mutable List
Now let’s consider that we need a mutable list from the array. We can use java streams API to convert primitive array to object type and then using collector we can convert it to list.
Collectors.toList returns a mutable list but as per the doc it’s not guaranteed.
var list = Arrays.stream(x)
.boxed()
.collect(Collectors.toList());
If we really need a mutability guarantee, then we can modify the above code a little bit.
toCollection method can accept type arguments and now we can pass ArrayList type for List. This returns a mutable list.
var list = Arrays.stream(x)
.boxed()
.collect(Collectors.toCollection(ArrayList::new));
Converting to Immutable List
Collections class provides unmodifiableList method, which converts modifiable to list unmodifiable.
Since this method expects a list as input, we need to convert our input array to modifiableList first and then use unmodifiableList method.
List<Integer> modifiableList =
Arrays.stream(x).boxed().collect(Collectors.toList());
var unmodifiableList = Collections.unmodifiableList(modifiableList);
unmodifiableList.add(1);
Or better approach is to convert the input array to stream and then box it Object type, then use toList() which returns the immutable list.
Array.asList method returns List as output. Although this list is mutable, this is in a fixed size. So if we try to modify it by adding more elements it will throw an error.
Hence one of the solutions to overcome that is to pass the result of Array.asList to a new ArrayList constructor, which will return mutable and dynamic size ArrayList.
// fixed size mutable list
var m = Arrays.asList(Arrays.stream(x).boxed().toArray(Integer[]::new));
// m.add(3); // doesnt work
// mutable non-fixed size mutable list
var l = new ArrayList<>(Arrays.asList(Arrays.stream(x).boxed().toArray(Integer[]::new)));
l.add(1); //works
Constructor
In this article, we explore different approaches to convert an array to a list in java.
We also discuss how we can get mutable and immutable lists from primitive arrays.
Before You Leave
Let me know if I can be of any help to your career, I would love to chat or jump on a call. you can connect me over Linkedin.
We use cookies on our website to give you the most relevant experience by remembering your preferences and repeat visits. By clicking “Accept”, you consent to the use of ALL the cookies.
This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary cookies are absolutely essential for the website to function properly. These cookies ensure basic functionalities and security features of the website, anonymously.
Cookie
Duration
Description
cookielawinfo-checkbox-analytics
11 months
This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Analytics".
cookielawinfo-checkbox-functional
11 months
The cookie is set by GDPR cookie consent to record the user consent for the cookies in the category "Functional".
cookielawinfo-checkbox-necessary
11 months
This cookie is set by GDPR Cookie Consent plugin. The cookies is used to store the user consent for the cookies in the category "Necessary".
cookielawinfo-checkbox-others
11 months
This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Other.
cookielawinfo-checkbox-performance
11 months
This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Performance".
viewed_cookie_policy
11 months
The cookie is set by the GDPR Cookie Consent plugin and is used to store whether or not user has consented to the use of cookies. It does not store any personal data.
Functional cookies help to perform certain functionalities like sharing the content of the website on social media platforms, collect feedbacks, and other third-party features.
Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.
Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics the number of visitors, bounce rate, traffic source, etc.
Advertisement cookies are used to provide visitors with relevant ads and marketing campaigns. These cookies track visitors across websites and collect information to provide customized ads.