public class TestTreeSet { public static void main(String[] agrs){ TreeSet<String> treeSet = new TreeSet<String>(); System.out.println("TreeSet初始化容量大小:"+treeSet.size()); //元素添加: treeSet.add("hello"); treeSet.add("!"); treeSet.add("a"); treeSet.add("new"); treeSet.add("world"); System.out.println("TreeSet容量大小:" + treeSet.size()); System.out.println("TreeSet元素顺序为:" + treeSet.toString()); //增加for循环遍历: for(String str:treeSet){ System.out.println("遍历元素:"+str); } //迭代器遍历:升序 Iterator<String> iteratorAesc = treeSet.iterator(); while(iteratorAesc.hasNext()){ String str = iteratorAesc.next(); System.out.println("遍历元素升序:"+str); } //迭代器遍历:降序 Iterator<String> iteratorDesc = treeSet.descendingIterator(); while(iteratorDesc.hasNext()){ String str = iteratorDesc.next(); System.out.println("遍历元素降序:"+str); } //元素获取:实现NavigableSet接口 String firstEle = treeSet.first();//获取TreeSet头节点: System.out.println("TreeSet头节点为:" + firstEle); // 获取指定元素之前的所有元素集合:(不包含指定元素) SortedSet<String> headSet = treeSet.headSet("new"); System.out.println("new节点之前的元素为:"+headSet.toString()); //获取给定元素之间的集合:(包含头,不包含尾) SortedSet subSet = treeSet.subSet("hello", "new"); System.out.println("!--world之间节点元素为:"+subSet.toString()); //集合判断: boolean isEmpty = treeSet.isEmpty(); System.out.println("TreeSet是否为空:"+isEmpty); boolean isContain = treeSet.contains("who"); System.out.println("TreeSet是否包含who元素:"+isContain); //元素删除: boolean jiaboyanRemove = treeSet.remove("world"); System.out.println("jiaboyan元素是否被删除"+jiaboyanRemove); //集合中不存在的元素,删除返回false boolean whoRemove = treeSet.remove("who"); System.out.println("who元素是否被删除"+whoRemove); //删除并返回第一个元素:如果set集合不存在元素,则返回null String pollFirst = treeSet.pollFirst(); System.out.println("删除的第一个元素:"+pollFirst); //删除并返回最后一个元素:如果set集合不存在元素,则返回null String pollLast = treeSet.pollLast(); System.out.println("删除的最后一个元素:"+pollLast); treeSet.clear();//清空集合: } } 输出: TreeSet初始化容量大小:0 TreeSet容量大小:5 TreeSet元素顺序为:[a, hello, new, world, !] 遍历元素:a 遍历元素:hello 遍历元素:new 遍历元素:world 遍历元素:! 遍历元素升序:a 遍历元素升序:hello 遍历元素升序:new 遍历元素升序:world 遍历元素升序:! 遍历元素降序:! 遍历元素降序:world 遍历元素降序:new 遍历元素降序:hello 遍历元素降序:a TreeSet头节点为:a new节点之前的元素为:[a, hello] !--world之间节点元素为:[hello] TreeSet是否为空:false TreeSet是否包含who元素:false jiaboyan元素是否被删除true who元素是否被删除false 删除的第一个元素:a 删除的最后一个元素:!
public class TreeSetTest { public static void main(String[] agrs){ naturalSort(); } //自然排序顺序:升序 public static void naturalSort(){ TreeSet<String> treeSetString = new TreeSet<String>(); treeSetString.add("a"); treeSetString.add("z"); treeSetString.add("d"); treeSetString.add("b"); System.out.println("字母顺序:" + treeSetString.toString()); TreeSet<Integer> treeSetInteger = new TreeSet<Integer>(); treeSetInteger.add(1); treeSetInteger.add(24); treeSetInteger.add(23); treeSetInteger.add(6); System.out.println(treeSetInteger.toString()); System.out.println("数字顺序:" + treeSetString.toString()); } } 输出: 字母顺序:[a, b, d, z] 数字顺序:[1, 6, 23, 24]
class Location { private String name; private Integer ID; public Location(String name, Integer ID) { this.name = name; this.ID = ID; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getID() { return ID; } public void setID(Integer ID) { this.ID = ID; } } public class TestTreeSet1 { public static void main(String[] agrs){ customSort(); } //自定义排序顺序:升序 public static void customSort(){ TreeSet<Location> treeSet = new TreeSet<>(); //排序对象: Location location1 = new Location("a" , 1); Location location2 = new Location("new" , 2); Location location3 = new Location("world" , 3); //添加到集合: treeSet.add(location1); treeSet.add(location2); treeSet.add(location3); System.out.println("TreeSet集合顺序为:"+treeSet); } } 输出报错: Exception in thread "main" java.lang.ClassCastException: java4interview.Location cannot be cast to java.lang.Comparable
class Location implements Comparable<Location> { private String name; private Integer ID; public Location(String name, Integer ID) { this.name = name; this.ID = ID; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getID() { return ID; } public void setID(Integer ID) { this.ID = ID; } @Override public int compareTo(Location location) { int num = this.name.length() - location.name.length(); return num == 0 ? this.ID - location.ID :num; } @Override public String toString() { return "Location{" + "name=‘" + name + ‘\‘‘ + ", ID=" + ID + ‘}‘; } } public class TestTreeSet1 { public static void main(String[] agrs){ customSort(); } //自定义排序顺序:升序 public static void customSort(){ TreeSet<Location> treeSet = new TreeSet<>(); //排序对象: Location location1 = new Location("a" , 10); Location location2 = new Location("new" , 4); Location location3 = new Location("world" , 3); Location location4 = new Location("hello" , 5); //添加到集合: treeSet.add(location1); treeSet.add(location2); treeSet.add(location3); treeSet.add(location4); System.out.println(treeSet); } } 输出: [Location{name=‘a‘, ID=10}, Location{name=‘new‘, ID=4}, Location{name=‘world‘, ID=3}, Location{name=‘hello‘, ID=5}]
此外,还有另一种方式,那就是实现Comparetor<t>接口,并重写compare方法;
//自定义location类的比较器: public class locationComparator implements Comparator<Location> { public int compare(Location location1, Location location2) { int num = location1.getID() - location2.getID(); return num == 0 ? location1.getName().length() - location2.getName().length() : num; } }
//自定义location类的比较器: public class LocationComparator implements Comparator<Location> { public int compare(Location location1, Location location2) { int num = location1.getID() - location2.getID(); return num == 0 ? location1.getName().length() - location2.getName().length() : num; } }
关于compareTo()、compare()方法:
public class TreeSet<E> extends AbstractSet<E> implements NavigableSet<E>, Cloneable, java.io.Serializable { //TreeSet中保存元素的map对象: private transient NavigableMap<E,Object> m; //map对象中保存的value: private static final Object PRESENT = new Object(); }
public class TreeSet<E> extends AbstractSet<E> implements NavigableSet<E>, Cloneable, java.io.Serializable { //最底层的构造方法,不对外。传入一个NavigableMap接口的实现类 TreeSet(NavigableMap<E,Object> m) { this.m = m; } //无参构造:向底层构造传入一个TreeMap对象: public TreeSet() { this(new TreeMap<E,Object>()); } //传入比较器的构造:通常传入一个自定义Comparator的实现类; public TreeSet(Comparator<? super E> comparator) { this(new TreeMap<>(comparator)); } //将集合Collection传入TreeSet中: public TreeSet(Collection<? extends E> c) { this(); addAll(c); } //将集合SortedSet传入TreeSet中: public TreeSet(SortedSet<E> s) { this(s.comparator()); addAll(s); } }
public boolean add(E e) { return m.put(e, PRESENT)==null; }
public boolean remove(Object o) { return m.remove(o)==PRESENT; }
public interface NavigableSet<E> extends SortedSet<E> { E lower(E e);//返回此set集合中小于e元素的最大元素 E floor(E e);//返回此set集合中小于等于e元素的最大元素 E ceiling(E e);//返回此set集合中大于等于e元素的最小元素 E higher(E e);//返回此set集合中大于e元素的最小元素 E pollFirst(); //获取并移除此set集合中的第一个元素 E pollLast();//获取并移除此set集合中的最后一个元素 Iterator<E> iterator();//返回此set集合的迭代器--升序 NavigableSet<E> descendingSet();//以倒序的顺序返回此set集合 Iterator<E> descendingIterator();//返回此set集合的迭代器--倒序 //返回此set集合的部分元素--从fromElement开始到toElement结束,其中fromInclusive、toInclusive意为返回的集合是否包含头尾元素 NavigableSet<E> subSet(E fromElement, boolean fromInclusive, E toElement, boolean toInclusive); //返回此set集合的部分元素--小于toElement,inclusive意味返回的集合是否包含toElement NavigableSet<E> headSet(E toElement, boolean inclusive); //返回此set集合的部分元素--从fromElement开始到toElement结束,包含头不含为尾 SortedSet<E> subSet(E fromElement, E toElement); //返回此set集合的部分元素--小于toElement SortedSet<E> headSet(E toElement); //返回此set集合的部分元素--大于等于toElement SortedSet<E> tailSet(E fromElement); }
public interface SortedSet<E> extends Set<E> { //返回与排序有关的比较器 Comparator<? super E> comparator(); //返回从fromElement到toElement的元素集合: SortedSet<E> subSet(E fromElement, E toElement); //返回从第一个元素到toElement元素的集合: SortedSet<E> headSet(E toElement); //返回从fromElement开始到最后元素的集合: SortedSet<E> tailSet(E fromElement); //返回集合中的第一个元素: E first(); //返回集合中的最后一个元素: E last(); }
原文:https://www.cnblogs.com/liujiarui/p/14727507.html