String::length is a method reference. Using it is equivalent to
Comparator.comparing(s -> s.length())
So it compares strings by comparing their length.
Answer from JB Nizet on Stack OverflowString::length is a method reference. Using it is equivalent to
Comparator.comparing(s -> s.length())
So it compares strings by comparing their length.
TL;DR: You are passing a method reference
Java 8 introduces lambda syntax to the Java language, this is a small part of that, it allows you to pass a method as a lambda.
So you call the method Comparator.comparing, this requires a "key extractor" function that takes an object from the Collection<T> and returns some object of a type U extends Comparable<? super U>.
The method String.length returns an Integer (when boxed) and an Integer is comparable to another Integer. So the call Comparator.comparing(String::length) returns a Comparator<String> that:
- takes a pair of
String,s1, s2 - calls
l1 = s1.lengthandl2 = s2.length - returns
l1.compareTo(l2)
So, Why there is no parenthesis?
Because you are passing a method reference. This method will be called later on an as yet undefined instance. Notice that length is not static on String so String.length() would not compile.
Videos
A simple solution is to iterate over all names and compare the length using the .length() method.
Another solution is to compare the length each time you enter a string (only one for loop). Here's an example :
int nb = 2;
String[] names = new String[nb];
int maxLength = -1; // Default value
for (int i = 0; i < nb; i++) {
names[i] = JOptionPane.showInputDialog("Enter your name : ");
// Compare the length of this name to the previous greatest length
if (names[i].length() > maxLength) {
// Change the maximum
maxLength = names[i].length();
}
}
JOptionPane.showMessageDialog(null, "The greatest value is " + maxLength);
You can iterate all the names and use the .length()method in String:
String longest = "";
for (String name : names) {
longest = name.length() > longest.length() ? name : longest;
}
This will iterate through your name list (names) and assign the longest to the variable longest
== tests for reference equality (whether they are the same object).
.equals() tests for value equality (whether they contain the same data).
Objects.equals() checks for null before calling .equals() so you don't have to (available as of JDK7, also available in Guava).
Consequently, if you want to test whether two strings have the same value you will probably want to use Objects.equals().
// These two have the same value
new String("test").equals("test") // --> true
// ... but they are not the same object
new String("test") == "test" // --> false
// ... neither are these
new String("test") == new String("test") // --> false
// ... but these are because literals are interned by
// the compiler and thus refer to the same object
"test" == "test" // --> true
// ... string literals are concatenated by the compiler
// and the results are interned.
"test" == "te" + "st" // --> true
// ... but you should really just call Objects.equals()
Objects.equals("test", new String("test")) // --> true
Objects.equals(null, "test") // --> false
Objects.equals(null, null) // --> true
From the Java Language Specification JLS 15.21.3. Reference Equality Operators == and !=:
While
==may be used to compare references of typeString, such an equality test determines whether or not the two operands refer to the sameStringobject. The result isfalseif the operands are distinctStringobjects, even if they contain the same sequence of characters (ยง3.10.5, ยง3.10.6). The contents of two stringssandtcan be tested for equality by the method invocations.equals(t).
You almost always want to use Objects.equals(). In the rare situation where you know you're dealing with interned strings, you can use ==.
From JLS 3.10.5. String Literals:
Moreover, a string literal always refers to the same instance of class
String. This is because string literals - or, more generally, strings that are the values of constant expressions (ยง15.28) - are "interned" so as to share unique instances, using the methodString.intern.
Similar examples can also be found in JLS 3.10.5-1.
Other Methods To Consider
String.equalsIgnoreCase() value equality that ignores case. Beware, however, that this method can have unexpected results in various locale-related cases, see this question.
String.contentEquals() compares the content of the String with the content of any CharSequence (available since Java 1.5). Saves you from having to turn your StringBuffer, etc into a String before doing the equality comparison, but leaves the null checking to you.
== tests object references, .equals() tests the string values.
Sometimes it looks as if == compares values, because Java does some behind-the-scenes stuff to make sure identical in-line strings are actually the same object.
For example:
String fooString1 = new String("foo");
String fooString2 = new String("foo");
// Evaluates to false
fooString1 == fooString2;
// Evaluates to true
fooString1.equals(fooString2);
// Evaluates to true, because Java uses the same object
"bar" == "bar";
But beware of nulls!
== handles null strings fine, but calling .equals() from a null string will cause an exception:
String nullString1 = null;
String nullString2 = null;
// Evaluates to true
System.out.print(nullString1 == nullString2);
// Throws a NullPointerException
System.out.print(nullString1.equals(nullString2));
So if you know that fooString1 may be null, tell the reader that by writing
System.out.print(fooString1 != null && fooString1.equals("bar"));
The following are shorter, but itโs less obvious that it checks for null:
System.out.print("bar".equals(fooString1)); // "bar" is never null
System.out.print(Objects.equals(fooString1, "bar")); // Java 7 required
You need to specify a type parameter for Comparator for your implementation to work.
class comp implements Comparator<String> {
public int compare(String o1, String o2) {
if (o1.length() > o2.length()) {
return 1;
} else if (o1.length() < o2.length()) {
return -1;
} else {
return 0;
}
}
}
In Java 1.7 and later, you can also simplify the body of this method to:
class comp implements Comparator<String> {
public int compare(String o1, String o2) {
return Integer.compare(o1.length(), o2.length());
}
}
Also, Collections.sort sorts List objects. Since you're sorting an array, you should use Arrays.sort:
Arrays.sort(array, new comp());
You need to use Arrays.sort() method if data source is an array.
For instance,
String []array={"first","second","third","six"};
Arrays.sort(array,new Comparator<String>()
{
public int compare(String s1,String s2)
{
return s1.length() - s2.length();
}
});
Or convert array to List to use Collections.sort() method,
Collections.sort(Arrays.asList(array),new Comparator<String>()
{
public int compare(String s1,String s2)
{
return s1.length() - s2.length();
}
});
Show your teacher this!
Stream.generate(scanner::next).limit(5)
.max(Comparator.comparing(String::length))
.ifPresent(System.out::println);
if/else and loops are the same for this problem.
String largerString = q;
if(w.length()>largerString.length())
{
largerString = w;
}
if(e.length()>largerString.length())
{
largerString = e;
}
if(r.length()>largerString.length())
{
largerString = r;
}
if(t.length()>largerString.length())
{
largerString = t;
}
System.out.println(largerString);
Hey, r/learnprogramming. A quick view of my posts will show that this is new and unchartered territory for me, breaking out of r/atheism, which is where I usually lurk. Unfortunately, it is not on good terms. I missed the day in class that my AP CompSci teacher taught interfaces, but I was given the assignment nonetheless, and I'm trying my best to do it. I've gotten PRETTY close, but I'm still just a few lines of code off from victory, and I am simply out of original ideas and book/internet resources. I have enclosed the typed up question as well as my code thus far.
-
Write a class Compare3 that provides a static method largest. Method largest should take three Comparable parameters and return the largest of the three (so its return type will also be Comparable). Recall that method compareTo is part of the Comparable interface, so largest can use the compareTo method of its parameters to compare them. See Section 5.5 of the text for a description of the compareTo method.
-
Write a class Comparisons whose main method tests your largest method above. First prompt the user for and read in three strings, use your largest method to find the largest of the three strings, and print it out. (It's easiest to put the call to largest directly in the call to println.) Note that since largest is a static method, you will call it through its class name, e.g., Compare3.largest(val1, val2, val3). Add code to also prompt the user for three integers. Try to use your largest method to find the largest of the three integers. What error message do you get if you pass the integers in directly? This is a situation where the Integer wrapper class is useful. Create a new Integer object to hold each of the integers and pass the objects to largest. Again, it's easiest if you put the call directly in the call to println.
Here's a link to the GoogleDoc for my answer to question 1: https://docs.google.com/document/d/1tO2EHCkJeCVeWXfT0qupudkNZkkTBEJXotSSsORsPck/edit?hl=en&authkey=COWSiJwB
Here's a link to the GoogleDoc for my answer to questions 2: https://docs.google.com/document/d/1-Wq9SWB-7X7TYmpX94qY7W_S4ZYZyFxBBKTSB5aJM9E/edit?hl=en&authkey=CMicz_gD
Question two is of lower priority as I have more time to figure that one out (and I don't suspect it to be an issue), although unfortunately my time is up for question one. I have spent today from 11:30 until now trying to work out this damn code, and although I'm not here to whine, it's tough being a high school CompSci student, hahah.
But yeah, any resources, advice, and help is much appreciated! I was happy to learn of the existence of this place, as it means I have an oppurtunity to wander a little bit, find out what Reddit is like outside of r/atheism.
I think the error lies in my if statements with the greater than and less thats, and I think there may be another one creeping in the interface part at the top, as I'm just doing that out of the authority of my book, I don't really understand it. I just need some pointing in the right direction.