java学习(123):treeset排序集合.docx

上传人:A*** 文档编号:142725740 上传时间:2020-08-22 格式:DOCX 页数:2 大小:37.52KB
返回 下载 相关 举报
java学习(123):treeset排序集合.docx_第1页
第1页 / 共2页
java学习(123):treeset排序集合.docx_第2页
第2页 / 共2页
亲,该文档总共2页,全部预览完了,如果喜欢就下载吧!
资源描述

《java学习(123):treeset排序集合.docx》由会员分享,可在线阅读,更多相关《java学习(123):treeset排序集合.docx(2页珍藏版)》请在金锄头文库上搜索。

1、java学习(123):treeset排序集合import java.util.Comparator;public class GoodsSorts implements Comparator public int compare(Object o1,Object o2) Goods g1=(Goods)o1; Goods g2=(Goods)o2; System.out.println(调用排序方法); if(g1.getPrice()g2.getPrice() return -1; else if(g1.getPrice()g2.getPrice() return 1; return 0;

2、 测试类import java.util.TreeSet;public class test63 public static void main(String args) TreeSet tree=new TreeSet(new GoodsSorts();/创建一个采用默认树形自然排序的对象 Goods g0=new Goods(); g0.setName(剃须刀); g0.setPrice(2000.0); Goods g1=new Goods(); g1.setName(西瓜); g1.setPrice(7000.0); Goods g2=new Goods(); g2.setName(小

3、刀); g2.setPrice(3000.0); Goods g3=new Goods(); g3.setName(矿泉水); g3.setPrice(4000.0); tree.add(g3); tree.add(g2); tree.add(g1); tree.add(g0); for(Object o:tree) System.out.println(Goods)o).getName()+t+(Goods)o).getPrice(); System.out.println(第一个+(Goods)tree.first().getName(); System.out.println(最后一个+(Goods)tree.last().getName(); 运行结果

展开阅读全文
相关资源
相关搜索

当前位置:首页 > IT计算机/网络 > 其它相关文档

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号