SortedSet接口与TreeSet实现类(二)
来源:互联网 发布:各编程语言比较 编辑:程序博客网 时间:2024/06/06 07:28
package cn.kcn.set;import java.util.*;/* * 让SortedSet集合做到排序还有另一种方式:java.util.Comparator; * 单独编写一个比较器 */public class SortedSetTest02 { public static void main(String[] args) { //创建TreeSet集合时候提供一个比较器 SortedSet ss = new TreeSet(new ProductComparator()); Product p1 = new Product(3.5); Product p2 = new Product(5.3); Product p3 = new Product(7.8); Product p4 = new Product(8.9); Product p5 = new Product(6.1); ss.add(p1); ss.add(p2); ss.add(p3); ss.add(p4); ss.add(p5); Iterator it = ss.iterator(); while(it.hasNext()){ System.out.println(it.next()); } }}//商品类class Product{ double price;//商品价格 Product(double price){ this.price = price; } @Override public String toString() { return price+""; }}//单独编写一个比较器class ProductComparator implements Comparator{ //需求:按照商品价格排序 public int compare(Object o1,Object o2){ //对o1,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; } }}
0 0
- SortedSet接口与TreeSet实现类(二)
- SortedSet接口与TreeSet实现类(一)
- SortedSet与SortedMap,TreeSet与TreeMap
- Java类集--Set接口、HashSet、TreeSet、SortedSet接口
- SortedSet和TreeSet
- SortedSet和TreeSet
- SortedSet和TreeSet
- SortedSet和TreeSet
- SortedSet和TreeSet
- SortedSet接口
- SortedSet接口
- SortedSet接口
- Java类集框架——Set接口和子接口SortedSet以及SortedSet接口的子类TreeSet之间的联系和使用、排序设置重复元素的说明
- [疯狂Java]集合:SortedSet、TreeSet
- TreeSet实现排序(二)
- java类集中的SortedSet接口
- Set 二(TreeSet与HashSet)十五
- Core Java (二十四) SortedMap,NavigableMap,SortedSet,NavigableSet接口
- Linux下安装Tomcat服务器和部署Web应用
- 22个HTML5初级技巧
- 多线程实现数据库的并发操作
- 插入排序/选择排序/交换排序/归并排序/基数排序
- Java的作业
- SortedSet接口与TreeSet实现类(二)
- 第一天
- Map集合常用方法
- 看博客的日常
- C++ 链表实现
- Spring定时任务的简单例子(转载)
- Java-面向对象(高级篇)--接口的基本概念
- C++ primer | 第八章 IO 库
- JSON详解