HashMap 中有put方法 put(key,value)key是无序不可重复的
存储在HashSet集合和HashMap集合key部分中的元素,需要重写hashCode和equals方法
Set a = new HashSet(); //创建集合
Employee e1 = new Employee("1000","JACK");
Employee e2 = new Employee("2001","Look");
Employee e3 = new Employee("3000","Bob");
Employee e4 = new Employee("1000","JACK");
a.add(e1);
a.add(e2);
a.add(e3);
a.add(e4);
System.out.println(a.size()); //重写了hashCode和equals方法后,e1=e4,结果为3
Employee类以及重写hashCode和equals方法
class Employee{
String no;//编号
String name;//姓名
Employee(String no,String name) {
this.no = no;
this.name = name;
}
public boolean equals(Object o) { //如果员工编号相同,并且名字相同,则是同一个对象 hashCode返回相同时调用equals
if(this==o) {
return true;
}
if(o instanceof Employee) {
Employee e = (Employee)o;
if(e.no.equals(this.no)&&e.name.equals(this.name)) {
return true;
}
}
return false;
}
public int hashCode() {
return no.hashCode();
}
}
SortSet、TreeSet
SortedSet集合存储元素自动排序:
因为被存储的元素实现了Comparable接口
编写TreeSet集合在添加元素的时候,会调用comparaTo方法完成比较
import java.text.SimpleDateFormat;
import java.util.*;
public class SortedSettest {
public static void main(String[] args) throws Exception{
SortedSet a = new TreeSet();//创建集合
a.add(10); //自动装箱
a.add(20);
a.add(1);
a.add(5);
//遍历
Iterator it = a.iterator();
while(it.hasNext()) {
Object element = it.next();
System.out.println(element);
}
}
String排序
SortedSet str = new TreeSet();
str.add("CWA");
str.add("AS");
str.add("CAB");
str.add("ZAV");
it = str.iterator();
while(it.hasNext()) {
Object element = it.next();
System.out.println(element);
}
日期Date排序
String time1 = "2018-01-05";
String time2 = "2019-02-05";
String time3 = "2019-01-15";
String time4 = "2017-05-04";
SimpleDateFormat timea = new SimpleDateFormat("yyyy-MM-dd"); //设置格式
Date t1 = timea.parse(time1);
Date t2 = timea.parse(time2);
Date t3 = timea.parse(time3);
Date t4 = timea.parse(time4);
SortedSet times = new TreeSet();
times.add(t1);
times.add(t2);
times.add(t3);
times.add(t4);
it = times.iterator();
while(it.hasNext()) {
Object element = it.next();
if(element instanceof Date) {
Date d = (Date)element;
System.out.println(timea.format(d));
}
}
SortedSet users = new TreeSet();
User u1 = new User(15);
User u2 = new User(28);
User u3 = new User(7);
users.add(u1);
users.add(u2);
users.add(u3);
//遍历
it = users.iterator();
while(it.hasNext()) {
System.out.println(it.next());
}
比较
class User implements Comparable{
int age;
public User(int age) {
this.age = age;
}
public String toString() {
return "User[age="+age+"]";
}
//实现java.lang.Comparable;接口中的compareTo方法
//按照User的age排序,编写比较规则
public int compareTo(Object o) {
int age1 = this.age;
int age2 = ((User)o).age;
return age1-age2;
}
}
SortedSet排序的另外一种方法 java.util.Comparator; 单独编写一个比较器
SortedSet products = new TreeSet(new ProductComparator()); //创建TreeSet集合的时候提供一个比较器
添加元素和遍历
Product p1 = new Product(1.1);
Product p2 = new Product(3.8);
Product p3 = new Product(2.5);
products.add(p1); //添加元素
products.add(p2);
products.add(p3);
Iterator it = products.iterator();
while(it.hasNext()) {
System.out.println(it.next());
}
Product类
class Product{
double price;
public Product(double price) {
this.price = price;
}
public String toString() {
return price + "";
}
}
单独比较器
class ProductComparator implements Comparator{
//按照商品价格排序
public int compare(Object o1,Object o2) {
double price1 = ((Product)o1).price;
double price2 = ((Product)o2).price;
if(price1==price2) {
return 0;
}else if(price1 > price2) {
return 1;
}else{
return -1;
}
}
}
//匿名内部类 不推荐使用,因为比较器无法得到重复利用
SortedSet products = new TreeSet(new Comparator(){
public int compare(Object o1,Object o2) {
double price1 = ((Product)o1).price;
double price2 = ((Product)o2).price;
if(price1==price2) {
return 0;
}else if(price1 > price2) {
return 1;
}else{
return -1;
}
}
}
);