码迷,mamicode.com
首页 > 其他好文 > 详细

HashSet AND TreeSet

时间:2018-07-19 21:15:27      阅读:139      评论:0      收藏:0      [点我收藏+]

标签:one   nta   hashset   ack   bean   contains   its   foreach   treeset   

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));*/
}

}

HashSet AND TreeSet

标签:one   nta   hashset   ack   bean   contains   its   foreach   treeset   

原文地址:https://www.cnblogs.com/jiminluo/p/9337892.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!