#1 how to convert int[]/ Integer[] to list for int[]: int[] arr = {1, 2, 3}; //you have to box the primitive type of Integer List<Integer> list = Arrays.stream(arr).boxed().collect(Collectors.toList()); for Integer: Integer[] arr = {1, 2, 3}; List<Integer> list = Array.asList(array); but cannot do: list.remove(0); // UnsupportedOperationException :( Here you cannot remove the 0 element because asList returns a fixed-size list backed by the specified array. So you should do something like: List<Object> newList = new ArrayList<>(Arrays.asList(array)); in order to make the newList modifiable. #2 how to convert Integer[] to list, and to int[] Integer[] arr = {1, 2, 3}; //to list List<Integer> list = Array.asList(array); // to int[] int[] array = list.stream().mapToInt(x->x).toArray(); //to back to Integer[] Integer[] array = list.stream().mapToInt(x->x).boxed().toArray(Integer[]::new); more examples: Integer[] a = {1,3,5}
I love programming
The reason for doing the work is to create something useful that helps people. Please click the ads if my articles are useful for you. Definitely, that's not enough. My target is working at some great organizations, such that one day, all the people with internet access can benefit from the service I contributed.