In programming, it’s often necessary to compare two values and determine which one is larger.
This is particularly useful when working with strings, as they can be used to represent a wide range of data types and values.
The method “max” is a common operation in many programming languages that takes two parameters and returns the larger of the two.
In this particular case, we’re interested in a method that takes two string parameters and returns the larger of the two.
Here we will explore how to write a “max” method that works specifically with strings and discuss some of the considerations that go into implementing this functionality.
- Method max that has two string parameters and returns the larger of the two using String.compareTo()
- Method max that has two string parameters and returns the larger of the two using String.length()
- Method max that has two string parameters and returns the larger of the two using conditional operator.
Method max that has two string parameters and returns the larger of the two using String.compareTo()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | public class StringComparision { public static String max(String str1, String str2) { if (str1.compareTo(str2) > 0) { return str1; } else { return str2; } } public static void main(String args[]) { String s1="Hello kaise ho ji"; String s2="How are you"; String result=StringComparision.max(s1,s2); System.out.println("Largest String is: "+result); } } |
This implementation uses the compareTo
method of String
to compare the two input strings lexicographically (i.e., by their dictionary order). If str1
is greater than str2
according to this ordering, then str1
is returned; otherwise, str2
is returned.
1 2 3 4 5 6 7 8 9 10 11 | public static String max(String str1, String str2) { if (str1 == null) { return str2; } else if (str2 == null) { return str1; } else if (str1.compareTo(str2) > 0) { return str1; } else { return str2; } } |
in above implementation assumes that both input strings are non-null. If either input string is null
, then the compareTo
method will throw a NullPointerException
. If you want to handle this case more gracefully, you could add a null check to the method:
Method max that has two string parameters and returns the larger of the two using String.length()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | public class StringComparision { public String maxString(String s1, String s2){ if(s1.length()>s2.length()){ return s1; } else{ return s2; } } public static void main(String args[]) { String s1="Hello kaise ho ji"; String s2="How are you"; StringComparision sc=new StringComparision(); String result=sc.maxString(s1,s2); System.out.println("Largest String is: "+result); } } |
here maxString
method first checks the length of the two input strings s1
and s2
using the length()
method of String
. If s1
is longer than s2
, then s1
is returned; otherwise, s2
is returned.
1 | Largest String is: Hello kaise ho ji |
Method max that has two string parameters and returns the larger of the two using conditional operator
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | public class StringComparision { public String maxString(String s1, String s2){ return (s1.length()>s2.length())? s1:s2; } public static void main(String args[]) { String s1="Hello kaise ho ji"; String s2="How are you"; StringComparision sc=new StringComparision(); String result=sc.maxString(s1,s2); System.out.println("Largest String is: "+result); } } |
1 | Largest String is: Hello kaise ho ji |
Using Collections.max
method:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | import java.util.Collections; import java.util.List; import java.util.Arrays; public class StringComparision { public static String max(String str1, String str2) { List<String> list = Arrays.asList(str1, str2); return Collections.max(list); } public static void main(String args[]) { String s1="Hello kaise ho ji"; String s2="How are you"; String result=StringComparision.max(s1,s2); System.out.println("Largest String is: "+result); } } |
Using Stream.max
method:
1 2 3 4 5 6 7 8 9 10 11 12 13 | import java.util.stream.Stream; public class StringComparision { public static String max(String str1, String str2) { return Stream.of(str1, str2).max(String::compareTo).get(); } public static void main(String args[]) { String s1="Hello kaise ho ji"; String s2="How are you"; String result=StringComparision.max(s1,s2); System.out.println("Largest String is: "+result); } } |
Using Math.max
method:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | import java.util.stream.Stream; public class StringComparision { import java.util.stream.Stream; public class StringComparision { public static String max(String str1, String str2) { return (Math.max(str1.length(), str2.length()) == str1.length()) ? str1 : str2; } public static void main(String args[]) { String s1="Hello kaise ho ji"; String s2="How are you"; String result=StringComparision.max(s1,s2); System.out.println("Largest String is: "+result); } } public static void main(String args[]) { String s1="Hello kaise ho ji"; String s2="How are you"; String result=StringComparision.max(s1,s2); System.out.println("Largest String is: "+result); } } |
Using if-else
statement:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | import java.util.stream.Stream; public class StringComparision { public static String max(String str1, String str2) { if (str1.compareTo(str2) > 0) { return str1; } else { return str2; } } public static void main(String args[]) { String s1="Hello kaise ho ji"; String s2="How are you"; String result=StringComparision.max(s1,s2); System.out.println("Largest String is: "+result); } } |
Using a ternary operator with a nested compareTo
method:
1 2 3 4 5 6 7 8 9 10 11 12 13 | import java.util.stream.Stream; public class StringComparision { public static String max(String str1, String str2) { return (str1.length() > str2.length()) ? str1 : (str1.compareTo(str2) > 0) ? str1 : str2; } public static void main(String args[]) { String s1="Hello kaise ho ji"; String s2="How are you"; String result=StringComparision.max(s1,s2); System.out.println("Largest String is: "+result); } } |
Using String.format
and a conditional operator:
1 2 3 4 5 6 7 8 9 10 11 12 13 | import java.util.stream.Stream; public class StringComparision { public static String max(String str1, String str2) { return String.format("%s", str1.length() > str2.length() ? str1 : str2); } public static void main(String args[]) { String s1="Hello kaise ho ji"; String s2="How are you"; String result=StringComparision.max(s1,s2); System.out.println("Largest String is: "+result); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 | import java.util.stream.Stream; public class StringComparision { public static String max(String str1, String str2) { return (str1.length() > str2.length()) ? str1 : (str1.length() == str2.length()) ? str1 : str2; } public static void main(String args[]) { String s1="Hello kaise ho ji"; String s2="How are you"; String result=StringComparision.max(s1,s2); System.out.println("Largest String is: "+result); } } |
Here we discussed Write a method max that has two string parameters and returns the larger of the two.