Junior — Middle
In what types of collections have you used Stream methods in Java?
sobes.tech AI
Answer from AI
Stream methods in Java are usually applied to collections that implement the Collection interface, such as lists (List), sets (Set), and queues (Queue). I most often used Stream with ArrayList and HashSet for filtering, transforming, and aggregating data. You can also create streams from arrays using Arrays.stream(). Example of using Stream with a List:
List<String> names = Arrays.asList("Anna", "Bob", "Charlie");
List<String> filtered = names.stream()
.filter(name -> name.startsWith("A"))
.collect(Collectors.toList());
Additionally, Stream can be applied to a Map through methods like entrySet(), keySet(), or values(), for example:
Map<Integer, String> map = new HashMap<>();
map.put(1, "one");
map.put(2, "two");
List<String> values = map.values().stream()
.filter(v -> v.length() > 3)
.collect(Collectors.toList());