//小根堆、大根堆

package Pri;

import java.util.Comparator;
import java.util.PriorityQueue;

/**
 * @author 真他喵的学不动咯
 * @create 2022-08-13--17:12
 */
public class Priority {   //小根堆、大根堆
    //大根堆
    public static class MyComparator implements Comparator<Integer>{
        //负数,第一个参数在前
        //整数,第二个参数在前

        @Override
        public int compare(Integer o1, Integer o2) {
            if (o1>o2){
                return -1;
            }
            if (o1<o2){
                return 1;
            }else{
            return 0;}
        }
    }


    public static void  main(String[] args){
        //小根堆,小数在前
        PriorityQueue<Integer> heap=new PriorityQueue<>();
        heap.add(6);
        heap.add(1);
        heap.add(8);
        heap.add(9);
        heap.add(5);
        heap.add(7);
        System.out.println(heap.peek());  //输出最小值但不弹出
        System.out.println("****************************************");
        while (!heap.isEmpty()){          //如果堆不是空的,那就全弹完  按顺序从小到大
            System.out.println(heap.poll());
        }
        System.out.println("================");
        //大根堆,大数在前
        PriorityQueue<Integer> heap1=new PriorityQueue<>(new MyComparator());   //把比较器塞在构造方法中,注意引用格式
        heap1.add(6);
        heap1.add(1);
        heap1.add(8);
        heap1.add(9);
        heap1.add(5);
        heap1.add(7);
        System.out.println(heap1.peek());  //输出最小值但不弹出
        System.out.println("****************************************");
        while (!heap1.isEmpty()){          //如果堆不是空的,那就全弹完  按顺序从小到大
            System.out.println(heap1.poll());
        }
        System.out.println("================");
        String t1="eabc";
        String t2="d";
        System.out.println(t1.compareTo(t2));
    }


    //有序结构排序
   /*
   1.PriorityQueue<Show.Student> heap=new PriorityQueue<>(new Show.IdComparator());  //优先队列【实质是堆】
    heap.add(s1);
   2.TreeSet<Show.Student> treeSet=new TreeSet<>(new Show.IdComparator());   //TreeSet实现有序
    */

    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值