Suppose you have a comma-separated list of String e.g. "Samsung, Apple, Sony, Google, Microsoft, Amazon" and you want to convert it into an ArrayList containing all String elements e.g. Samsung, Apple, Google, etc. How do you do that? Well, Java doesn't provide any such constructor or factory method to create ArrayList from delimited String, but you can use String.split() method and Arrays.asList() method together to create an ArrayList from any delimited String, not just comma separated one.
Learn Java and Programming through articles, code examples, and tutorials for developers of all levels.
How to create and initialize List or ArrayList in one line in Java? Example
creating and initializing List at the same time
Sometimes we want to create and initialize a List like ArrayList or LinkedList in one
line much like creating an array and initializing it on the same line. If you look at The array on Java programming language you can create and initialize both primitive
and object arrays e.g. String
array very easily in just one line but in order to create a List
equivalent of that array, you need to type a lot of code. This is also one of the
tricky
Java question sometimes appears in Interview as Write Java code to create and initialize ArrayList in the same line.
Java ArrayList Tutorials and Examples for Beginners (with Java ArrayList Cheat Sheet)
Hello guys, if you want to learn ArrayList in-depth and looking for a complete guide on ArrayList then you have come to the right place. Earlier, I have shared the best Java collection courses and in this article, I am going to share tutorials and examples to learn and master ArrayList in Java. In the last 10 years, I have written several ArrayList tutorials, touching different ArrayList concepts and many how-to-do examples with ArrayList. In this tutorial, I am giving a summary of each of them. Why? So that any Java beginner who wants to learn ArrayList in detail, can go through the relevant tutorial and learn.
Subscribe to:
Comments (Atom)