Java集合涉及的類(代碼)

來源:互聯網
上載者:User

標籤:

Customer:

public class Customer implements Comparable{
        private Integer customerId;
        private String customerName;
                
        public Integer getCustomerId() {
            return customerId;
        }
        public void setCustomerId(Integer customerId) {
            this.customerId = customerId;
        }
        public String getCustomerName() {
            return customerName;
        }
        public void setCustomerName(String customerName) {
            this.customerName = customerName;
        }
        public Customer(Integer customerId, String customerName) {
            this.customerId = customerId;
            this.customerName = customerName;
        }          
    @Override
        public String toString() {
            return "Customer [customerId=" + customerId + ", customerName="
                    + customerName + "]";
        }
    public Customer() {
        }
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + customerId;
        result = prime * result
                + ((customerName == null) ? 0 : customerName.hashCode());
        return result;
    }
    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Customer other = (Customer) obj;
        if (customerId != other.customerId)
            return false;
        if (customerName == null) {
            if (other.customerName != null)
                return false;
        } else if (!customerName.equals(other.customerName))
            return false;
        return true;
    }
     //TreeSet類按照compareTo方法比較返回
     //按id或name排序
    @Override
    public int compareTo(Object o) {
        if(o instanceof Customer){
            Customer c=(Customer) o;
            //給整體添加‘-‘號改變正/逆序
//            return this.customerId-c.customerId;                             
            return this.customerName.compareTo(c.customerName);            
        }       
        return 0;
    }        
}

CustomerComparator:

import java.util.Comparator;
public class CustomerComparator implements Comparator{
    @Override
    public int compare(Object o1, Object o2) {
        if(o1 instanceof Customer && o2 instanceof Customer){
            Customer cust1=(Customer) o1;
            Customer cust2=(Customer) o2;
            return -cust1.getCustomerName().compareTo(cust2.getCustomerName());
        }
        return 0;
    }
}

  

Java集合涉及的類(代碼)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.