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:

list & lt; Students & gt; Students = New Arrestist & lt; Students & gt; (); Students.add (new student (1, "bob", 15)); Students.add (new student (2, "Jane", 14)); Students.add (new student (3, "Gary", 16)); SortedSet & LT; Students & gt; Set1 = new tree plant & lt; Students & gt; (New era commuter ()); Set1.addAll (students); (Student student: set 1) {// age of order}

Comments

Popular posts from this blog

c# - How to capture HTTP packet with SharpPcap -

php - Multiple Select with Explode: only returns the word "Array" -

php - jQuery AJAX Post not working -