博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HashSet AND TreeSet
阅读量:5091 次
发布时间:2019-06-13

本文共 5537 字,大约阅读时间需要 18 分钟。

package com.gxnu.edu.bean;

import java.io.Serializable;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import com.gxnu.edu.lqm.Month;

public class Person implements Comparable<Person>,Cloneable,Serializable{

private String name;

private double height;
private Month month;
private List<Integer> list;
{
list = new ArrayList<>();
list.addAll(Arrays.asList(1,2,3,4));
}
public void add(int i){
this.list.add(i);
}
public void printList(){
list.forEach(System.out::println);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
public Month getMonth() {
return month;
}
public void setMonth(Month month) {
this.month = month;
}
public Person() {
super();
// TODO Auto-generated constructor stub
}
public Person(String name, double height, Month month) {
super();
this.name = name;
this.height = height;
this.month = month;
}
@Override
public String toString() {
return "Person [name=" + name + ", height=" + height + ", month=" + month + "]";
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
long temp;
temp = Double.doubleToLongBits(height);
result = prime * result + (int) (temp ^ (temp >>> 32));
result = prime * result + ((month == null) ? 0 : month.hashCode());
result = prime * result + ((name == null) ? 0 : name.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;
Person other = (Person) obj;
if (Double.doubleToLongBits(height) != Double.doubleToLongBits(other.height))
return false;
if (month != other.month)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
@Override
public int compareTo(Person p) {
int i = this.getName().compareTo(p.getName());
if(i==0){
double d = this.getHeight()-p.getHeight();
if(d==0){
i=this.getMonth().ordinal()-p.getMonth().ordinal();
}else if(d<0){
i=-1;
}else{
i=1;
}
}
return i;
}
public Person clone(){
try {
return (Person) super.clone();
} catch (CloneNotSupportedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}
}

 

 

package com.gxnu.edu.lqm.collection.test;

import java.util.Comparator;

import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

import org.junit.Test;

import com.gxnu.edu.bean.Person;

import com.gxnu.edu.lqm.Month;

public class SetEx {

@Test
public void test1(){
Set<Person> hashSet = new HashSet<>();
Person p = new Person("zhangsan",166,Month.APRIL);
hashSet.add(p);
p = new Person("lisi",176,Month.AUGUST);
hashSet.add(p);
p = new Person("wangwu",169,Month.AUGUST);
hashSet.add(p);
p = new Person("zhaoliu",186,Month.DECEMBER);
hashSet.add(p);
p = new Person("zhangsan",166,Month.APRIL);
hashSet.add(p);
p = new Person("liqi",188,Month.JULY);
hashSet.add(p);
p = new Person("wangba",178,Month.NOWENBER);
hashSet.add(p);
p = new Person("jiujiu",167,Month.JUNE);
hashSet.add(p);
p = new Person("xiaoxiao",187,Month.JANUARY);
hashSet.add(p);
p = new Person("xihua",169,Month.JANUARY);
hashSet.add(p);
p = new Person("zhangsan",166,Month.OTOBER);
hashSet.add(p);
List<Person> list = new LinkedList<>();
Person p2 = new Person("zhangsan",166,Month.MARCH);
list.add(p2);
p2 = new Person("zhangsan",166,Month.APRIL);
list.add(p2);
p2 = new Person("lisi",176,Month.AUGUST);
list.add(p2);
hashSet.forEach(System.out::println);
boolean b = hashSet.retainAll(list);
System.out.println("****"+b);
hashSet.forEach(System.out::println);
System.out.println("************使用removeIf***************");
hashSet.removeIf(pp->pp.getName().contains("li"));
hashSet.forEach(s->System.out.println(s));
}
@Test
public void testTree(){
Comparator<Person> cr1=Comparator.comparing(Person::getName);
Comparator<Person> cr2=Comparator.comparing(Person::getHeight);
Comparator<Person> cr3=Comparator.comparing(Person::getMonth);
Comparator<Person> com=cr1.thenComparing(cr2).thenComparing(cr3);
TreeSet<Person> set = new TreeSet<>(com);
Person p = new Person("zhangsan",166,Month.APRIL);
set.add(p);
p = new Person("lisi",176,Month.AUGUST);
set.add(p);
p = new Person("wangwu",169,Month.AUGUST);
set.add(p);
p = new Person("zhaoliu",186,Month.DECEMBER);
set.add(p);
p = new Person("zhangsan",166,Month.APRIL);
set.add(p);
p = new Person("liqi",188,Month.JULY);
set.add(p);
p = new Person("wangba",178,Month.NOWENBER);
set.add(p);
p = new Person("jiujiu",167,Month.JUNE);
set.add(p);
p = new Person("xiaoxiao",187,Month.JANUARY);
set.add(p);
p = new Person("xihua",169,Month.JANUARY);
set.add(p);
p = new Person("zhangsan",166,Month.OTOBER);
set.add(p);
set.forEach(s->System.out.println(s));
System.out.println("***********");
set.headSet(new Person("maocao",188,Month.APRIL));
set.forEach(System.out::println);
/*
List<Person> list = new LinkedList<>();
Person p2 = new Person("zhangsan",166,Month.MARCH);
list.add(p2);
p2 = new Person("zhangsan",166,Month.APRIL);
list.add(p2);
p2 = new Person("lisi",176,Month.AUGUST);
list.add(p2);
set.forEach(System.out::println);
boolean b = set.retainAll(list);
System.out.println("****"+b);
set.forEach(System.out::println);
System.out.println("************使用removeIf***************");
set.removeIf(pp->pp.getName().contains("li"));
set.forEach(s->System.out.println(s));*/
}

}

转载于:https://www.cnblogs.com/jiminluo/p/9337892.html

你可能感兴趣的文章
iOS CocoaPods安装和使用图解
查看>>
iOS 开发 UI 搭建心得(一)—— 驾驭 StoryBoard
查看>>
android自定义倒计时控件示例
查看>>
Android系统Surface机制的SurfaceFlinger服务的线程模型分析
查看>>
android用于打开各种文件的intent
查看>>
Java语言的垃圾回收机制
查看>>
eclipse运行不走debug断点
查看>>
MOSS 日志小记
查看>>
初识wordpress
查看>>
C#MD5为密码加密
查看>>
newlisp的lambda表达式
查看>>
源代码管理十诫
查看>>
socket粘包实例个人理解
查看>>
leapmotion 初识
查看>>
【安卓5】高级控件——ListActivity
查看>>
Windows虚拟机中无法传输Arduino程序的问题
查看>>
0909对编译原理的了解
查看>>
我的2016
查看>>
Visual Studio 2010的新特性
查看>>
前端之路——JavaScript基础 二
查看>>