Java static generic methods using examples - Tools

Introduction: When multiple tools to model class sorting, comparison and other operations, the need to write a lot of code duplication, because how lazy always want to save, so consider this stuff to simplify the code using generics

 

Case: The current model there are two classes, Fruit and Person, but they need to sort the business logic methods vary, and therefore need to write two sorting methods are, but because the same sort of place too, the only difference is that two judges magnitude relation object, doing so in this simplified operation.

 

Steps:

1, the preparation of model class interface interface Model 

1 public interface Model<T> {
2     public int compareTo(T model);
3 }

Here only we need to define a method of comparison

2, the preparation of the Person class class Person

 1 public class Person implements Model<Person> {
 2     private String name;
 3 
 4     public Person(String _name) {
 5         this.name = _name;
 6     }
 7 
 8     @Override
 9     public int compareTo(Person _person) {
10         return this.name.compareToIgnoreCase(_person.name);
11     }
12 }

Fruit of the code I do not impress, because for example I write maybe the comparison method is the same, but the actual business code may not be the same

3, writing tools class Sort

 1 public class Sort {
 2 
 3     public static <T extends Model<T>> void sort(List<T> list) {
 4         for (int i = 0; i < list.size() - 1; i++) {
 5             for (int j = i; j < list.size(); j++) {
 6                 if (list.get(i).compareTo(list.get(j)) > 0) {
 7                     T swap = list.get(i);
 8                     list.set(i, list.get(j));
 9                     list.set(j, swap);
10                 }
11             }
12         }
13     }
14 }

Easily found a sorting algorithm on the set go

Then the main function is to call

 1 public class Main{
 2     public static void main(String[] args){
 3         LinkedList<Person> list = new LinkedList<>();
 4         list.add(new Person("admin"));
 5         list.add(new Person("root"));
 6         list.add(new Person("huawei"));
 7         list.add(new Person("cisco"));
 8         Sort.sort(list);
 9         for (Person item : list) {
10             System.out.println(item.toString());
11         }
12     }
13 }

4, Fruit classes can be implemented as Model compareTo method according to the Person class can be sorted using methods Sort.sort

 

Postscript: My idea is beginning to write a Model interfaces, write a compareTo method in the interface, then the parameter type does not know how to write

If you write Model, then, when Person achieve compareTo method parameter type is certainly Model

I this logic is relatively simple, compares with the name field, if after the Model Interface plus getName method, how can one not comfortable

Model class may not have the name field, and such Person class may be compared with the Fruit category

Although it can not do so in logic, but how can see it uncomfortable ah, so he thought this thing out with generics

Guess you like

Origin www.cnblogs.com/panther1942/p/11498945.html