好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

Java List中数据的去重

list中数据的去重,通常使用将list转换为set,简单直接,因为set集合的特点就是没有重复的元素。需要考虑一下两种情况:

1.list集合中的数据类型是基本数据类型

可以直接将list集合转换成set,就会自动去除重复的元素。

如下示例:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

public class test {

   public static void main(string[] args) {

     list list = new arraylist();

     list.add( 11 );

     list.add( 12 );

     list.add( 13 );

     list.add( 14 );

     list.add( 15 );

     list.add( 11 );

     system.out.println(list);

     set set = new hashset();

     list newlist = new arraylist();

     set.addall(list);

     newlist.addall(set);

     system.out.println(newlist);

   }

}

2.list集合中存储的数据类型是对象类型

需要在对象的实体类中去重写equals()方法和hashcode()方法

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

public class people {

   private string name;

   private string phonenumber;

   public string getname() {

     return name;

   }

   public void setname(string name) {

     this .name = name;

   }

   public string getphonenumber() {

     return phonenumber;

   }

   public void setphonenumber(string phonenumber) {

     this .phonenumber = phonenumber;

   }

   public people(string name, string phonenumber) {

     super ();

     this .name = name;

     this .phonenumber = phonenumber;

}

   @override

   public string tostring() {

     return "people{" +

         "name='" + name + ' '' +

         ", phonenumber='" + phonenumber + ' '' +

         '}' ;

   }

   @override

   public boolean equals(object o) {

     people p = (people) o;

     return name.equals(p.name) && phonenumber.equals(p.phonenumber);

   }

   @override

   public int hashcode() {

     string str = name + phonenumber;

     return str.hashcode();

   }

}

public static void main(string[] args) {

   list<people> listpeople = new arraylist<people>();

   listpeople.add( new people( "张三" , "11111" ));

   listpeople.add( new people( "张三" , "22222" ));

   listpeople.add( new people( "李四" , "33333" ));

   listpeople.add( new people( "张三" , "22222" ));

   set<people> setdata = new hashset<people>();

   setdata.addall(listpeople);

   system.out.println( "list:" + listpeople.tostring());

   system.out.println( "set:" + setdata.tostring());

}

最后,我们拿出string中的equals()方法和hashcode()方法源码来加深认识:

equals()

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

public boolean equals(object anobject) {

   if ( this == anobject) {

     return true ;

   }

   if (anobject instanceof string) {

     string anotherstring = (string)anobject;

     int n = count;

     if (n == anotherstring.count) {

       char v1[] = value;

       char v2[] = anotherstring.value;

       int i = offset;

       int j = anotherstring.offset;

       while (n-- != 0 ) {

         if (v1[i++] != v2[j++])

           return false ;

       }

       return true ;

     }

   }

   return false ;

}

比较两个对象时,首先先去判断两个对象是否具有相同的地址,如果是同一个对象的引用,则直接放回true;如果地址不一样,则证明不是引用同一个对象,接下来就是挨个去比较两个字符串对象的内容是否一致,完全相等返回true,否则false。

hashcode()

?

1

2

3

4

5

6

7

8

9

10

11

12

13

public int hashcode() {

   int h = hash;

   if (h == 0 && count > 0 ) {

     int off = offset;

     char val[] = value;

     int len = count;

     for ( int i = 0 ; i < len; i++) {

       h = 31 *h + val[off++];

     }

     hash = h;

   }

   return h;

}

hashcode()官方定义:

hashcode方法返回该对象的哈希码值。支持该方法是为哈希表提供一些优点,例如,java.util.hashtable 提供的哈希表。

hashcode 的常规协定是:

在 java 应用程序执行期间,在同一对象上多次调用 hashcode 方法时,必须一致地返回相同的整数,前提是对象上 equals 比较中所用的信息没有被修改。从某一应用程序的一次执行到同一应用程序的另一次执行,该整数无需保持一致。

如果根据 equals(object) 方法,两个对象是相等的,那么在两个对象中的每个对象上调用 hashcode 方法都必须生成相同的整数结果。

以下情况不是必需的:如果根据 equals(java.lang.object) 方法,两个对象不相等,那么在两个对象中的任一对象上调用 hashcode 方法必定会生成不同的整数结果。但是,程序员应该知道,为不相等的对象生成不同整数结果可以提高哈希表的性能。

实际上,由 object 类定义的 hashcode 方法确实会针对不同的对象返回不同的整数。(这一般是通过将该对象的内部地址转换成一个整数来实现的,但是 javatm 编程语言不需要这种实现技巧。)

当equals方法被重写时,通常有必要重写 hashcode 方法,以维护 hashcode 方法的常规协定,该协定声明相等对象必须具有相等的哈希码。

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对的支持。如果你想了解更多相关内容请查看下面相关链接

原文链接:https://blog.csdn.net/ym01213/article/details/85333529

查看更多关于Java List中数据的去重的详细内容...

  阅读:15次