0
点赞
收藏
分享

微信扫一扫

JAVA泛型——自定义比较器的几种方式

老王420 2022-01-05 阅读 53

方式一:通过实现Comparabler接口,覆盖compareTo方法对对象完成排序属性

例子:初始化一个TreeSet类型的集合,当集合中有元素时即对集合中元素以字符串长度为规则进行排序

import java.util.*;
class GenericDemo2 
{
	public static void main(String[] args) 
	{
		TreeSet<String> ts = new TreeSet<String>(new LenComparator());

		ts.add("abcd");
		ts.add("cc");
		ts.add("cba");
		ts.add("aaa");
		ts.add("z");
		ts.add("hahaha");


		Iterator<String> it = ts.iterator();

		while(it.hasNext())
		{
			String s = it.next();
			System.out.println(s);
		}
	}
}


class LenComparator implements Comparator<String>
{
	public int compare(String o1,String o2)
	{
		int num = new Integer(o2.length()).compareTo(new Integer(o1.length()));

		if(num==0)
			return o2.compareTo(o1);
		return num;
	}
}

方式二:通过重写Collections.sort方法的Comparator对象

例子1:这是Kruskal算法中的一部分。定义一个数据类型Edge代表有向加权图的一条边,其中source代表起点,target代表重点,cost代表权值。通过比较权值cost的大小来衡量两条边谁更大。

static class Edge{
        public int source, target, cost;
        Edge(int source, int target, int cost){
            this.source = source;
            this.target = target;
            this.cost = cost;
        }
    }

    //直接走一遍kruskal算法,结果就出来了
    public static int kruskal(int N, Vector<Edge> edges){
        ...
        //排序
        Collections.sort(edges, new Comparator<Edge>() {
            @Override
            public int compare(Edge o1, Edge o2) {
                if(o1.cost<o2.cost)return -1;
                else if(o1.cost<o2.cost)return 1;
                else return 0;
            }
        });
        ...
    }
————————————————
版权声明:本文为CSDN博主「小乖乖的臭坏坏」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_42887138/article/details/122289265

例子2:定义一个数据类型EpElement类,(中间包含的Point类代表平面直角坐标系的坐标,拥有x和y两个整型)比较方式按照纵坐标进行比较

public static class EpElement{
        private GraghBasic.Point P;
        private String direction;//left, right, bottom, top
        private int id;
        EpElement(){}
        EpElement(GraghBasic.Point P, String direction, int id){
            this.P = P;
            this.direction = direction;
            this.id = id;
        }
    }
public static void main(String[] args) {
    //定义EP表Vector容器内的基本元素
    Vector<EpElement> EP = new Vector<EpElement>();
    
    ...
    
	//自定义容器比较器
    Collections.sort(EP, new Comparator<Object>(){
        @Override
        public int compare(Object o1, Object o2) {
            EpElement s1 = (EpElement) o1;
            EpElement s2 = (EpElement) o2;
            if (s1.P.y>s2.P.y){
                return 1;//返回1,就默认前者大于后者
            }
            else if(s1.P.y<s2.P.y){
                return -1;
            }
            else{
                if(s1.P.x>s2.P.x){
                    return 1;
                }
                else if(s1.P.x<s2.P.x){
                    return -1;
                }
                else {
                    return 0;
                }
            }
	        }
	    });
————————————————
版权声明:本文为CSDN博主「小乖乖的臭坏坏」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_42887138/article/details/122104241
举报

相关推荐

0 条评论