欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java之 TreeSet的詳細(xì)使用說(shuō)明

 更新時(shí)間:2021年02月24日 12:28:44   作者:南 墻  
這篇文章主要介紹了Java之 TreeSet的詳細(xì)使用說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧

第1部分 TreeSet介紹

TreeSet簡(jiǎn)介

TreeSet 是一個(gè)有序的集合,它的作用是提供有序的Set集合。它繼承于AbstractSet抽象類,實(shí)現(xiàn)了NavigableSet<E>, Cloneable, java.io.Serializable接口。

TreeSet 繼承于AbstractSet,所以它是一個(gè)Set集合,具有Set的屬性和方法。

TreeSet 實(shí)現(xiàn)了NavigableSet接口,意味著它支持一系列的導(dǎo)航方法。比如查找與指定目標(biāo)最匹配項(xiàng)。

TreeSet 實(shí)現(xiàn)了Cloneable接口,意味著它能被克隆。

TreeSet 實(shí)現(xiàn)了java.io.Serializable接口,意味著它支持序列化。

TreeSet是基于TreeMap實(shí)現(xiàn)的。TreeSet中的元素支持2種排序方式:自然排序 或者 根據(jù)創(chuàng)建TreeSet 時(shí)提供的 Comparator 進(jìn)行排序。這取決于使用的構(gòu)造方法。

TreeSet為基本操作(add、remove 和 contains)提供受保證的 log(n) 時(shí)間開銷。

另外,TreeSet是非同步的。 它的iterator 方法返回的迭代器是fail-fast的。

TreeSet的構(gòu)造函數(shù)

// 默認(rèn)構(gòu)造函數(shù)。使用該構(gòu)造函數(shù),TreeSet中的元素按照自然排序進(jìn)行排列。
TreeSet()
// 創(chuàng)建的TreeSet包含collection
TreeSet(Collection<? extends E> collection)
// 指定TreeSet的比較器
TreeSet(Comparator<? super E> comparator)
// 創(chuàng)建的TreeSet包含set
TreeSet(SortedSet<E> set)
TreeSet的API
boolean   add(E object)
boolean   addAll(Collection<? extends E> collection)
void   clear()
Object   clone()
boolean   contains(Object object)
E    first()
boolean   isEmpty()
E    last()
E    pollFirst()
E    pollLast()
E    lower(E e)
E    floor(E e)
E    ceiling(E e)
E    higher(E e)
boolean   remove(Object object)
int   size()
Comparator<? super E> comparator()
Iterator<E>  iterator()
Iterator<E>  descendingIterator()
SortedSet<E>  headSet(E end)
NavigableSet<E>  descendingSet()
NavigableSet<E>  headSet(E end, boolean endInclusive)
SortedSet<E>  subSet(E start, E end)
NavigableSet<E>  subSet(E start, boolean startInclusive, E end, boolean endInclusive)
NavigableSet<E>  tailSet(E start, boolean startInclusive)
SortedSet<E>  tailSet(E start)

說(shuō)明:

(01) TreeSet是有序的Set集合,因此支持add、remove、get等方法。

(02) 和NavigableSet一樣,TreeSet的導(dǎo)航方法大致可以區(qū)分為兩類,一類時(shí)提供元素項(xiàng)的導(dǎo)航方法,返回某個(gè)元素;另一類時(shí)提供集合的導(dǎo)航方法,返回某個(gè)集合。

lower、floor、ceiling 和 higher 分別返回小于、小于等于、大于等于、大于給定元素的元素,如果不存在這樣的元素,則返回 null。

第2部分 TreeSet數(shù)據(jù)結(jié)構(gòu)

TreeSet的繼承關(guān)系

java.lang.Object
 ↳ java.util.AbstractCollection<E>
  ↳ java.util.AbstractSet<E>
  ↳ java.util.TreeSet<E>
public class TreeSet<E> extends AbstractSet<E> 
 implements NavigableSet<E>, Cloneable, java.io.Serializable{}

TreeSet與Collection關(guān)系如下圖:

從圖中可以看出:

(01) TreeSet繼承于AbstractSet,并且實(shí)現(xiàn)了NavigableSet接口。

(02) TreeSet的本質(zhì)是一個(gè)"有序的,并且沒(méi)有重復(fù)元素"的集合,它是通過(guò)TreeMap實(shí)現(xiàn)的。TreeSet中含有一個(gè)"NavigableMap類型的成員變量"m,而m實(shí)際上是"TreeMap的實(shí)例"。

第3部分 TreeSet源碼解析(基于JDK1.6.0_45)

為了更了解TreeSet的原理,下面對(duì)TreeSet源碼代碼作出分析。

 package java.util; 
 public class TreeSet<E> extends AbstractSet<E>
 implements NavigableSet<E>, Cloneable, java.io.Serializable
 {
 // NavigableMap對(duì)象
 private transient NavigableMap<E,Object> m;
 
 // TreeSet是通過(guò)TreeMap實(shí)現(xiàn)的,
 // PRESENT是鍵-值對(duì)中的值。
 private static final Object PRESENT = new Object();
 // 不帶參數(shù)的構(gòu)造函數(shù)。創(chuàng)建一個(gè)空的TreeMap
 public TreeSet() {
 this(new TreeMap<E,Object>());
 }
 // 將TreeMap賦值給 "NavigableMap對(duì)象m"
 TreeSet(NavigableMap<E,Object> m) {
 this.m = m;
 }
 // 帶比較器的構(gòu)造函數(shù)。
 public TreeSet(Comparator<? super E> comparator) {
 this(new TreeMap<E,Object>(comparator));
 }
 // 創(chuàng)建TreeSet,并將集合c中的全部元素都添加到TreeSet中
 public TreeSet(Collection<? extends E> c) {
 this();
 // 將集合c中的元素全部添加到TreeSet中
 addAll(c);
 }
 // 創(chuàng)建TreeSet,并將s中的全部元素都添加到TreeSet中
 public TreeSet(SortedSet<E> s) {
 this(s.comparator());
 addAll(s);
 }
 // 返回TreeSet的順序排列的迭代器。
 // 因?yàn)門reeSet時(shí)TreeMap實(shí)現(xiàn)的,所以這里實(shí)際上時(shí)返回TreeMap的“鍵集”對(duì)應(yīng)的迭代器
 public Iterator<E> iterator() {
 return m.navigableKeySet().iterator();
 }
 // 返回TreeSet的逆序排列的迭代器。
 // 因?yàn)門reeSet時(shí)TreeMap實(shí)現(xiàn)的,所以這里實(shí)際上時(shí)返回TreeMap的“鍵集”對(duì)應(yīng)的迭代器
 public Iterator<E> descendingIterator() {
 return m.descendingKeySet().iterator();
 }
 // 返回TreeSet的大小
 public int size() {
 return m.size();
 }
 // 返回TreeSet是否為空
 public boolean isEmpty() {
 return m.isEmpty();
 }
 // 返回TreeSet是否包含對(duì)象(o)
 public boolean contains(Object o) {
 return m.containsKey(o);
 }
 // 添加e到TreeSet中
 public boolean add(E e) {
 return m.put(e, PRESENT)==null;
 }
 // 刪除TreeSet中的對(duì)象o
 public boolean remove(Object o) {
 return m.remove(o)==PRESENT;
 }
 // 清空TreeSet
 public void clear() {
 m.clear();
 }
 // 將集合c中的全部元素添加到TreeSet中
 public boolean addAll(Collection<? extends E> c) {
 // Use linear-time version if applicable
 if (m.size()==0 && c.size() > 0 &&
  c instanceof SortedSet &&
  m instanceof TreeMap) {
  SortedSet<? extends E> set = (SortedSet<? extends E>) c;
  TreeMap<E,Object> map = (TreeMap<E, Object>) m;
  Comparator<? super E> cc = (Comparator<? super E>) set.comparator();
  Comparator<? super E> mc = map.comparator();
  if (cc==mc || (cc != null && cc.equals(mc))) {
  map.addAllForTreeSet(set, PRESENT);
  return true;
  }
 }
 return super.addAll(c);
 }
 
 // 返回子Set,實(shí)際上是通過(guò)TreeMap的subMap()實(shí)現(xiàn)的。
 public NavigableSet<E> subSet(E fromElement, boolean fromInclusive,
     E toElement, boolean toInclusive) {
  return new TreeSet<E>(m.subMap(fromElement, fromInclusive,
     toElement, toInclusive));
 }
 
 // 返回Set的頭部,范圍是:從頭部到toElement。
 // inclusive是是否包含toElement的標(biāo)志
 public NavigableSet<E> headSet(E toElement, boolean inclusive) {
  return new TreeSet<E>(m.headMap(toElement, inclusive));
 }
 
 // 返回Set的尾部,范圍是:從fromElement到結(jié)尾。
 // inclusive是是否包含fromElement的標(biāo)志
 public NavigableSet<E> tailSet(E fromElement, boolean inclusive) {
  return new TreeSet<E>(m.tailMap(fromElement, inclusive));
 }
 
 // 返回子Set。范圍是:從fromElement(包括)到toElement(不包括)。
 public SortedSet<E> subSet(E fromElement, E toElement) {
  return subSet(fromElement, true, toElement, false);
 }
 
 // 返回Set的頭部,范圍是:從頭部到toElement(不包括)。
 public SortedSet<E> headSet(E toElement) {
  return headSet(toElement, false);
 }
 
 // 返回Set的尾部,范圍是:從fromElement到結(jié)尾(不包括)。
 public SortedSet<E> tailSet(E fromElement) {
  return tailSet(fromElement, true);
 }
 
 // 返回Set的比較器
 public Comparator<? super E> comparator() {
  return m.comparator();
 }
 
 // 返回Set的第一個(gè)元素
 public E first() {
  return m.firstKey();
 }
 
 // 返回Set的最后一個(gè)元素
 public E first() {
 public E last() {
  return m.lastKey();
 }
 
 // 返回Set中小于e的最大元素
 public E lower(E e) {
  return m.lowerKey(e);
 }
 
 // 返回Set中小于/等于e的最大元素
 public E floor(E e) {
  return m.floorKey(e);
 }
 
 // 返回Set中大于/等于e的最小元素
 public E ceiling(E e) {
  return m.ceilingKey(e);
 }
 
 // 返回Set中大于e的最小元素
 public E higher(E e) {
  return m.higherKey(e);
 }
 
 // 獲取第一個(gè)元素,并將該元素從TreeMap中刪除。
 public E pollFirst() {
  Map.Entry<E,?> e = m.pollFirstEntry();
  return (e == null)? null : e.getKey();
 }
 
 // 獲取最后一個(gè)元素,并將該元素從TreeMap中刪除。
 public E pollLast() {
  Map.Entry<E,?> e = m.pollLastEntry();
  return (e == null)? null : e.getKey();
 }
 
 // 克隆一個(gè)TreeSet,并返回Object對(duì)象
 public Object clone() {
  TreeSet<E> clone = null;
  try {
  clone = (TreeSet<E>) super.clone();
  } catch (CloneNotSupportedException e) {
  throw new InternalError();
  }
 
  clone.m = new TreeMap<E,Object>(m);
  return clone;
 }
 
 // java.io.Serializable的寫入函數(shù)
 // 將TreeSet的“比較器、容量,所有的元素值”都寫入到輸出流中
 private void writeObject(java.io.ObjectOutputStream s)
  throws java.io.IOException {
  s.defaultWriteObject();
 
  // 寫入比較器
  s.writeObject(m.comparator());
 
  // 寫入容量
  s.writeInt(m.size());
 
  // 寫入“TreeSet中的每一個(gè)元素”
  for (Iterator i=m.keySet().iterator(); i.hasNext(); )
  s.writeObject(i.next());
 }
 
 // java.io.Serializable的讀取函數(shù):根據(jù)寫入方式讀出
 // 先將TreeSet的“比較器、容量、所有的元素值”依次讀出
 private void readObject(java.io.ObjectInputStream s)
  throws java.io.IOException, ClassNotFoundException {
  // Read in any hidden stuff
  s.defaultReadObject();
 
  // 從輸入流中讀取TreeSet的“比較器”
  Comparator<? super E> c = (Comparator<? super E>) s.readObject();
 
  TreeMap<E,Object> tm;
  if (c==null)
  tm = new TreeMap<E,Object>();
  else
  tm = new TreeMap<E,Object>(c);
  m = tm;
 
  // 從輸入流中讀取TreeSet的“容量”
  int size = s.readInt();
 
  // 從輸入流中讀取TreeSet的“全部元素”
  tm.readTreeSet(size, s, PRESENT);
 }
 
 // TreeSet的序列版本號(hào)
 private static final long serialVersionUID = -2479143000061671589L;
 }

總結(jié):

(01) TreeSet實(shí)際上是TreeMap實(shí)現(xiàn)的。當(dāng)我們構(gòu)造TreeSet時(shí);若使用不帶參數(shù)的構(gòu)造函數(shù),則TreeSet的使用自然比較器;若用戶需要使用自定義的比較器,則需要使用帶比較器的參數(shù)。

(02) TreeSet是非線程安全的。

(03) TreeSet實(shí)現(xiàn)java.io.Serializable的方式。當(dāng)寫入到輸出流時(shí),依次寫入“比較器、容量、全部元素”;當(dāng)讀出輸入流時(shí),再依次讀取。

第4部分 TreeSet遍歷方式

4.1 Iterator順序遍歷

for(Iterator iter = set.iterator(); iter.hasNext(); ) { 
 iter.next();
} 

4.2 Iterator順序遍歷

// 假設(shè)set是TreeSet對(duì)象
for(Iterator iter = set.descendingIterator(); iter.hasNext(); ) { 
 iter.next();
}

4.3 for-each遍歷HashSet

// 假設(shè)set是TreeSet對(duì)象,并且set中元素是String類型
String[] arr = (String[])set.toArray(new String[0]);
for (String str:arr)
 System.out.printf("for each : %s\n", str);

TreeSet不支持快速隨機(jī)遍歷,只能通過(guò)迭代器進(jìn)行遍歷!

TreeSet遍歷測(cè)試程序如下:

 import java.util.*;
 
 /**
 * @desc TreeSet的遍歷程序
 *
 * @author skywang
 * @email kuiwu-wang@163.com
 */
 public class TreeSetIteratorTest {
 
 public static void main(String[] args) {
  TreeSet set = new TreeSet();
  set.add("aaa");
  set.add("aaa");
  set.add("bbb");
  set.add("eee");
  set.add("ddd");
  set.add("ccc");
 
  // 順序遍歷TreeSet
  ascIteratorThroughIterator(set) ;
  // 逆序遍歷TreeSet
  descIteratorThroughIterator(set);
  // 通過(guò)for-each遍歷TreeSet。不推薦!此方法需要先將Set轉(zhuǎn)換為數(shù)組
  foreachTreeSet(set);
 }
 
 // 順序遍歷TreeSet
 public static void ascIteratorThroughIterator(TreeSet set) {
  System.out.print("\n ---- Ascend Iterator ----\n");
  for(Iterator iter = set.iterator(); iter.hasNext(); ) {
  System.out.printf("asc : %s\n", iter.next());
  }
 }
 
 // 逆序遍歷TreeSet
 public static void descIteratorThroughIterator(TreeSet set) {
  System.out.printf("\n ---- Descend Iterator ----\n");
  for(Iterator iter = set.descendingIterator(); iter.hasNext(); )
  System.out.printf("desc : %s\n", (String)iter.next());
 }
 
 // 通過(guò)for-each遍歷TreeSet。不推薦!此方法需要先將Set轉(zhuǎn)換為數(shù)組
 private static void foreachTreeSet(TreeSet set) {
  System.out.printf("\n ---- For-each ----\n");
  String[] arr = (String[])set.toArray(new String[0]);
  for (String str:arr)
  System.out.printf("for each : %s\n", str);
 }
 }

運(yùn)行結(jié)果:

---- Ascend Iterator ----
asc : aaa
asc : bbb
asc : ccc
asc : ddd
asc : eee
---- Descend Iterator ----
desc : eee
desc : ddd
desc : ccc
desc : bbb
desc : aaa
---- For-each ----
for each : aaa
for each : bbb
for each : ccc
for each : ddd
for each : eee

第5部分 TreeSet示例

下面通過(guò)實(shí)例學(xué)習(xí)如何使用TreeSet

import java.util.*;
/**
 * @desc TreeSet的API測(cè)試
 *
 * @author skywang
 * @email kuiwu-wang@163.com
 */
public class TreeSetTest {
 public static void main(String[] args) {
 testTreeSetAPIs();
 }
 
 // 測(cè)試TreeSet的api
 public static void testTreeSetAPIs() {
 String val;
 // 新建TreeSet
 TreeSet tSet = new TreeSet();
 // 將元素添加到TreeSet中
 tSet.add("aaa");
 // Set中不允許重復(fù)元素,所以只會(huì)保存一個(gè)“aaa”
 tSet.add("aaa");
 tSet.add("bbb");
 tSet.add("eee");
 tSet.add("ddd");
 tSet.add("ccc");
 System.out.println("TreeSet:"+tSet);
 // 打印TreeSet的實(shí)際大小
 System.out.printf("size : %d\n", tSet.size());
 // 導(dǎo)航方法
 // floor(小于、等于)
 System.out.printf("floor bbb: %s\n", tSet.floor("bbb"));
 // lower(小于)
 System.out.printf("lower bbb: %s\n", tSet.lower("bbb"));
 // ceiling(大于、等于)
 System.out.printf("ceiling bbb: %s\n", tSet.ceiling("bbb"));
 System.out.printf("ceiling eee: %s\n", tSet.ceiling("eee"));
 // ceiling(大于)
 System.out.printf("higher bbb: %s\n", tSet.higher("bbb"));
 // subSet()
 System.out.printf("subSet(aaa, true, ccc, true): %s\n", tSet.subSet("aaa", true, "ccc", true));
 System.out.printf("subSet(aaa, true, ccc, false): %s\n", tSet.subSet("aaa", true, "ccc", false));
 System.out.printf("subSet(aaa, false, ccc, true): %s\n", tSet.subSet("aaa", false, "ccc", true));
 System.out.printf("subSet(aaa, false, ccc, false): %s\n", tSet.subSet("aaa", false, "ccc", false));
 // headSet()
 System.out.printf("headSet(ccc, true): %s\n", tSet.headSet("ccc", true));
 System.out.printf("headSet(ccc, false): %s\n", tSet.headSet("ccc", false));
 // tailSet()
 System.out.printf("tailSet(ccc, true): %s\n", tSet.tailSet("ccc", true));
 System.out.printf("tailSet(ccc, false): %s\n", tSet.tailSet("ccc", false));
 // 刪除“ccc”
 tSet.remove("ccc");
 // 將Set轉(zhuǎn)換為數(shù)組
 String[] arr = (String[])tSet.toArray(new String[0]);
 for (String str:arr)
  System.out.printf("for each : %s\n", str);
 // 打印TreeSet
 System.out.printf("TreeSet:%s\n", tSet);
 // 遍歷TreeSet
 for(Iterator iter = tSet.iterator(); iter.hasNext(); ) {
  System.out.printf("iter : %s\n", iter.next());
 }
 // 刪除并返回第一個(gè)元素
 val = (String)tSet.pollFirst();
 System.out.printf("pollFirst=%s, set=%s\n", val, tSet);
 // 刪除并返回最后一個(gè)元素
 val = (String)tSet.pollLast();
 System.out.printf("pollLast=%s, set=%s\n", val, tSet);
 // 清空HashSet
 tSet.clear();
 // 輸出HashSet是否為空
 System.out.printf("%s\n", tSet.isEmpty()?"set is empty":"set is not empty");
 }
}

運(yùn)行結(jié)果:

TreeSet:[aaa, bbb, ccc, ddd, eee]
size : 5
floor bbb: bbb
lower bbb: aaa
ceiling bbb: bbb
ceiling eee: eee
higher bbb: ccc
subSet(aaa, true, ccc, true): [aaa, bbb, ccc]
subSet(aaa, true, ccc, false): [aaa, bbb]
subSet(aaa, false, ccc, true): [bbb, ccc]
subSet(aaa, false, ccc, false): [bbb]
headSet(ccc, true): [aaa, bbb, ccc]
headSet(ccc, false): [aaa, bbb]
tailSet(ccc, true): [ccc, ddd, eee]
tailSet(ccc, false): [ddd, eee]
for each : aaa
for each : bbb
for each : ddd
for each : eee
TreeSet:[aaa, bbb, ddd, eee]
iter : aaa
iter : bbb
iter : ddd
iter : eee
pollFirst=aaa, set=[bbb, ddd, eee]
pollLast=eee, set=[bbb, ddd]
set is empty

補(bǔ)充:Java中關(guān)于使用TreeSet存儲(chǔ)數(shù)據(jù)的自然排序和定制排序

一、題目

創(chuàng)建類的 5 個(gè)對(duì)象,并把這些對(duì)象放入 TreeSet 集合中(TreeSet 需使用泛型和不用泛型分別來(lái)定義)

分別按以下兩種方式對(duì)集合中的元素進(jìn)行排序,并遍歷輸出:

1、使 Employee 實(shí)現(xiàn) Comparable 接口,并按 name 排序

2、創(chuàng)建 TreeSet 時(shí)傳入 Comparator 對(duì)象,按生日日期的先后排序。

二、定義一個(gè) Employee 類

/**
 * 該類包含:private 成員變量 name,age,birthday,其中 birthday 為
 * MyDate 類的對(duì)象;
 * 并為每一個(gè)屬性定義 getter, setter 方法;
 * 并重寫 toString 方法輸出 name, age, birthday
 * @author
 * @create 2021-01-22-15:00
 */
public class Employee implements Comparable<Employee> {
 private String name;
 private int age;
 private MyDate birthday;
 public Employee() {
 }
 public Employee(String name, int age, MyDate birthday) {
  this.name = name;
  this.age = age;
  this.birthday = birthday;
 }
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }
 public int getAge() {
  return age;
 }
 public void setAge(int age) {
  this.age = age;
 }
 public MyDate getBirthday() {
  return birthday;
 }
 public void setBirthday(MyDate birthday) {
  this.birthday = birthday;
 }
 @Override
 public String toString() {
  return "Employee{" +
    "name='" + name + '\'' +
    ", age=" + age +
    ", birthday=" + birthday +
    '}';
 }
  //不用泛型
// @Override
// public int compareTo(Object o) {
//  if(o instanceof Employee){
//   Employee employee = (Employee) o;
//   return this.name.compareTo(employee.name);
//  }
//  throw new RuntimeException("輸入的數(shù)據(jù)類型不一致");
// }
  //使用泛型
 @Override
 public int compareTo(Employee o) {
  return this.name.compareTo(o.name);
 }
}

三、MyDate 類

/**
 * MyDate 類包含:
 * private 成員變量 year,month,day;并為每一個(gè)屬性定義 getter, setter
 * 方法;
 * @author 
 * @create 2021-01-22-15:00
 */
public class MyDate implements Comparable<MyDate> {
 private int year;
 private int month;
 private int day;
 public MyDate() {
 }
 public MyDate(int year, int month, int day) {
  this.year = year;
  this.month = month;
  this.day = day;
 }
 @Override
 public String toString() {
  return "MyDate{" +
    "year=" + year +
    ", month=" + month +
    ", day=" + day +
    '}';
 }
 public int getYear() {
  return year;
 }
 public void setYear(int year) {
  this.year = year;
 }
 public int getMonth() {
  return month;
 }
 public void setMonth(int month) {
  this.month = month;
 }
 public int getDay() {
  return day;
 }
 public void setDay(int day) {
  this.day = day;
 }
 @Override
 public int compareTo(MyDate o) {
  int minusYear= this.year-o.year;
  if (minusYear !=0){
   return minusYear;
  }
  int minusMonth= this.month-o.month;
  if (minusMonth !=0){
   return minusMonth;
  }
  return this.day-o.day;
 }
}

四、單元測(cè)試

(一)

@Test
 public void test1(){
  TreeSet<Employee> set = new TreeSet<>();
  set.add(new Employee("hh",23,new MyDate(1992,4,12)));
  set.add(new Employee("ff",43,new MyDate(1956,5,4)));
  set.add(new Employee("aa",27,new MyDate(1936,8,6)));
  set.add(new Employee("gg",38,new MyDate(1992,4,4)));
  Iterator<Employee> iterator = set.iterator();
  while (iterator.hasNext()){
   System.out.println(iterator.next());
  }
 }

結(jié)果如下:

(二)

 @Test
 public void test2(){
  TreeSet<Employee> set = new TreeSet<>(new Comparator<Employee>() {
   @Override
   public int compare(Employee e1, Employee e2) {
    //加上泛型
    MyDate b1 = e1.getBirthday();
    MyDate b2 = e2.getBirthday();
    return b1.compareTo(b2);
    //不加泛型
//    if (o1 instanceof Employee && o2 instanceof Employee){
//     Employee m1 = (Employee) o1;
//     Employee m2 = (Employee) o2;
//     MyDate m1Birthday = m1.getBirthday();
//     MyDate m2Birthday = m2.getBirthday();
//
//     int minusYear = m1Birthday.getYear()- m2Birthday.getYear();
//     if (minusYear!=0){
//      return minusYear;
//     }
//     int minusMonth = m1Birthday.getMonth()- m2Birthday.getMonth();
//     if (minusMonth!=0){
//      return minusMonth;
//     }
//     int minusDay = m1Birthday.getDay()- m2Birthday.getDay();
//     return minusDay;
//
//    }
//    throw new RuntimeException("傳入的數(shù)據(jù)類型不一致");
   }
  });
  set.add(new Employee("hh",23,new MyDate(1944,12,4)));
  set.add(new Employee("ff",43,new MyDate(1957,5,4)));
  set.add(new Employee("aa",27,new MyDate(1906,12,6)));
  set.add(new Employee("gg",38,new MyDate(1906,4,4)));
  Iterator<Employee> iterator = set.iterator();
  while (iterator.hasNext()){
   System.out.println(iterator.next());
  }
 }

結(jié)果如下:

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。

相關(guān)文章

  • Java web spring異步方法實(shí)現(xiàn)步驟解析

    Java web spring異步方法實(shí)現(xiàn)步驟解析

    這篇文章主要介紹了Java web spring異步方法實(shí)現(xiàn)步驟解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-08-08
  • springboot aop切到service層,不生效問(wèn)題

    springboot aop切到service層,不生效問(wèn)題

    這篇文章主要介紹了springboot aop切到service層,不生效問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-05-05
  • SpringCloud高可用配置中心Config詳解

    SpringCloud高可用配置中心Config詳解

    Spring Cloud Config 是一個(gè)解決分布式系統(tǒng)的配置管理方案,它包含了 server 和 client 兩個(gè)部分,這篇文章主要介紹了SpringCloud之配置中心Config(高可用),需要的朋友可以參考下
    2022-04-04
  • 教你怎么用Java回溯算法解數(shù)獨(dú)

    教你怎么用Java回溯算法解數(shù)獨(dú)

    一直不太會(huì)數(shù)獨(dú)問(wèn)題,這次下決定搞明白,所以整理了本篇文章,文中有非常詳細(xì)的代碼示例,對(duì)不會(huì)算法的小伙伴們很有幫助,需要的朋友可以參考下
    2021-06-06
  • RestTemplate的DELETE及PUT等請(qǐng)求方法使用精講

    RestTemplate的DELETE及PUT等請(qǐng)求方法使用精講

    這篇文章主要為大家介紹了RestTemplate的DELETE及PUT等請(qǐng)求方法的使用精講,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步
    2022-03-03
  • Java 中使用數(shù)組存儲(chǔ)和操作數(shù)據(jù)

    Java 中使用數(shù)組存儲(chǔ)和操作數(shù)據(jù)

    本文將介紹Java中常用的數(shù)組操作方法,通過(guò)詳細(xì)的示例和解釋,幫助讀者全面理解和掌握這些方法,具有一定的參考價(jià)值,感興趣的可以了解一下
    2023-09-09
  • mybatis Example的Criteria用法:or與isNull詳解

    mybatis Example的Criteria用法:or與isNull詳解

    這篇文章主要介紹了mybatis Example的Criteria用法:or與isNull詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-12-12
  • Java 字節(jié)數(shù)組類型(byte[])與int類型互轉(zhuǎn)方法

    Java 字節(jié)數(shù)組類型(byte[])與int類型互轉(zhuǎn)方法

    下面小編就為大家?guī)?lái)一篇Java 字節(jié)數(shù)組類型(byte[])與int類型互轉(zhuǎn)方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-02-02
  • java多線程編程之捕獲子線程異常示例

    java多線程編程之捕獲子線程異常示例

    java多線程程序中所有線程都不允許拋出未捕獲的checked exception,也就是說(shuō)各個(gè)線程需要自己把自己的checked exception處理掉。但是無(wú)法避免的是unchecked exception,也就是RuntimeException,當(dāng)拋出異常時(shí)子線程會(huì)結(jié)束,但不會(huì)影響主線程
    2014-02-02
  • jsp+dao+bean+servlet(MVC模式)實(shí)現(xiàn)簡(jiǎn)單用戶登錄和注冊(cè)頁(yè)面

    jsp+dao+bean+servlet(MVC模式)實(shí)現(xiàn)簡(jiǎn)單用戶登錄和注冊(cè)頁(yè)面

    這篇文章主要介紹了jsp+dao+bean+servlet(MVC模式)實(shí)現(xiàn)簡(jiǎn)單用戶登錄和注冊(cè)頁(yè)面,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-12-12

最新評(píng)論