Get intersection of several Lists using retainAll in Java - java

I'm having troubles to get the intersection of several Lists on Java. What I'm doing is this:
I get (lets say) 3 Lists of integer numbers:
list 1: [2, 2, 2, 2, 5, 5]
list 2: [2, 2, 103]
list 3: [2, 431]
I'm applying retainAll to the first one using each of the remaining lists:
list1.retainAll(list2);
list1.retainAll(list3);
And I'm getting this result:
list1: [2, 2, 2, 2]
But I'd expect to get this one:
list1: [2]
...Since the only element all lists share is one 2 and not four 2.
I know this is probably the expected behaviour of the retainAll function, but I need to get the result I mentioned above.
Any help?
Edit:
Using a HashSet to disallow duplicates won't do the trick either. In this case, for instance:
list 1: [2, 2, 2, 2, 5, 5]
list 2: [2, 2, 103]
list 3: [2, 2, 2, 431]
I need to get a result of:
list 1: [2, 2] (since all lists have at least a pair of 2's)
Instead of
list 1: [2]

What about this method:
public static <T> Collection <T> intersect (Collection <? extends T> a, Collection <? extends T> b)
{
Collection <T> result = new ArrayList <T> ();
for (T t: a)
{
if (b.remove (t)) result.add (t);
}
return result;
}
public static void main (String [] args)
{
List <Integer> list1 = new ArrayList <Integer> (Arrays.<Integer>asList (2, 2, 2, 2, 5, 5));
List <Integer> list2 = new ArrayList <Integer> (Arrays.<Integer>asList (2, 2, 103));
List <Integer> list3 = new ArrayList <Integer> (Arrays.<Integer>asList (2, 431));
System.out.println (intersect (list1, intersect (list2, list3)));
}

This problem can be solved easier with a multiset data structure. For example, if you use guava's Multiset, you can use Multisets.retainOccurrences()

I would use some kind of Set, perhaps a HashSet. They won't add duplicate elements, and they have the retainAll method.
Set<Integer> uniqueNums = new HashSet<Integer>(list1);
uniqueNums.retainAll(list2);
uniqueNums.retainAll(list3);
Here's the javadocs for Set.

With retainAll you will get wrong answer as you said. I would recommend you to use HashMap keeping integer/count pairs and scan each other list and narrow down your map.
Populate the map from the values of list1.
Iterate over each other list and take the min(# of intg in other_list, map.get(intg)) and update the map with that value.
Resulting map will be the intersection of all lists.

Instead of a list you need a data structure called bag, or multiset. The Apache commons collections library for example includes one:
http://commons.apache.org/collections/apidocs/org/apache/commons/collections/Bag.html#retainAll(java.util.Collection)

Here is a one just as you like and it is recursive.
public static <T> List<T> intersect(List<T> c1, List<T> c2) {
List<T> inter = new ArrayList<>(c1);
inter.retainAll(c2);
return inter;
}
public static <T> List<T> intersect(List<T> first, List<T>... rest) {
if (rest.length == 0)
return first;
List<T> second = rest[0];
first = intersect(first,second);
rest = Arrays.copyOfRange(rest, 1, rest.length);
return intersect(first, rest);
}

Related

Problem when working with arrays in Java - code does not go inside the "if" statement [duplicate]

How do I convert an array to a list in Java?
I used the Arrays.asList() but the behavior (and signature) somehow changed from Java SE 1.4.2 (docs now in archive) to 8 and most snippets I found on the web use the 1.4.2 behaviour.
For example:
int[] numbers = new int[] { 1, 2, 3 };
Arrays.asList(numbers)
on 1.4.2 returns a list containing the elements 1, 2, 3
on 1.5.0+ returns a list containing the array 'numbers'
In many cases it should be easy to detect, but sometimes it can slip unnoticed:
Assert.assertTrue(Arrays.asList(numbers).indexOf(4) == -1);
In your example, it is because you can't have a List of a primitive type. In other words, List<int> is not possible.
You can, however, have a List<Integer> using the Integer class that wraps the int primitive. Convert your array to a List with the Arrays.asList utility method.
Integer[] numbers = new Integer[] { 1, 2, 3 };
List<Integer> list = Arrays.asList(numbers);
See this code run live at IdeOne.com.
In Java 8, you can use streams:
int[] numbers = new int[] { 1, 2, 3 };
Arrays.stream(numbers)
.boxed()
.collect(Collectors.toList());
We cannot have List<int> as int is a primitive type so we can only have List<Integer>.
Java 16
Java 16 introduces a new method on Stream API called toList(). This handy method returns an unmodifiable List containing the stream elements. So, trying to add a new element to the list will simply lead to UnsupportedOperationException.
int[] ints = new int[] {1,2,3,4,5};
Arrays.stream(ints).boxed().toList();
Java 8 (int array)
int[] ints = new int[] {1,2,3,4,5};
List<Integer> list11 =Arrays.stream(ints).boxed().collect(Collectors.toList());
Java 8 and below (Integer array)
Integer[] integers = new Integer[] {1,2,3,4,5};
List<Integer> list21 = Arrays.asList(integers); // returns a fixed-size list backed by the specified array.
List<Integer> list22 = new ArrayList<>(Arrays.asList(integers)); // good
List<Integer> list23 = Arrays.stream(integers).collect(Collectors.toList()); //Java 8 only
Need ArrayList and not List?
In case we want a specific implementation of List e.g. ArrayList then we can use toCollection as:
ArrayList<Integer> list24 = Arrays.stream(integers)
.collect(Collectors.toCollection(ArrayList::new));
Why list21 cannot be structurally modified?
When we use Arrays.asList the size of the returned list is fixed because the list returned is not java.util.ArrayList, but a private static class defined inside java.util.Arrays. So if we add or remove elements from the returned list, an UnsupportedOperationException will be thrown. So we should go with list22 when we want to modify the list. If we have Java8 then we can also go with list23.
To be clear list21 can be modified in sense that we can call list21.set(index,element) but this list may not be structurally modified i.e. cannot add or remove elements from the list. You can also check this answer of mine for more explanation.
If we want an immutable list then we can wrap it as:
List<Integer> list22 = Collections.unmodifiableList(Arrays.asList(integers));
Another point to note is that the method Collections.unmodifiableList returns an unmodifiable view of the specified list. An unmodifiable view collection is a collection that is unmodifiable and is also a view onto a backing collection. Note that changes to the backing collection might still be possible, and if they occur, they are visible through the unmodifiable view.
We can have a truly immutable list in Java 9 and 10.
Truly Immutable list
Java 9:
String[] objects = {"Apple", "Ball", "Cat"};
List<String> objectList = List.of(objects);
Java 10 (Truly Immutable list):
We can use List.of introduced in Java 9. Also other ways:
List.copyOf(Arrays.asList(integers))
Arrays.stream(integers).collect(Collectors.toUnmodifiableList());
Speaking about conversion way, it depends on why do you need your List.
If you need it just to read data. OK, here you go:
Integer[] values = { 1, 3, 7 };
List<Integer> list = Arrays.asList(values);
But then if you do something like this:
list.add(1);
you get java.lang.UnsupportedOperationException.
So for some cases you even need this:
Integer[] values = { 1, 3, 7 };
List<Integer> list = new ArrayList<Integer>(Arrays.asList(values));
First approach actually does not convert array but 'represents' it like a List. But array is under the hood with all its properties like fixed number of elements. Please note you need to specify type when constructing ArrayList.
The problem is that varargs got introduced in Java 5 and unfortunately, Arrays.asList() got overloaded with a vararg version too. So Arrays.asList(numbers) is understood by the Java 5 compiler as a vararg parameter of int arrays.
This problem is explained in more details in Effective Java 2nd Ed., Chapter 7, Item 42.
I recently had to convert an array to a List. Later on the program filtered the list attempting to remove the data. When you use the Arrays.asList(array) function, you create a fixed size collection: you can neither add nor delete. This entry explains the problem better than I can: Why do I get an UnsupportedOperationException when trying to remove an element from a List?.
In the end, I had to do a "manual" conversion:
List<ListItem> items = new ArrayList<ListItem>();
for (ListItem item: itemsArray) {
items.add(item);
}
I suppose I could have added conversion from an array to a list using an List.addAll(items) operation.
Even shorter:
List<Integer> list = Arrays.asList(1, 2, 3, 4);
Using Arrays
This is the simplest way to convert an array to List. However, if you try to add a new element or remove an existing element from the list, an UnsupportedOperationException will be thrown.
Integer[] existingArray = {1, 2, 3};
List<Integer> list1 = Arrays.asList(existingArray);
List<Integer> list2 = Arrays.asList(1, 2, 3);
// WARNING:
list2.add(1); // Unsupported operation!
list2.remove(1); // Unsupported operation!
Using ArrayList or Other List Implementations
You can use a for loop to add all the elements of the array into a List implementation, e.g. ArrayList:
List<Integer> list = new ArrayList<>();
for (int i : new int[]{1, 2, 3}) {
list.add(i);
}
Using Stream API in Java 8
You can turn the array into a stream, then collect the stream using different collectors: The default collector in Java 8 use ArrayList behind the screen, but you can also impose your preferred implementation.
List<Integer> list1, list2, list3;
list1 = Stream.of(1, 2, 3).collect(Collectors.toList());
list2 = Stream.of(1, 2, 3).collect(Collectors.toCollection(ArrayList::new));
list3 = Stream.of(1, 2, 3).collect(Collectors.toCollection(LinkedList::new));
See also:
Why do we use autoboxing and unboxing in Java?
When to use LinkedList over ArrayList?
Another workaround if you use Apache commons-lang:
int[] numbers = new int[] { 1, 2, 3 };
Arrays.asList(ArrayUtils.toObject(numbers));
Where ArrayUtils.toObject converts int[] to Integer[]
In Java 9 you have the even more elegant solution of using immutable lists via the new convenience factory method List.of:
List<String> immutableList = List.of("one","two","three");
(shamelessly copied from here )
One-liner:
List<Integer> list = Arrays.asList(new Integer[] {1, 2, 3, 4});
If you are targeting Java 8 (or later), you can try this:
int[] numbers = new int[] {1, 2, 3, 4};
List<Integer> integers = Arrays.stream(numbers)
.boxed().collect(Collectors.<Integer>toList());
NOTE:
Pay attention to the Collectors.<Integer>toList(), this generic method helps you to avoid the error "Type mismatch: cannot convert from List<Object> to List<Integer>".
you have to cast in to array
Arrays.asList((Object[]) array)
Using Guava:
Integer[] array = { 1, 2, 3};
List<Integer> list = Lists.newArrayList(sourceArray);
Using Apache Commons Collections:
Integer[] array = { 1, 2, 3};
List<Integer> list = new ArrayList<>(6);
CollectionUtils.addAll(list, array);
I've had the same problem and wrote a generic function that takes an array and returns an ArrayList of the same type with the same contents:
public static <T> ArrayList<T> ArrayToArrayList(T[] array) {
ArrayList<T> list = new ArrayList<T>();
for(T elmt : array) list.add(elmt);
return list;
}
Given Array:
int[] givenArray = {2,2,3,3,4,5};
Converting integer array to Integer List
One way: boxed() -> returns the IntStream
List<Integer> givenIntArray1 = Arrays.stream(givenArray)
.boxed()
.collect(Collectors.toList());
Second Way: map each element of the stream to Integer and then collect
NOTE:
Using mapToObj you can covert each int element into string stream, char stream etc by casing i to (char)i
List<Integer> givenIntArray2 = Arrays.stream(givenArray)
.mapToObj(i->i)
.collect(Collectors.toList());
Converting One array Type to Another Type Example:
List<Character> givenIntArray2 = Arrays.stream(givenArray)
.mapToObj(i->(char)i)
.collect(Collectors.toList());
So it depends on which Java version you are trying-
Java 7
Arrays.asList(1, 2, 3);
OR
final String arr[] = new String[] { "G", "E", "E", "K" };
final List<String> initialList = new ArrayList<String>() {{
add("C");
add("O");
add("D");
add("I");
add("N");
}};
// Elements of the array are appended at the end
Collections.addAll(initialList, arr);
OR
Integer[] arr = new Integer[] { 1, 2, 3 };
Arrays.asList(arr);
In Java 8
int[] num = new int[] {1, 2, 3};
List<Integer> list = Arrays.stream(num)
.boxed().collect(Collectors.<Integer>toList())
Reference - http://www.codingeek.com/java/how-to-convert-array-to-list-in-java/
Can you improve this answer please as this is what I use but im not 100% clear. It works fine but intelliJ added new WeatherStation[0]. Why the 0 ?
public WeatherStation[] removeElementAtIndex(WeatherStation[] array, int index)
{
List<WeatherStation> list = new ArrayList<WeatherStation>(Arrays.asList(array));
list.remove(index);
return list.toArray(new WeatherStation[0]);
}
Use this to convert an Array arr to List.
Arrays.stream(arr).collect(Collectors.toList());
An example of defining a generic method to convert an array to a list:
public <T> List<T> fromArrayToList(T[] a) {
return Arrays.stream(a).collect(Collectors.toList());
}
use two line of code to convert array to list if you use it in integer value
you must use autoboxing type for primitive data type
Integer [] arr={1,2};
List<Integer> listInt=Arrays.asList(arr);
As of Java 8, the following should do
int[] temp = {1, 2, 3, 4, 5};
List<Integer> tempList = Arrays.stream(temp).boxed().collect(Collectors.toList());
If you are trying to optimize for memory, etc., (and don't want to pull in external libraries) it's simpler than you think to implement your own immutable "array view list" – you just need to extend java.util.AbstractList.
class IntArrayViewList extends AbstractList<Integer> {
int[] backingArray;
int size;
IntArrayViewList(int[] backingArray, int size) {
this.backingArray = backingArray;
this.size = size;
}
public Iterator<Integer> iterator() {
return new Iterator<Integer>() {
int i = 0;
#Override
public boolean hasNext() {
return i < size;
}
#Override
public Integer next() {
return get(i++);
}
};
}
public int size() {
return size;
}
public Integer get(int i) {
return backingArray[i];
}
}
int is a primitive. Primitives can’t accept null and have default value. Hence, to accept null you need to use wrapper class Integer.
Option 1:
int[] nos = { 1, 2, 3, 4, 5 };
Integer[] nosWrapped = Arrays.stream(nos).boxed()   
.toArray(Integer[]::new);
nosWrapped[5] = null // can store null
Option 2:
You can use any data structure that uses the wrapper class Integer
int[] nos = { 1, 2, 3, 4, 5 };
List<Integer> = Arrays.asList(nos)
I started looking at this by trying to reduce the amount of code preparing the input of some test cases. I see a lot of effort around trying to include advanced and new features along with Arrays.asList(), but below the code chosen due simplicity:
//Integer input[]
List<Integer> numbers = Arrays.asList(new Integer[]{1, 2 ,3, 4, 5, 4, 3, 2, 1, 3, 4});
//String input[]
List<String> names = Arrays.asList(new String[]{"Jhon", "Lucas", "Daniel", "Jim", "Sam"});
//String input[]
List<Character> letters = Arrays.asList(new Character[]{'A', 'B', 'K', 'J', 'F'});
Please notice that Anonymous array example will work just with Arrays of Non Primitive Types as the API uses Generics, that's the reason you can see several 2 line examples around, more info here: Why don't Java Generics support primitive types?
For newer JDKs there is another simpler option, the below examples are equivalent to the ones show above:
//Integer
List<Integer> numbers = Arrays.asList(1, 2 ,3, 4, 5, 4, 3, 2, 1, 3, 4);
//String
List<String> names = Arrays.asList("Jhon", "Lucas", "Daniel", "Jim", "Sam");
//Character
List<Character> letters = Arrays.asList('A', 'B', 'K', 'J', 'F');

A better way to dedupe when adding linkedlist to a linkedlist?

I'm trying to add some dedupe logic to a LinkedList. The duplicates come from new LinkedLists getting added to the master LinkedList.:
masterList.addAll(0, newList)
The tricky part for me is that, each new list is getting added to the beginning of the master list (as the above code shows). And the dedupe needs to keep the elements that are added later. For instance:
masterList = [3, 4]
newList = [5, 4]
masterList.addAll(0, newList)
Now masterList = [5, 4, 3, 4]. And the "4" at the end of masterList should be removed as dupe while the later-added "4" needs to remain. So the dedupe result should be masterList = [5, 4, 3].
My current solution is to dedupe AFTER the "add" is done:
protected List<String> dedupeIds(List<String> masterList) {
// HashSet to store seen values
HashSet<String> set = new HashSet<>();
for (Iterator<String> iter = masterList.iterator(); iter.hasNext();) {
String doc_id = iter.next();
// put the doc id in Set hs, if cannot add as key, it means dupe
if (!set.add(doc_id)) {
iter.remove();
}
}
return masterList;
}
The current solution works, but I wonder if there is a way to dedupe during "add"?
As Chrylis suggested, perhaps consider a different collection type. LinkedHashSet would be a good choice for storing unique elements (it's a Set) while also preserving order.
final Set<Integer> a = new LinkedHashSet<>(Arrays.asList(3, 4));
System.out.println(Arrays.toString(a.toArray())); // Prints [3, 4]
final Set<Integer> b = new LinkedHashSet<>(Arrays.asList(5, 4));
System.out.println(Arrays.toString(b.toArray())); // Prints [5, 4]
b.addAll(a);
System.out.println(Arrays.toString(b.toArray())); // Prints [5, 4, 3]

Shuffle list except for certain sequences

I want to shuffle an ArrayList but based on some custom conditions:
if my array list was something like [1, 4, 5, 6, 9, 45, 67],
I want to shuffle it but make sure 5, 6, 9 always appear together.
Is there any method available in Collections class to do this?
I have tried doing this, but it throws ConcurrentModificationException
List<Integer> y= new ArrayList<>();
y.add(1);
y.add(4);
y.add(5);
y.add(6);
y.add(9);
y.add(45);
y.add(67);
List<Integer> z = y.subList(2, 5);
y.removeAll(z);
Collections.shuffle(y);
int index = ThreadLocalRandom.current()
.nextInt(0, y.size() + 1);
y.addAll(index,z);
It sounds like your data should really be a list of lists, especially since its likely that you will have more than 1 group that needs to stay together.
You can always flatten it when you need.
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class Example {
public static void main(String[] args) {
List<List<Integer>> y = new ArrayList<List<Integer>>();
y.add(new ArrayList<Integer>(Arrays.asList(1)));
y.add(new ArrayList<Integer>(Arrays.asList(4)));
y.add(new ArrayList<Integer>(Arrays.asList(5, 6, 9)));
y.add(new ArrayList<Integer>(Arrays.asList(45)));
y.add(new ArrayList<Integer>(Arrays.asList(67)));
Collections.shuffle(y);
List<Integer> flatList = new ArrayList<>();
y.forEach(flatList::addAll);
}
}
A simple way of doing this is to store your target elements in a separate List:
List<Integer> target = new ArrayList<>();
target.add(5);
target.add(6);
target.add(9);
Then shuffle your main list:
Collections.shuffle(y);
Then get a random number from 0 -> y.size().
Random ran = new Random();
int pos = ran.nextInt(y.size());
And insert your target list into your original list:
y.addAll(pos, target);
Note: this assumes your original list has the target 3 numbers removed already.
Without seeing your code, I'd think that the ConcurrentModificationExceptions are thrown because you try to remove the group elements from the list or to add them back in while iterating it. Changing a collection while iterating it leads to those exceptions: Iterating through a Collection, avoiding ConcurrentModificationException when removing in loop.
It will get a lot easier if you do not treat the groups as the exception, but as the norm. What I mean by this is that you should convert your List<?> into a List<List<?>> where each sub list contains either one element or one of the groups. You can then shuffle that list easily with Collections.shuffle() and flatten it again.
Look at this rough implementation:
List<Integer> ints = new ArrayList<>(asList(2, 3, 5, 4, 8, 7, 11, 55));
List<List<Integer>> groups = asList(asList(5, 4), asList(7, 11));
// remove all the grouped elements from the list
groups.forEach(ints::removeAll);
// wrap the single elements into list and join them with the groups
List<List<Integer>> wrapped = Stream.concat(ints.stream().map(Arrays::asList),
groups.stream())
.collect(Collectors.toList());
Collections.shuffle(wrapped);
// flatten the list into single elements again
List<Integer> shuffled = wrapped.stream()
.flatMap(Collection::stream)
.collect(Collectors.toList());
System.out.println(shuffled); // e.g. [55, 3, 7, 11, 2, 8, 5, 4]
// ----- ----
Note that while this is quite readable, it is probably not the most efficient or error proof solution. But it should give you an idea how to tackle the problem.
Edit after comment from Gonen I. Here is a helper method to only remove the exact sequences and not random parts of them all over the list:
private static <T> void removeSequence(List<T> list, List<T> sequence)
{
int indexOfSubList = Collections.lastIndexOfSubList(list, sequence);
while (indexOfSubList != -1)
{
for (int j = 0; j < sequence.size(); j++)
{
list.remove(indexOfSubList);
}
indexOfSubList = Collections.lastIndexOfSubList(list, sequence);
}
}
Use it by replacing groups.forEach(ints::removeAll); by groups.forEach(group -> removeSequence(ints, group));

Using collection to remove duplicate Lists

I have 3 Lists, two of which are duplicates. I want to store these Lists into one unit, removing all the duplicates, so I was thinking to add each List to a collection. This is what I tried:
List<Integer> coins1 = Arrays.asList(5, 5, 10);
List<Integer> coins2 = Arrays.asList(5, 10, 5);
List<Integer> coins3 = Arrays.asList(10, 10);
coins1.sort((a, b) -> a.compareTo(b));
coins2.sort((a, b) -> a.compareTo(b));
coins3.sort((a, b) -> a.compareTo(b));
Collection<List<Integer>> allCoinPossibilities = new TreeSet();
allCoinPossibilities.add(coins1);
allCoinPossibilities.add(coins2);
allCoinPossibilities.add(coins3);
I get an error "java.util.Arrays$ArrayList cannot be cast to java.lang.Comparable". The error makes sense, the Collection doesn't know how to compare each list to disallow duplicates. Do I need to override a compare method? If so, How would I do that? Is this the correct approach to solving this problem?
Thanks!
Make a HashSet and add everything into that.
At the end you'll be left with just the unique elements
List<Integer> coins1 = Arrays.asList(5, 5, 10);
List<Integer> coins2 = Arrays.asList(5, 10, 5);
List<Integer> coins3 = Arrays.asList(10, 10);
Set<Integer> dedupedCollection = new HashSet<Integer>();
dedupedCollection.add(coins1);
dedupedCollection.add(coins2);
dedupedCollection.add(coins3);
return dedupedCollection;
then you can return dedupedCollection; as the final set with no duplicartes.
Why not use a HashSet instead?
Lists already have a good hashCode implementation and a correct equals method.
Also, comparing Lists is not really logically possible - think about comparing two sets of numbers. How do I compare [2, 3, 8] with [1, 7, -2]?
Set<List<Integer>> noDuplicates = new HashSet<>();
noDuplicates.add(coins1);
noDuplicates.add(coins2);
noDuplicates.add(coins3);
//Now there are no duplicate lists.
Keep in mind that the lists must also have the same orderings, otherwise equals returns false. If you do not want this requirement you can also use a Set for your coins.
You can write your own comparator and pass that as an argument while creating the TreeSet, e.g.:
class ListComparator implements Comparator<List<Integer>>{
#Override
public int compare(List<Integer> o1, List<Integer> o2) {
// TODO comparison logic
return 0;
}
}
Collection<List<Integer>> allCoinPossibilities = new TreeSet(new ListComparator());

Converting array to list in Java

How do I convert an array to a list in Java?
I used the Arrays.asList() but the behavior (and signature) somehow changed from Java SE 1.4.2 (docs now in archive) to 8 and most snippets I found on the web use the 1.4.2 behaviour.
For example:
int[] numbers = new int[] { 1, 2, 3 };
Arrays.asList(numbers)
on 1.4.2 returns a list containing the elements 1, 2, 3
on 1.5.0+ returns a list containing the array 'numbers'
In many cases it should be easy to detect, but sometimes it can slip unnoticed:
Assert.assertTrue(Arrays.asList(numbers).indexOf(4) == -1);
In your example, it is because you can't have a List of a primitive type. In other words, List<int> is not possible.
You can, however, have a List<Integer> using the Integer class that wraps the int primitive. Convert your array to a List with the Arrays.asList utility method.
Integer[] numbers = new Integer[] { 1, 2, 3 };
List<Integer> list = Arrays.asList(numbers);
See this code run live at IdeOne.com.
In Java 8, you can use streams:
int[] numbers = new int[] { 1, 2, 3 };
Arrays.stream(numbers)
.boxed()
.collect(Collectors.toList());
We cannot have List<int> as int is a primitive type so we can only have List<Integer>.
Java 16
Java 16 introduces a new method on Stream API called toList(). This handy method returns an unmodifiable List containing the stream elements. So, trying to add a new element to the list will simply lead to UnsupportedOperationException.
int[] ints = new int[] {1,2,3,4,5};
Arrays.stream(ints).boxed().toList();
Java 8 (int array)
int[] ints = new int[] {1,2,3,4,5};
List<Integer> list11 =Arrays.stream(ints).boxed().collect(Collectors.toList());
Java 8 and below (Integer array)
Integer[] integers = new Integer[] {1,2,3,4,5};
List<Integer> list21 = Arrays.asList(integers); // returns a fixed-size list backed by the specified array.
List<Integer> list22 = new ArrayList<>(Arrays.asList(integers)); // good
List<Integer> list23 = Arrays.stream(integers).collect(Collectors.toList()); //Java 8 only
Need ArrayList and not List?
In case we want a specific implementation of List e.g. ArrayList then we can use toCollection as:
ArrayList<Integer> list24 = Arrays.stream(integers)
.collect(Collectors.toCollection(ArrayList::new));
Why list21 cannot be structurally modified?
When we use Arrays.asList the size of the returned list is fixed because the list returned is not java.util.ArrayList, but a private static class defined inside java.util.Arrays. So if we add or remove elements from the returned list, an UnsupportedOperationException will be thrown. So we should go with list22 when we want to modify the list. If we have Java8 then we can also go with list23.
To be clear list21 can be modified in sense that we can call list21.set(index,element) but this list may not be structurally modified i.e. cannot add or remove elements from the list. You can also check this answer of mine for more explanation.
If we want an immutable list then we can wrap it as:
List<Integer> list22 = Collections.unmodifiableList(Arrays.asList(integers));
Another point to note is that the method Collections.unmodifiableList returns an unmodifiable view of the specified list. An unmodifiable view collection is a collection that is unmodifiable and is also a view onto a backing collection. Note that changes to the backing collection might still be possible, and if they occur, they are visible through the unmodifiable view.
We can have a truly immutable list in Java 9 and 10.
Truly Immutable list
Java 9:
String[] objects = {"Apple", "Ball", "Cat"};
List<String> objectList = List.of(objects);
Java 10 (Truly Immutable list):
We can use List.of introduced in Java 9. Also other ways:
List.copyOf(Arrays.asList(integers))
Arrays.stream(integers).collect(Collectors.toUnmodifiableList());
Speaking about conversion way, it depends on why do you need your List.
If you need it just to read data. OK, here you go:
Integer[] values = { 1, 3, 7 };
List<Integer> list = Arrays.asList(values);
But then if you do something like this:
list.add(1);
you get java.lang.UnsupportedOperationException.
So for some cases you even need this:
Integer[] values = { 1, 3, 7 };
List<Integer> list = new ArrayList<Integer>(Arrays.asList(values));
First approach actually does not convert array but 'represents' it like a List. But array is under the hood with all its properties like fixed number of elements. Please note you need to specify type when constructing ArrayList.
The problem is that varargs got introduced in Java 5 and unfortunately, Arrays.asList() got overloaded with a vararg version too. So Arrays.asList(numbers) is understood by the Java 5 compiler as a vararg parameter of int arrays.
This problem is explained in more details in Effective Java 2nd Ed., Chapter 7, Item 42.
I recently had to convert an array to a List. Later on the program filtered the list attempting to remove the data. When you use the Arrays.asList(array) function, you create a fixed size collection: you can neither add nor delete. This entry explains the problem better than I can: Why do I get an UnsupportedOperationException when trying to remove an element from a List?.
In the end, I had to do a "manual" conversion:
List<ListItem> items = new ArrayList<ListItem>();
for (ListItem item: itemsArray) {
items.add(item);
}
I suppose I could have added conversion from an array to a list using an List.addAll(items) operation.
Even shorter:
List<Integer> list = Arrays.asList(1, 2, 3, 4);
Using Arrays
This is the simplest way to convert an array to List. However, if you try to add a new element or remove an existing element from the list, an UnsupportedOperationException will be thrown.
Integer[] existingArray = {1, 2, 3};
List<Integer> list1 = Arrays.asList(existingArray);
List<Integer> list2 = Arrays.asList(1, 2, 3);
// WARNING:
list2.add(1); // Unsupported operation!
list2.remove(1); // Unsupported operation!
Using ArrayList or Other List Implementations
You can use a for loop to add all the elements of the array into a List implementation, e.g. ArrayList:
List<Integer> list = new ArrayList<>();
for (int i : new int[]{1, 2, 3}) {
list.add(i);
}
Using Stream API in Java 8
You can turn the array into a stream, then collect the stream using different collectors: The default collector in Java 8 use ArrayList behind the screen, but you can also impose your preferred implementation.
List<Integer> list1, list2, list3;
list1 = Stream.of(1, 2, 3).collect(Collectors.toList());
list2 = Stream.of(1, 2, 3).collect(Collectors.toCollection(ArrayList::new));
list3 = Stream.of(1, 2, 3).collect(Collectors.toCollection(LinkedList::new));
See also:
Why do we use autoboxing and unboxing in Java?
When to use LinkedList over ArrayList?
Another workaround if you use Apache commons-lang:
int[] numbers = new int[] { 1, 2, 3 };
Arrays.asList(ArrayUtils.toObject(numbers));
Where ArrayUtils.toObject converts int[] to Integer[]
In Java 9 you have the even more elegant solution of using immutable lists via the new convenience factory method List.of:
List<String> immutableList = List.of("one","two","three");
(shamelessly copied from here )
One-liner:
List<Integer> list = Arrays.asList(new Integer[] {1, 2, 3, 4});
If you are targeting Java 8 (or later), you can try this:
int[] numbers = new int[] {1, 2, 3, 4};
List<Integer> integers = Arrays.stream(numbers)
.boxed().collect(Collectors.<Integer>toList());
NOTE:
Pay attention to the Collectors.<Integer>toList(), this generic method helps you to avoid the error "Type mismatch: cannot convert from List<Object> to List<Integer>".
you have to cast in to array
Arrays.asList((Object[]) array)
Using Guava:
Integer[] array = { 1, 2, 3};
List<Integer> list = Lists.newArrayList(sourceArray);
Using Apache Commons Collections:
Integer[] array = { 1, 2, 3};
List<Integer> list = new ArrayList<>(6);
CollectionUtils.addAll(list, array);
I've had the same problem and wrote a generic function that takes an array and returns an ArrayList of the same type with the same contents:
public static <T> ArrayList<T> ArrayToArrayList(T[] array) {
ArrayList<T> list = new ArrayList<T>();
for(T elmt : array) list.add(elmt);
return list;
}
Given Array:
int[] givenArray = {2,2,3,3,4,5};
Converting integer array to Integer List
One way: boxed() -> returns the IntStream
List<Integer> givenIntArray1 = Arrays.stream(givenArray)
.boxed()
.collect(Collectors.toList());
Second Way: map each element of the stream to Integer and then collect
NOTE:
Using mapToObj you can covert each int element into string stream, char stream etc by casing i to (char)i
List<Integer> givenIntArray2 = Arrays.stream(givenArray)
.mapToObj(i->i)
.collect(Collectors.toList());
Converting One array Type to Another Type Example:
List<Character> givenIntArray2 = Arrays.stream(givenArray)
.mapToObj(i->(char)i)
.collect(Collectors.toList());
So it depends on which Java version you are trying-
Java 7
Arrays.asList(1, 2, 3);
OR
final String arr[] = new String[] { "G", "E", "E", "K" };
final List<String> initialList = new ArrayList<String>() {{
add("C");
add("O");
add("D");
add("I");
add("N");
}};
// Elements of the array are appended at the end
Collections.addAll(initialList, arr);
OR
Integer[] arr = new Integer[] { 1, 2, 3 };
Arrays.asList(arr);
In Java 8
int[] num = new int[] {1, 2, 3};
List<Integer> list = Arrays.stream(num)
.boxed().collect(Collectors.<Integer>toList())
Reference - http://www.codingeek.com/java/how-to-convert-array-to-list-in-java/
Can you improve this answer please as this is what I use but im not 100% clear. It works fine but intelliJ added new WeatherStation[0]. Why the 0 ?
public WeatherStation[] removeElementAtIndex(WeatherStation[] array, int index)
{
List<WeatherStation> list = new ArrayList<WeatherStation>(Arrays.asList(array));
list.remove(index);
return list.toArray(new WeatherStation[0]);
}
Use this to convert an Array arr to List.
Arrays.stream(arr).collect(Collectors.toList());
An example of defining a generic method to convert an array to a list:
public <T> List<T> fromArrayToList(T[] a) {
return Arrays.stream(a).collect(Collectors.toList());
}
use two line of code to convert array to list if you use it in integer value
you must use autoboxing type for primitive data type
Integer [] arr={1,2};
List<Integer> listInt=Arrays.asList(arr);
As of Java 8, the following should do
int[] temp = {1, 2, 3, 4, 5};
List<Integer> tempList = Arrays.stream(temp).boxed().collect(Collectors.toList());
If you are trying to optimize for memory, etc., (and don't want to pull in external libraries) it's simpler than you think to implement your own immutable "array view list" – you just need to extend java.util.AbstractList.
class IntArrayViewList extends AbstractList<Integer> {
int[] backingArray;
int size;
IntArrayViewList(int[] backingArray, int size) {
this.backingArray = backingArray;
this.size = size;
}
public Iterator<Integer> iterator() {
return new Iterator<Integer>() {
int i = 0;
#Override
public boolean hasNext() {
return i < size;
}
#Override
public Integer next() {
return get(i++);
}
};
}
public int size() {
return size;
}
public Integer get(int i) {
return backingArray[i];
}
}
int is a primitive. Primitives can’t accept null and have default value. Hence, to accept null you need to use wrapper class Integer.
Option 1:
int[] nos = { 1, 2, 3, 4, 5 };
Integer[] nosWrapped = Arrays.stream(nos).boxed()   
.toArray(Integer[]::new);
nosWrapped[5] = null // can store null
Option 2:
You can use any data structure that uses the wrapper class Integer
int[] nos = { 1, 2, 3, 4, 5 };
List<Integer> = Arrays.asList(nos)
I started looking at this by trying to reduce the amount of code preparing the input of some test cases. I see a lot of effort around trying to include advanced and new features along with Arrays.asList(), but below the code chosen due simplicity:
//Integer input[]
List<Integer> numbers = Arrays.asList(new Integer[]{1, 2 ,3, 4, 5, 4, 3, 2, 1, 3, 4});
//String input[]
List<String> names = Arrays.asList(new String[]{"Jhon", "Lucas", "Daniel", "Jim", "Sam"});
//String input[]
List<Character> letters = Arrays.asList(new Character[]{'A', 'B', 'K', 'J', 'F'});
Please notice that Anonymous array example will work just with Arrays of Non Primitive Types as the API uses Generics, that's the reason you can see several 2 line examples around, more info here: Why don't Java Generics support primitive types?
For newer JDKs there is another simpler option, the below examples are equivalent to the ones show above:
//Integer
List<Integer> numbers = Arrays.asList(1, 2 ,3, 4, 5, 4, 3, 2, 1, 3, 4);
//String
List<String> names = Arrays.asList("Jhon", "Lucas", "Daniel", "Jim", "Sam");
//Character
List<Character> letters = Arrays.asList('A', 'B', 'K', 'J', 'F');

Categories

Resources