Java - Find Largest and Smallest word in a String

In this section, we will show you how to find longest word in as given String in Java.

1. Using For loop

2. Using Java 8 Streams

Find Largest Word: Using For loop

public class Main {

public static void main(String[] args) {

String inputString = "Java is an awesome programming language";
String[] strArray = inputString.split(" ");
String maxlengthWord = "";
for(int i = 0; i < strArray.length; i++){
if(strArray[i].length() >maxlengthWord.length()){
maxlengthWord = strArray[i];
}
}
System.out.println(maxlengthWord);
}
}

Here the logic is simple,

First, declare the string as a string literal.

Using the split() method, split the string based on whitespace. It returns an array of strings.

Declare an empty string; later, we use it to accumulate the longest word.

Iterate over the string array and check whether the length of the [i]th string is greater than the length of the maximum-length word. If so, maxlengthWord = strArray[i];

Finally print the longest word.

Console Output: 

programming


Find Smallest Word: Using For loop

public class Main {

public static void main(String[] args) {

String inputString = "Java is awesome";
String[] strArray = inputString.split(" ");
String minlengthWord = strArray[0];
for(int i = 1; i < strArray.length; i++){
if(strArray[i].length() <minlengthWord.length()){
minlengthWord = strArray[i];
}
}
System.out.println(minlengthWord);
}
}

Here the logic is simple,

First, declare the string as a string literal.

Using the split() method, split the string based on whitespace. It returns an array of strings.

Declare a string with value is first element of the array.

Iterate over the string array and check whether the length of the [i]th string is less than the length of the minimum-length word. If so, minlengthWord = strArray[i];

Finally print the smallest word.

Console Output: 

is


Find Largest Word: Using Java 8 Streams

import java.util.Arrays;
import java.util.Comparator;
import java.util.Optional;

public class Main {

public static void main(String[] args) {

String inputString = "Java is an awesome programming language";

Optional<String> maxlengthWord =
Arrays.stream(inputString.split(" ")).
max(Comparator.comparingInt(String::length));
if(maxlengthWord.isPresent()){
System.out.println(maxlengthWord.get());
}
}
}

First, declare the string as a string literal.

Using the split() method, split the string based on whitespace. It returns an array of strings.

stream() method accepts a mandatory parameter array which is the array of whose elements are to be converted into a sequential stream and returns a Sequential Stream from the array passed as the parameter.

max() method is terminal operation which returns the maximum element of this stream according to the provided Comparator.

comparingInt() method sorts a list of elements of any type using an integer sort key.

Console Output: 

programming


Find Smallest Word: Using Java 8 Streams

import java.util.Arrays;
import java.util.Comparator;
import java.util.Optional;

public class Main {

public static void main(String[] args) {

String inputString = "Java is awesome";

Optional<String> minlengthWord =
Arrays.stream(inputString.split(" ")).
min(Comparator.comparingInt(String::length));
if(minlengthWord.isPresent()){
System.out.println(minlengthWord.get());
}
}
}

First, declare the string as a string literal.

Using the split() method, split the string based on whitespace. It returns an array of strings.

stream() method accepts a mandatory parameter array which is the array of whose elements are to be converted into a sequential stream and returns a Sequential Stream from the array passed as the parameter.

min() method is terminal operation which returns the minimum element of this stream according to the provided Comparator.

comparingInt() method sorts a list of elements of any type using an integer sort key.

Console Output: 

is

More interesting topics,

Popular posts from this blog

Learn Java 8 streams with an example - print odd/even numbers from Array and List

Java Stream API - How to convert List of objects to another List of objects using Java streams?

Registration and Login with Spring Boot + Spring Security + Thymeleaf

Java, Spring Boot Mini Project - Library Management System - Download

ReactJS, Spring Boot JWT Authentication Example

Spring Boot + Mockito simple application with 100% code coverage

Top 5 Java ORM tools - 2024

Java - Blowfish Encryption and decryption Example

Spring boot video streaming example-HTML5

Google Cloud Storage + Spring Boot - File Upload, Download, and Delete