Convert double array to ArrayList Java
Ngày đăng:
15/01/2022
Trả lời:
0
Lượt xem:
143
Show I once asked this question to one of the Java developers during an Interview and like many others, he answered that Arrays.asList() can convert an array of primitive integer values to ArrayList From JDK 8 onwards, you either had to make a utility method or need to use general-purpose Java libraries like Google Guava or Apache Commons to convert an array of primitive values. byte, short, char, int, long, float, and double to ArrayList of Byte, Character, Short, Integer, Long, Float, and Double wrapper classes. Though from Java 8 onwards, you can use the java.util.Stream to convert an int[] to ArrayList Once you get the Stream of int values, you can use theboxed() method to convert it to Stream of Integer wrapper objects. After that you can just convert Stream to List as shown in that article i.e. you can use thecollect() method of the stream with Collectors.toList() to get a list. int[] to ListIf you want ArrayList instead of List then you can pass a Supplier to Collectors, which will accumulate elements into an ArrayList as shown below:int[] primes = new int[]{2, 3, 5, 7, 11, 13, 17}; ArrayList<Integer> list = IntStream.of(primes) .boxed() .collect(Collectors.toCollection(ArrayList::new)); Alternatively, you can also use the copy constructor of the Collection interface to create an ArrayList by copying all elements from the List instance as shown in the following example: List<Integer> list = IntStream.of(primes) .boxed() .collect(Collectors.toList()); ArrayList<Integer> arraylist = new ArrayList<>(list); What is this code doing? Well, if you are not very familiar with Java 8, Stream, and method reference then here is a step by step explanation of the above one-liner of converting a primitive array to ArrayList in JDK 8: 1) The IntStream.of(primes) is converting an int array to an IntStream object. 2) The boxed() method of IntStream convert applies boxing on each element of IntStream and returns a Stream of Integer i.e it converts IntStream to Stream 3) The collect() method collects all elements of Stream into any Collection class by using Collectors of Java 8. A Collector encapsulates the functions used as arguments to collect(Supplier, BiConsumer, BiConsumer), allowing for reuse of collection strategies and composition of collect operations such as multiple-level grouping or partitioning. 4) The Collectors.toCollection() method collects elements of Stream into a Collection which is specified by the first parameter. Here we are passing ArrayList::new which is a constructor reference, which means all elements of Stream will be collected into an ArrayList. SeeThe Complete Java MasterClassto learn more. How to convert int[] to ArrayList of Integer in Java 8By far, That was the simplest way to convert an array of primitive data types to a List of wrapper objects like an array of ints to ArrayList of Integers.Btw, there is another way to convert a primitive array to ArrayList in Java 8 by using theArrays.stream()method, which converts an array to Stream in Java 8. This method is equivalent toIntStream.of(int[])and return an IntStream. The rest of the code will remain the same i.e. you can use the following one-liner to convert anint[]to ArrayList in JDK 8:
ArrayList<Integer> list = Arrays.stream(ints) .boxed() .collect(Collectors.toCollection(ArrayList::new)); Java Program to convert int Array to ArrayList of Integer in JavaHere is the full Java program to convert an int[] to ArrayListimport java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; import java.util.stream.IntStream; /* * Java Program to convert a long[] to ArrayList From the output, it's clear that both ways of converting an array of primitive values to ArrayList of objects work fine. You can use whatever you want. SeeFrom Collections to Streams in Java 8 Using Lambda Expressionsto learn more. How to convert int[] to ArrayList of Integer in Java 6,7If you are not running in Java 8 and want to convert int[] to ArrayList<> on an earlier version of Java, like JDK 6 and JDK 7, then you have two choices, either use Google's Guava or Apache commons library as shown here or write your own conversion method as shown below:/* * Java Program to convert int[] to ArrayList The code is pretty simple and self-explanatory. We are iterating over ArrayList using enhanced for loop of Java 5 and adding each int value from array to ArrayList. When you add, Java uses autoboxing to convert an int value to an Integer object, so in the end, you have an ArrayList of Integer. Though, you can also use other ways to loop over an array, like by using for or while loop. One slightly important thing to note is initializing ArrayList with the length of the array. This prevents ArrayList from being resizing multiple times. If you remember, by default ArrayList initializes with 10 elements and then keeps resize when it is about to fill, I guess when the load factor grows up to 0.75 it resizes and doubles itself. This involves a lot of array copy, which can slow down your application. It's also a Java collection best practice to provide size while creating an instance of any Collection class like ArrayList. See these Java Performance coursesfor more such tips. That's all about how to convert an int[] to ArrayList Related Java 8 Tutorials If you are interested in learning more about the new features of Java 8, here are my earlier articles covering some of the important concepts of Java 8:
Thanks a lot for reading this article so far. If you like this article then please share it with your friends and colleagues. If you have any questions or feedback then please drop a comment. P. S. - In general, you should use Stream if you are using Java 8, it's cleaner than the Java 6 approach and also takes just one line to convert a primitive array to ArrayList. If you don't know much about Stream and Java 8, I suggest you read a good book on Java 8 to learn more aboutIntStreamand other Stream features likeJava 8 in Action. |