comparator

Comparing two Integers with my own Comparator

孤街醉人 提交于 2019-12-24 04:44:08
问题 I am learning how to use Comparator interface in java and I am trying to write my own Comparator which would compare Integers differently ( e.g 3>5 ). I have a problem with it, could someone tell what is wrong with my code? import java.util.*; import java.lang.*; class MyComparator<Integer> implements Comparator<Integer> { public int compare(Integer a, Integer b) { if(a.compareTo(b)>0) return -1; else if(a.compareTo(b)<0) return 1; else return 0; } } The compilator cannot find compareTo

Java Comparator Arrays.sort()

帅比萌擦擦* 提交于 2019-12-24 03:00:30
问题 I want to sort an array of 2 dimensional arrays in Java according to some rules, let's say the distance from the origin. I saw several ways of doing it using Arrays.sort() : 1) Arrays.sort(points, Comparator.comparing(p -> p[0]*p[0] + p[1]*p[1])); 2) Arrays.sort(points, (p1, p2) -> p1[0]*p1[0] + p1[1]*p1[1] - p2[0]*p2[0] - p2[1]*p2[1]); 3) Defining the class : class Point implements Comparable<Point>{ // class variables, constructor public int compareTo(Point p) { return (x*x + y*y).compareTo

Comparing students according to score JAVA

大兔子大兔子 提交于 2019-12-24 01:16:32
问题 I have a class of Student. The class has String name , int studentId and int studentScore . I want to compare the students in ArrayList<Student> students according to the studentscore. This means if "student 1" and "student 2" has the same amount of studentscore then both of their ranks will be the same. I have a function where I set The rank. But I'm unsure how to compare everything in the arraylist. When I search for comparing int values I get the results: How to properly compare two

Comparison method violates its general contract when sorting files

对着背影说爱祢 提交于 2019-12-23 20:11:28
问题 I know there are a lot of questions with this kind of exception, and I did found the solution, but my problem is that the same code in different project doesn't throw an exception while this one does. Both projects have the same version of Java and other libraries. Basically I have this small function that retrieves list of files from directory, sorts them by timestamp, and then returns list of absolute file names: public static List<String> getFiles(String dir) { List<String> fileList = new

Can Comparator compare two objects of different type?

风格不统一 提交于 2019-12-23 19:02:15
问题 I have two objects of classes Person and Employee. Both classes have common attribute age. And I have added few objects of both theses classes to an Arraylist and now I want two write one Comparator and pass it to sort method of Collections class. And want the list to be sorted on the basis of age. I am trying this just for getting more clarity on use of Comparable and Comparator in Java. EDIT: Reason Why I was asking this question was that I was not clear about Comparator and Comparable. I

Printing arraylist into output file?

。_饼干妹妹 提交于 2019-12-23 17:53:16
问题 FOLLOW UP QUESTION HERE: Java: Printing Arraylist to Output File? For some reason, it takes an extraordinary amount of time for the ArrayLists to be printed to the output file, usually 20-30 minutes. However, this only happens with the sort methods or the filterTitle or filterArtist methods (methods that concern String inputs). When I run filterRank or filterYear , it runs perfectly fine. When I print the song2 ArrayList directly from the filter methods, the only thing that is printed is [] ,

VHDL directly comparing vectors

守給你的承諾、 提交于 2019-12-23 17:22:36
问题 I was wondering if its possible to directly compare 2 vectors with eachother instead of just looking at them bit by bit. For example: entity Comparator is port(a,b in: std_logic_vector (2 downto 0); out1, out2 out: std_logic); end Comparator; architecture behavioural of Comparator1 is begin if a = b then out1 <= '1' else if /= then out2 <= '1' end if; end behaviour; Is this possible? 回答1: The answer is yes, you can compare two array types of the same type and subtype indication directly.

TreeSet Custom Comparator Algo .. String Comparision

对着背影说爱祢 提交于 2019-12-23 16:52:31
问题 From the input string provided: { "200,400,7,1", "100,0,1,1", "200,200,3,1", "0,400,11,1", "407,308,5,1","100,600,9,1" } , I am adding the same in a TreeSet and want it to be sorted with the 3rd element order, so the expected output will be: (100,0,1,1) (200,200,3,1) (407,308,5,1) (200,400,7,1) (100,600,9,1) (0,400,11,1) But my actual output is: (100,0,1,1)(0,400,11,1)(200,200,3,1)(407,308,5,1)(200,400,7,1)(100,600,9,1) But since the string comparison of 11 is less than 9 but in terms of

Java Interface Comparator static compare

北城以北 提交于 2019-12-23 12:11:20
问题 int compare(Object o1, Object o2) Compares its two arguments for order. For compare 2 objects o1 and o2 need do something like: MyClass o1=new MyClass(); MyClass o2=new MyClass(); if (o1.compare(o1,o2)>0) ...... Why this methtod not static? If method was static possible like: if (MyClass.compare(o1,o2)>0) .... 回答1: If it were static, how could it be called polymorphically? The point of Comparator is that you can pass an instance into something like sort ... which has to then call the compare

Passing Object type and Field to Comparator

纵饮孤独 提交于 2019-12-23 04:24:32
问题 Is it possible to write a Comparator so that I can pass the Object type, the field type and the field I like to sort on? I've made some small changes to http://www.davekoelle.com/files/AlphanumComparator.java to accommodate sorting on the field email of type String in the Object type User . I have this code that works. public class Main { public static void main(String[] args) { List<User> users = new ArrayList<>(); users.add(new User(7, "user1", "user1@c.com")); users.add(new User(11,