The java program that transforms a list of strings based on the length of each string. If a string's length is even, it converts the string to uppercase; otherwise, it converts the string to lowercase. The program uses Java Streams and the map operation to perform this transformation. Here's an explanation of the code:
import java.util.ArrayList; import java.util.List; import java.util.stream.Collectors; public class ConvertCaseBasedOnLength { public static void main(String[] args) { List<String> fruits = new ArrayList<>(); fruits.add("Apple"); fruits.add("Banana"); fruits.add("Raspberries"); fruits.add("Cherry"); fruits.add("Mango"); fruits.add("Date"); fruits.add("Watermelon"); System.out.println("Original Strings : " + fruits); List<String> modifiedstrings = fruits.stream().map(s -> (s.length() % 2 == 0) ? s.toUpperCase() : s.toLowerCase()).collect(Collectors.toList()); System.out.println("Modified Strings : " + modifiedstrings); } }
Original Strings : [Apple, Banana, Raspberries, Cherry, Mango, Date, Watermelon] Modified Strings : [apple, BANANA, raspberries, CHERRY, mango, DATE, WATERMELON]
Learn All in Tamil © Designed & Developed By Tutor Joes | Privacy Policy | Terms & Conditions