java - Can I use a Comparator without implementing Comparable? -
Is it possible to use comparative without applying a comparative range? For example, if I had the following:
MyClass {Comparator comp; Ordered List Inheritance (Compare C) {this.comp = c; }}
Can I then use Comp to compare two objects? If so, how would I do this?
Thank you ...
You do not use you use.
Comparative
is an interface implemented by the object to specify its order sequence with another type of object.
Comparator
is a common interface that only takes two objects and tells you their sort order. You can do this:
Public class students {Private Final IND; Name of private final string; Private finals end age; Public student (int id, string name, int era) {this.id = id; This.name = name; This.age = age; } Public int getId () {return ID; } Public string getName () {return name; } Public int getAge () {return age; }} With
:
Public Classes Age Compaq Tools Companator & lt; Students & gt; {Comparison of Public Entry (Student S1, Student S2) {if (s1.getAge () == s2.getAge ()) {Return 0; } And {return s1.getAge () & lt; S2.getAge ()? -1 1; }}
and:
Comments
Post a Comment