// Java program to compare time using// compareTo() methodimportjava.util.*;importjava.time.*;publicclassMain{publicstaticvoidmain(String[]args){LocalTime time1;LocalTime time2;LocalTime time3;time1=LocalTime.of(10,15,18);//10:15:18time2=LocalTime.of(11,45,46);//11:45:46time3=...
// Java program to compare dates using// Date.compareTo() methodimportjava.util.*;publicclassMain{publicstaticvoidmain(String[]args){Date date1=newDate(22,9,16);Date date2=newDate(21,10,15);Date date3=newDate(21,10,15);intresult=0;result=date1.compareTo(date2);if(result>0)System...
("C2", "Red"); tree_map1.put("C4", "Green"); tree_map1.put("C3", "Black"); tree_map1.put("C1", "White"); System.out.println(tree_map1); } } class sort_key implements Comparator<String>{ @Override public int compare(String str1, String str2) { return str1.compareTo(...
import java.util.Arrays; public class LambdaExpressionEx { public static void main(String[] args) { String[] words = { "kind", "massive", "atom", "car", "blue" }; Arrays.sort(words, (String s1, String s2) -> (s1.compareTo(s2))); ...
Comparator<String> comparator = new Comparator<String>() { public int compare(String s1, String s2) { String[] strings1 = s1.split("\\s"); String[] strings2 = s2.split("\\s"); return strings1[strings1.length - 1] .compareTo(strings2[strings2.length - 1]); } }; This exampl...
Comparing Time in Java: A Guide [duplicate] could be the Solution 1: use compareTo(). date1.compareTo(date2); from java docs: The returned value will be 0 if the provided Date is equivalent to this Date. If this Date occurs before the Date argument, the returned value will be a ne...
// Furthermore, all such elements must be mutually comparable: e1.compareTo(e2) must not throw a ClassCastException for any elements e1 and e2 in the set. Set<String>treeSetList =newTreeSet<String>(myList); System.out.printf("\n=== Unique values using TreeSet: %s", treeSetList);...
MyList.stream().sorted((obj1,obj2)->obj1.getItem().getValue().compareTo(obj2.getItem().getValue())).forEach(System.out::println); Below is an example of a sorted stream in reverse order. importjava.util.*;publicclassStreamCompareToExample{// Main functionspublicstaticvoidmain(String[]...
().compareTo(n.getUid()) == 0) { this.setTitle(n.getTitle()); this.setNote(n.getNote()); } else { throw new IllegalArgumentException("note"); } } /* background thread to simulate some background process that make changes */ public void startNodeBackgroundThread(ActionEvent ...
The object methods equals and compareTo in java.sql.Date and java.sql.Time will yield erroneous results if compared objects are constructed in different time zones.import java.sql.SQLException; String dateString = "2003-03-24"; java.sql.Date date = java.sql.Date.valueOf(dateString); ...