Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does Java use the Set interface to store arrays without repeating elements

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains how Java uses the Set interface to store an array of non-repeating elements. Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how Java uses the Set interface to store arrays without repeating elements.

Set interface

The Set interface, like the List interface, inherits the Collection interface. The elements in the Set interface are out of order, and there are certain rules to ensure that the stored elements are not duplicated.

HashSet collection

HashSet is an implementation class of the Set interface, and the stored elements are non-repeatable and unordered. When an object is added to the HashSet collection, the hashCode () method of the object is first called to calculate the hash value of the object, thus determining the storage location of the element. If the hash value is the same at this time, call the object's equals () method to ensure that there are no duplicate elements at that location.

Package collection class; import java.util.HashSet; import java.util.Iterator; public class Set {public static void main (String [] args) {HashSet set=new HashSet (); set.add ("hello"); set.add ("world"); set.add ("abc"); set.add ("hello"); Iterator it=set.iterator () While (it.hasNext ()) {Object obj=it.next (); System.out.print (obj+ "");}

Running result

As can be seen from the running results, the order in which elements are taken out is not consistent with that in which elements are added, and the duplicate string is removed and added only once, because when the add () method of the HashSet collection is stored in the element, the hashCode () method currently stored in the object is called first to obtain the hash value of the object, and then a storage location is calculated according to the hash value. If there is no element in this location, the element is directly stored in the If an element exists at that location, the equal () method is called to compare the currently stored element with the element at that location in turn. If the return result is false, the element is saved in the collection, and the return result is true, then there is a duplicate element and the element is discarded.

Package collection class; import java.util.HashSet; class Student {String id; String name; public Student (String id,String name) {this.id=id; this.name=name;} public String toString () {String s = id + ":" + name; return s;}} public class Set1 {public static void main (String [] args) {HashSet hs=new HashSet () Student stu1=new Student ("1", "hello"); Student stu2=new Student ("2", "world"); Student stu3=new Student ("1", "hello"); hs.add (stu1); hs.add (stu2); hs.add (stu3); System.out.println (hs);}}

Running result

Duplicate elements are not removed because the hashCode () and equals () methods are not overridden when defining the Student class.

Package API; import java.util.HashSet; class Student {private String id; private String name; public Student (String id,String name) {this.id=id; this.name=name;} / / override toString method public String toString () {return id+ ":" + name } / / override the hashCode method public int hashCode () {/ / return the hash value of the id attribute return id.hashCode ();} public boolean equals (Object obj) {/ / determine whether it is the same object if (this==obj) {return true } / / determine whether the object is of Student type if (! (obj instanceof Student)) {return false;} / / convert the object to Student type Student stu= (Student) obj; / / determine whether the id is the same boolean b=this.id.equals (stu.id); return b }} public class Set2 {public static void main (String [] args) {HashSet hs=new HashSet (); Student stu1=new Student ("1", "hello"); Student stu2=new Student ("2", "world"); Student stu3=new Student ("1", "hello"); hs.add (stu1); hs.add (stu2); hs.add (stu3); System.out.println (hs) }}

Running result

Because the Student class overrides the hashCode () and equals () methods of the Object class. Return the hash value of the id property in the hashCode () method, compare whether the object's id property is equal in the equals () method, and return the result. When the add () method of the HashSet collection is called to add the stu3 object, it is found that its hash value is the same as the stu2 object, and stu2.equals (stu3) returns true. HashSet determines that the two objects are the same, so duplicate Student objects are removed.

At this point, I believe you have a better understanding of "how Java uses the Set interface to store arrays of non-repeating elements". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report