Java TreeSet with Comparator sorting

TreeSet guarantees no duplicate data, also guarantees long(n) time complexity for add(), remove(), contains().

 
import java.util.Comparator;
import java.util.TreeSet;
 
public class MySetWithCompr {
 
    public static void main(String b[]){
         
        TreeSet<MyComp> ts = new TreeSet<MyComp>(new MyCompC());
        ts.add(new MyComp("RED", 1));
        System.out.println("added");
        ts.add(new MyComp("ORANGE", 1));
        System.out.println("added");
        ts.add(new MyComp("BLUE", 1));
        System.out.println("added");
        ts.add(new MyComp("GREEN", 1));
        System.out.println("added");
        System.out.println(ts.add(new MyComp("GREEN", 2)));           
        System.out.println("green 2 added");
        for(MyComp a:ts) {
            System.out.println(a.name + " " + a.i);
        }
        System.out.println("remove:" + ts.remove(new MyComp("GREEN",0))); // returns true, the nice thing is that you can remove items according to its feature.
        for(MyComp a:ts) {
            System.out.println(a.name + " " + a.i);
        }
    }
}
 
class MyComp implements Comparable<MyComp> {
    public String name;
    public int i;

    public MyComp(String n, int x) {name = n; i = x;}

    @Override
    public int compareTo(MyComp entry) {
        if(name.equals(entry.name)) {
            System.out.println("local name = " + name);
            System.out.println("local i = " + i);
            System.out.println("entry name = " + entry.name);
            System.out.println("entry i = " + entry.i);
            entry.i = i;
            System.out.println("local name = " + name);
            System.out.println("local i = " + i);
            System.out.println("entry name = " + entry.name);
            System.out.println("entry i = " + entry.i);
            return 0;
        }
        else {
            return name.equals(entry.name) ? -1 : 1;
        }
    }
}

class MyCompC implements Comparator<MyComp>{
 
    @Override
    public int compare(MyComp str1, MyComp str2) {
        return str1.compareTo(str2);
    }
     
}

 

local name = RED
local i = 1
entry name = RED
entry i = 1
local name = RED
local i = 1
entry name = RED
entry i = 1
added
added
added
added
local name = GREEN
local i = 2
entry name = GREEN
entry i = 1
local name = GREEN
local i = 2
entry name = GREEN
entry i = 2
false
green 2 added
RED 1
ORANGE 1
BLUE 1
GREEN 2 (see this value has been updated)
local name = GREEN
local i = 0
entry name = GREEN
entry i = 2
local name = GREEN
local i = 0
entry name = GREEN
entry i = 0
remove:true (remove successfully, you can remove based on "GREEN", not ("GREEN", 2). sometimes you don't know what's the value associated with GREEN, but you know you need to delete "GREEN", then you use this to achieve that.)
RED 1
ORANGE 1
BLUE 1

转载于:https://www.cnblogs.com/RuiYan/p/5463989.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值