java ------------ set (four) the Collections class

Collections class is a collection of java classes provided by the operating tool,

  It contains a large number of static methods for implementing the sorting collection element, operations such as search and replace. Similarly Arrays class values,

        Collection and are different, Collections class operation is set, is a set of interfaces Collection

 

1, the elements of the collection and sorting find and replace

To achieve the object class a total ordering, this class to implement the Comparable interface, this interface imposes its object to realize a total ordering of each class, called the natural order,

Tired the compareTo () method is called its natural comparison method, for sequentially comparing this object with the specified object, if the object is less than, equal to or greater than the specified object, a negative integer, zero or a positive integer

int compareTo (Object obj): compare this object than the specified object, small anti-negative integer, and so on back to zero, back to the big positive integer parameter specifies the object obj to compare 

package com.obge.model;

// can achieve the purpose of comparison between the size of the elements

//实现 Comparable 接口
public class Student implements Comparable {
    private int number = 0;
    private String name = "";
    private String gender="";

    public Student() {
    }

    public Student(int number, String name, String gender) {
        this.number = number;
        this.name = name;
        this.gender = gender;
    }

    public int getNumber() {
        return number;
    }

    public void setNumber(int number) {
        this.number = number;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }   
    
    // override method compareTO 
    @Override
     public  int the compareTo (Object O) {
        Student Student = (Student) O;
         // use the student number is compared 
        IF ( the this .number == student.number) {
             return 0 ; 
             // if the number is greater than the incoming students' learning school students No. 
        } the else  IF ( the this .number> student.number) {
             return . 1 ;
        }else {
            return -1;
        }
       
    }

    
    
}

Compare the size can only be used between the elements of the class sort Collections () method sorts the elements.

List interface is orderly, it can be sorted, Map interfaces not oh

 

package com.obge.boge;

import com.obge.model.Student;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;

public class CollectionsStu {
    public static void main(String[] args) {
        Student student1 = new Student();
        student1.setNumber ( 1 );
        Student student2 = new Student();
        student2.setNumber ( 7 );
        Student student3 = new Student();
        student3.setNumber ( 2 );
        Student student4 = new Student();
        student4.setNumber ( . 11 );
         // get hold of the objects set student put 
        the ArrayList the arrayList = new new the ArrayList ();
        arrayList.add(student1);
        arrayList.add(student2);
        arrayList.add(student3);
        arrayList.add(student4);

        System.out.println ( "before ordering --------- -------" );
        Iterator iterator = arrayList.iterator();
        while (iterator.hasNext()){
            Student stu = (Student) iterator.next();
            System.out.println(stu.getNumber());
        }
        System.out.println ( "Sort after --------- -------" );
         // Sort 
        the Collections.sort (the arrayList);
         // iterate 
        Iterator = arrayList.iterator ();
         the while (iterator.hasNext ()) {
            Student stu = (Student) iterator.next();
            System.out.println(stu.getNumber());
        }

        // use binarySearch find arrayList elements in an element corresponding to the index from zero, it is looking after ordering 
        System.out.println ( "student2 index is:" + Collections.binarySearch (arrayList, STUDENT2));

        // Alternatively fill () of all elements in the set is replaced with the same element 
        Collections.fill (arrayList, student1);
        iterator = arrayList.iterator();
        while (iterator.hasNext()){
            Student stu = (Student) iterator.next();
            System.out.println(stu.getNumber());
        }

    }
}

 

Guess you like

Origin www.cnblogs.com/obge/p/12511034.html