TreeSet如何检查重复项 [英] How TreeSet checks for Duplicates

查看:117
本文介绍了TreeSet如何检查重复项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在检查TreeSet如何检查重复元素并具有以下代码:

  import java.util。*; 

public class TreeDemo {

public static void main(String [] args)
{
TreeDemo()。go();

$ b $ public void go()
{
Song s1 = new Song(song1,artist1);
歌曲s2 =新歌曲(song2,artist2);
歌曲s3 =新歌(song3,artist3);
歌曲s4 =新歌曲(song3,artist3);

Set< Song> tree = new TreeSet< Song>();

tree.add(s1);
tree.add(s2);
tree.add(s3);
tree.add(s4);

System.out.println(tree);



$ b class Song实现了可比较的< Song> {
private String title;
私人字符串艺术家;

public Song(String t,String a)
{
title = t;
artist = a;
}

public String getTitle(){
return title;


public int compareTo(Song s){
// Song s =(Song)o;
返回title.compareTo(s.getTitle());
}

public String toString(){
return title;
}

}

当我执行这段代码时,获得以下输出:

  [song1,song2,song3] 

我的问题是: -


  • 即使我没有实现hashCode和equals方法(我实现了Comparable接口,因为它的强制性,并且需要保持Set排序),TreeSet是如何确定重复的?

  • 它是否使用Object类的默认实现?它看起来像使用标题字段进行此检查,因为当我添加将它视为重复,但是当我添加它不会将其视为重复。


$

解决方案

TreeSet (或技术上,支持它的 TreeMap )仅使用 compareTo()函数比较元素。
它不使用 Object .equals() .hashCode ()。而且,如果它使用了其中的任何一个,那么你的输出将是 $ b

  [song1,song2,song3,song3] 

因为对象的默认实现使用内存地址测试对象相等,而不是它们的成员。


I am checking how TreeSet checks for duplicate elements and have the following code

  import java.util.*;

  public class TreeDemo{

    public static void main(String[] args)
        {
            new TreeDemo().go();
        }

    public void go()
    {
        Song s1 = new Song("song1","artist1");
        Song s2 = new Song("song2","artist2");
        Song s3 = new Song("song3","artist3");
        Song s4 = new Song("song3","artist3");

        Set<Song> tree = new TreeSet<Song>();

        tree.add(s1);
        tree.add(s2);
        tree.add(s3);
        tree.add(s4);

        System.out.println(tree);

    }
}

class Song implements Comparable<Song>{
    private String title;
    private String artist;

    public Song(String t, String a)
    {
        title=t;
        artist=a;
    }

    public String getTitle(){
        return title; 
    }

    public int compareTo(Song s){
        //Song s = (Song)o;
        return title.compareTo(s.getTitle());
    }

public String toString(){
    return title;
}

}

When I execute this code, I get the following output

[song1, song2, song3]

My question is:-

  • Even if I haven't implemented hashCode and equals method (I did implement the Comparable interface since its mandatory and needed to keep the Set sorted), how did TreeSet determine the duplicates?
  • Did it use Object class default implementation? It looks like it used "title" field for this check since when I add treats it as duplicate but when I add it doesn't treats it as duplicate.

Thanks.

解决方案

TreeSet (or technically, the TreeMap that backs it) only uses the compareTo() function to compare the elements. It does not use Object's .equals() or .hashCode(). Moreover, if it had used any of them, your output would have been

[song1, song2, song3, song3]

because Object's default implementation uses memory addresses to test object equality, not their members.

这篇关于TreeSet如何检查重复项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆