Code Logo

Name Tag Cleanup with map()

Published at22 Apr 2026
Stream API Easy 0 views
Like0

A school event team receives a list of printed name tags, but many of them have extra spaces and inconsistent letter casing. Before the tags go to the printer, the team wants every name cleaned into a simple standard format.

Your task is to return a new list where each name is trimmed and converted to uppercase. The order of the names should stay the same as the original list.

For example, if the input is [" Ayu", "bima ", " Cici "], the result should be ["AYU", "BIMA", "CICI"]. If the input list is empty, the result should also be an empty list.

This challenge is a straightforward introduction to using map() in a Java stream. Every name goes through the same transformation, and the transformed values are collected into a new list.

Example Input & Output

Example 1
Input
names = []
Output
[]
Explanation

An empty list stays empty after mapping.

Example 2
Input
names = ["dino"]
Output
["DINO"]
Explanation

A one-item list still uses the same transformation rule.

Example 3
Input
names = [" Ayu", "bima ", " Cici "]
Output
["AYU", "BIMA", "CICI"]
Explanation

Each name is trimmed and converted to uppercase.

Algorithm Flow

Recommendation Algorithm Flow for Name Tag Cleanup with map()
Recommendation Algorithm Flow for Name Tag Cleanup with map()

Solution Approach

This problem is a good match for map() because every element in the list needs the same kind of transformation. No items are removed, no grouping is required, and the original order should stay intact. We simply take each string and turn it into its cleaned version.

The two transformations are small but important. First, call trim() so leading and trailing spaces disappear. Then call toUpperCase() so the final tag is normalized into one consistent uppercase style.

That makes the stream pipeline very direct:

return names.stream()
    .map(name -> name.trim().toUpperCase())
    .collect(Collectors.toList());

The stream walks through the list in encounter order, map() applies the cleanup to each name, and collect() builds the new list. Because the work is uniform for every item, this is exactly the kind of problem where map() reads naturally.

The runtime is O(n) for n names, and the extra space is proportional to the size of the returned list.

Best Answers

java - Approach 1
import java.util.List;
import java.util.stream.Collectors;

class Solution {
    public static List<String> cleanNameTags(List<String> names) {
        return names.stream()
                .map(name -> name.trim().toUpperCase())
                .collect(Collectors.toList());
    }
}