There are many ways to get a string from a list in Java. Here we will explore all possible ways and choose an easier one.
Understanding Lists in Java
A List is an ordered collection in Java, which is part of Java.util package. It can store elements like integers, strings, or objects. The most commonly used implementation is ArrayList.
Key Features of a List:
- Index-based: Elements are accessed using their index (starting from 0).
- Dynamic size: It can grow or shrink as needed.
- Preserves order: The order of insertion is maintained.
Ways To Create a List of Strings in Java
There are many ways to initialise the list in Java. Below are a few ways. We will use the list to get a string from a list in Java
Using ArrayList Object
List<String> itemsList = new ArrayList<>(); itemsList.add("Book"); itemsList.add("Pen"); itemsList.add("Ink"); itemsList.add("Glue"); itemsList.add("stapler");
Using Arrays.asList
List<String> watchesList = Arrays.asList("Patek Philip", "Tag Heuer", "Rolex");
Using List.of
(Java 9 and Above)
List<String> shoeList = List.of("Don Carlos", "Hush Puppies", "Urban Sole");
Using a Stream API (Java 8 and Above)
List<String> shirtsList = Stream.of("Levi's", "Mark & Spencer", "Polo", "Zara") .collect(Collectors.toList());
Using Collections.addAll
List<String> BooksList = new ArrayList<>(); Collections.addAll(BooksList, "The Book Theif","40 Rules Of Love","Broken Wings","God Of Small Things");
Details of Methods
Method | Modifiable | Allows Nulls | Introduced In |
---|---|---|---|
ArrayList | Yes | Yes | Java 1.2 |
Arrays.asList | No | Yes | Java 1.2 |
List.of | No | No | Java 9 |
Stream | Yes | Yes | Java 8 |
Collections.addAll | Yes | Yes | Java 1.5 |
Each method has its advantages and is suitable for different scenarios. Choose the one that best fits your needs!
Ways To Get A String From A List In Java
We have completed the list initialisation section. We found the maximum ways to do so also. Now we will proceed to extract data from the list.
Get A String From A List In Java Using A For Loop
for (int i = 0; i < watchesList.size(); i++) { System.out.println("Fruit: " + watchesList.get(i)); }
Get A String From A List In Java Using A For-Each Loop
for (String watch : watchesList) { System.out.println("Fruit: " + watch); }
watchesList.forEach(watch -> { System.out.println("watch name : "+watch); });
Get A String From A List In Java Using Streams (Java 8+)
watchesList.stream() .forEach(watch -> System.out.println(watch));
Or to filter the strings with the key available:
List<String> filtered = watchesList.stream() .filter(watch -> watch.startsWith("T")) .collect(Collectors.toList()); filtered.forEach(watch -> { System.out.println(watch); });
Get A String From A List In Java Using subList
Method
List<String> watchesSubList = watchesList.subList(0, 2); System.out.println("Sublist: " + watchesSubList);
Get A String From A List In Java Using A Parallel Stream
List<String> parallelStreamList = watchesList.parallelStream() .filter(watch -> watch.startsWith("P")).collect(Collectors.toList()); parallelStreamList.forEach(watch -> { System.out.println(watch); }); // You may use key: It is better way to use in search criteria
Conclusion
In this article, we learned how to get a string from a list in Java. We used different techniques to achieve the functionality. If you found this useful, encourage us.
Leave a Reply