Java集合涉及的类(代码)
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集合涉及的类(代码)的更多相关文章
- Java集合——Collections工具类
Java集合——Collections工具类 摘要:本文主要学习了Collections工具类的常用方法. 概述 Collections工具类主要用来操作集合类,比如List和Set. 常用操作 排序 ...
- java集合框架——工具类
一.概述 JAVA集合框架中有两个很重要的工具类,一个是Collections,另一个是Arrays.分别封装了对集合的操作方法和对数组的操作方法,这些操作方法使得程序员的开发更加高效. public ...
- java 集合Collections 工具类:排序,查找替换。Set、List、Map 的of方法创建不可变集合
Collections 工具类 Java 提供1个操作 Set List Map 等集合的工具类 Collections ,该工具类里提供了大量方法对集合元素进行排序.查询和修改等操作,还提供了将集合 ...
- Java集合----Collection工具类
Collections 工具类 Collections 是一个操作 Set.List 和 Map 等集合的工具类 Collections 中提供了大量方法对集合元素进行排序.查询和修改等操作,还提供了 ...
- Java容器涉及的类(代码)
Customer: public class Customer implements Comparable{ private Integer customerId; private String cu ...
- Java集合中那些类是线程安全的
线程安全类 在集合框架中,有些类是线程安全的,这些都是jdk1.1中的出现的.在jdk1.2之后,就出现许许多多非线程安全的类. 下面是这些线程安全的同步的类: vector:就比arraylist多 ...
- java 集合之实现类ArrayList 和 LinkedList
List 的方法列表 方法名 功能说明 ArrayList() 构造方法,用于创建一个空的数组列表 add(E e) 将指定的元素添加到此列表的尾部 get(int index) 返回此列表中指定位置 ...
- Java 集合的工具类Collections的常用方法
Collections类 java.utils.Collections是集合工具类,用来对集合进行操作. Collections类的常用方法 这里介绍四个常用方法: addAll(Collection ...
- JAVA集合接口及类
各接口及类关系图 Iterable 所有集合的初始接口,实现该接口可进行foreach操作,只有一个iterator()方法,并返回iterator类型: Iterable在java.lang下,It ...
随机推荐
- 一个异常与Android Studio系列教程参考
由于编译过程中遇到了错误:
- SQL查询语句 group by后, 字符串合并
合并列值 --******************************************************************************************* 表 ...
- JavaScript算法题之–随机数的生成
JavaScript算法题之–随机数的生成 需求描述:从一组有序的数据中生成一组随机并且不重复的数,类似于简单的抽奖程序的实现. 先来生成一个有序的数组: 1 var arr = [], 2 ...
- Android实现简单短信发送器
布局: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:to ...
- 利用Java的读写锁实现缓存的设计
Java中的读写锁: 多个读锁不互斥, 读锁与写锁互斥, 写锁与写锁互斥, 这是由JVM自行控制的,我们只要上好相应的锁即可. 缓存的设计: package com.cn.gbx; import ja ...
- Oracle 中取当前日期的上个月最后天和第一天
前一月 最后一天select last_day(add_months(sysdate,-1)) from dual; 2010-2-28 9:23:01; select t ...
- mysql 2003 10038 连接不上的解决
网上写的很复杂,其实解决办法是 你在右键管理员权限下运行 mysqld. 忘记这个了,害的又找了近1个小时的时间找问题.
- [转载] 首席工程师揭秘:LinkedIn大数据后台是如何运作的?(一)
本文作者:Jay Kreps,linkedin公司首席工程师:文章来自于他在linkedin上的分享:原文标题:The Log: What every software engineer should ...
- 巧妙的实现 CSS 斜线(炫酷的小效果)
开本系列,谈谈一些有趣的 CSS 题目,题目类型天马行空,想到什么说什么,不仅为了拓宽一下解决问题的思路,更涉及一些容易忽视的 CSS 细节. 解题不考虑兼容性,题目天马行空,想到什么说什么,如果 ...
- Object Pascal 控制语句
控制语句 1.常量声明语句常量在声明时就被赋予了一个值,在程序执行过程中是不可改变的. 格式 const 常量名 :数据类型 = 值 下面的例子声明了3 个常量: const Pi = 3.14159 ...