Processing usernames for display
In a user management system, we need to display usernames stored in a database. Using Streams and .forEach(), we can efficiently process and print each username without using traditional loops.
All the necessary classes from java.util have been imported for you.
この演習はコースの一部です
Input/Output and Streams in Java
演習の手順
- Convert the
usernameslist into aStreamobject. - Print each element in the
streamusing a lambda expression.
実践的なインタラクティブ演習
このサンプルコードを完成させて、この演習に挑戦してみましょう。
public class UserDisplay {
public static void main(String[] args) {
List usernames = new ArrayList<>();
usernames.add("Alice123");
usernames.add("BobTheCoder");
usernames.add("CharlieDev");
usernames.add("David99");
// Convert list to Stream
Stream stream = usernames.____();
// Print each username
stream.____(name -> System.out.println("User: " + name));
}
}