get Longest String - Java java.lang

Java examples for java.lang:String Array

Description

get Longest String

Demo Code


//package com.java2s;

public class Main {
    /**//from   www  .j  ava2  s.  c  om
     * @param stringList list of strings
     * @return longest string. If iterable is empty, "" is returned. If two strings are equally long, the first is returned.
     */
    public static String getLongestString(Iterable<String> stringList) {
        String forReturn = "";
        for (String string : stringList) {
            if (string.length() > forReturn.length()) {
                forReturn = string;
            }
        }
        return forReturn;
    }
}

Related Tutorials