日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

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

發布時間:2023/12/10 44 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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;} }

測試類

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("小刀");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());} }

運行結果

總結

以上是生活随笔為你收集整理的java学习(123):treeset排序集合的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。