java 容器

import java.util.*;

class Node2 implements Comparable<Node2> {
    int d = Integer.MAX_VALUE;
    Node2 previous = null;
    int x;
    int y;
    List<Node> nodeAdj = new ArrayList();

    public int compareTo(Node2 node2) {

        if (this.d > node2.d) {
            return 1;
        }
        if (this.d == node2.d) {
            return 0;
        }
        return -1;

    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Node2 node2 = (Node2) o;
        return d == node2.d;
    }

    @Override
    public int hashCode() {

       // System.out.println("11111111111111111");
       // System.out.println(d);
        int t=Objects.hash(d);

       // System.out.println("hash:"+t);

       // System.out.println("22222222222222");

        return t;
    }

    public String toString() {
        return "[" + x + "," + y + "]";
    }
}

public class TestPriorityQueue {

    public  static  void main(String [] args)
    {

        HashSet<Node2> hashSet=new HashSet<>();
        List<Node2> list=new ArrayList<>();

        for(int i=0;i<2;i++)
        {
            Node2 node2=new Node2();
            node2.d=5;
            node2.d=new Random().nextInt(1000);

            hashSet.add(node2);
            list.add(node2);
        }
        for( Node2 node2:list)
        {
            node2.d=5;
           boolean yyy=hashSet.contains(node2);
           System.out.println("contains "+yyy);
          boolean xxx= hashSet.add(node2);
          System.out.println("added "+xxx);
        }
        for( Node2 node2:hashSet)
        {
            System.out.println(node2.d);
        }



    }
}

//output
contains false
added true
contains true
added false
5
5
5
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值