Below is a Java program that finds the longest and shortest strings in a list of strings:


import java.util.ArrayList;
import java.util.List;
public class LongestShortestStrings {
public static void main(String[] args) {
 List<String> strings = new ArrayList<>();
 strings.add("apple"); 
 strings.add("banana"); 
 strings.add("cherry"); 
 strings.add("date"); 
 strings.add("grape"); 
 strings.add("kiwi"); 
String longest = findLongestString(strings); 
String shortest = findShortestString(strings); 
 System.out.println("Longest string: " + longest); 
 System.out.println("Shortest string: " + shortest);
 } 
public static String findLongestString(List<String> strings) {
String longest = ""
for (String str : strings) { 
if (str.length() > longest.length()) { 
 longest = str; 
 }
 }
return longest; 
 } 
public static String findShortestString(List<String> strings)
if (strings.isEmpty()) {
return null;
 }
String shortest = strings.get(0);
for (String str : strings) {
if (str.length() < shortest.length()) {
 shortest = str; 
 }
 } 
return shortest; 
 } }

Explanation:

  1. We start by defining a class called LongestShortestStrings.
  2. In the main method, we create a list of strings called strings. You can modify this list with your desired input strings.
  3. We call two methods, findLongestString and findShortestString, to get the longest and shortest strings from the strings list.
  4. findLongestString method iterates through the list of strings and keeps track of the longest string found so far. It compares the length of each string with the current longest and updates it if a longer one is found.
  5. findShortestString method also iterates through the list of strings and keeps track of the shortest string found so far. It initializes the shortest string as the first element of the list and then compares the length of each subsequent string with the current shortest and updates it if a shorter one is found.

Both methods return the longest and shortest strings, respectively, and then the main method prints out the results.

Remember to modify the strings list in the main method with your own input if you want to test the program with different sets of strings.