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集合涉及的类(代码)的更多相关文章

  1. Java集合——Collections工具类

    Java集合——Collections工具类 摘要:本文主要学习了Collections工具类的常用方法. 概述 Collections工具类主要用来操作集合类,比如List和Set. 常用操作 排序 ...

  2. java集合框架——工具类

    一.概述 JAVA集合框架中有两个很重要的工具类,一个是Collections,另一个是Arrays.分别封装了对集合的操作方法和对数组的操作方法,这些操作方法使得程序员的开发更加高效. public ...

  3. java 集合Collections 工具类:排序,查找替换。Set、List、Map 的of方法创建不可变集合

    Collections 工具类 Java 提供1个操作 Set List Map 等集合的工具类 Collections ,该工具类里提供了大量方法对集合元素进行排序.查询和修改等操作,还提供了将集合 ...

  4. Java集合----Collection工具类

    Collections 工具类 Collections 是一个操作 Set.List 和 Map 等集合的工具类 Collections 中提供了大量方法对集合元素进行排序.查询和修改等操作,还提供了 ...

  5. Java容器涉及的类(代码)

    Customer: public class Customer implements Comparable{ private Integer customerId; private String cu ...

  6. Java集合中那些类是线程安全的

    线程安全类 在集合框架中,有些类是线程安全的,这些都是jdk1.1中的出现的.在jdk1.2之后,就出现许许多多非线程安全的类. 下面是这些线程安全的同步的类: vector:就比arraylist多 ...

  7. java 集合之实现类ArrayList 和 LinkedList

    List 的方法列表 方法名 功能说明 ArrayList() 构造方法,用于创建一个空的数组列表 add(E e) 将指定的元素添加到此列表的尾部 get(int index) 返回此列表中指定位置 ...

  8. Java 集合的工具类Collections的常用方法

    Collections类 java.utils.Collections是集合工具类,用来对集合进行操作. Collections类的常用方法 这里介绍四个常用方法: addAll(Collection ...

  9. JAVA集合接口及类

    各接口及类关系图 Iterable 所有集合的初始接口,实现该接口可进行foreach操作,只有一个iterator()方法,并返回iterator类型: Iterable在java.lang下,It ...

随机推荐

  1. mysql启动关闭

    RedHat Linux (Fedora Core/Cent OS) 1.启动:/etc/init.d/mysqld start2.停止:/etc/init.d/mysqld stop3.重启:/et ...

  2. Android——android学习(android目录与AndroidManifest解析)

    res目录:存放android项目的各种资源文件 layout:存放界面布局文件 values:存放各种xml格式的资源文件 strings.xml:字符串资源文件: colors.xml:颜色资源文 ...

  3. Nemerle Quick Guide

    This is a quick guide covering nearly all of Nemerle's features. It should be especially useful to a ...

  4. java位运算符常见用法

    1. 判断int型变量a是奇数还是偶数 a&1 = 0 偶数 a&1 = 1 奇数 2. 求平均值,比如有两个int类型变量x.y,首先要求x+y的和,再除以2,但是有可能x+y的结果 ...

  5. ZOJ-2366 Weird Dissimilarity 动态规划+贪心

    题意:现给定一个字符集中一共Z个元素的环境,给出一个Z*Z的数组,表示从i到j之间的距离.给定两组字符串,分别问包含着两个字符串(给定的字符串为所求字符串的子序列不是子串)对应位的距离和值最小为多少? ...

  6. Java用通配符 获得泛型的协变和逆变

    Java对应泛型的协变和逆变

  7. number-of-segments-in-a-string

    https://leetcode.com/problems/number-of-segments-in-a-string/ package com.company; class Solution { ...

  8. java读取properties配置文件的方法

    app.properties mail.smtp.host=smtp.163.com mail.transport.protocol=smtp import java.io.InputStream; ...

  9. google_apactest_round_A_problem_D

    先尝试过小数据 题目 有8张卡牌,每个卡牌都可以有不同的等级,每个卡牌的不同等级具有不同的攻击力,可以通过花钱给卡牌充值从而升级,且每次只能升一级,比如可以花1个硬币将卡牌2从1级升级到2级,同时卡牌 ...

  10. hiho_1078_线段树区间修改

    题目 给定一组数,要求进行若干次操作,这些操作可以分为两种类型: (1) CMD 1 beg end value 将数组中下标在[beg, end] 区间内数字都变为value (2) CMD 2 b ...