1.Set Collection Overview
A collection that does not contain duplicate elements.
2. Code Introduction
Package Com;import java.util.hashset;import java.util.set;/** * Collection * List ordered (same as stored order and fetch order), REPEATABLE * Set unordered (store order and remove smooth Inconsistent), unique * Although the elements of the set set are unordered, but, as a collection, it must have its own order of storage, * we may store elements in the same order as the storage elements of their memory. * * HashSet: It does not guarantee the set iterator order, especially it does not guarantee that the order is constant. */public class Setdemo {public static void main (string[] args) {//Create collection Object set<string> set = new Hashset<string> ( );//add Element Set.add ("Hello"), Set.add ("World"), Set.add ("World"), Set.add ("Java"), Set.add ("Java");//Iterate over the collection element for (String Str:set) {System.out.println (str);}}}
3.HashSet class overview
The HashSet class does not guarantee the set iteration order.
In particular, it does not guarantee that the order is constant.
HashSet how to guarantee the uniqueness of elements?
The underlying data structure is a hash table.
A hashtable relies on a hash value store.
Package Com;import java.util.hashset;import java.util.set;/** * HashSet: Store string and traverse * Problem: Why does the string have the same content stored only one when the string is stored? * By looking at the Add method, we know that the bottom-up of this method depends on two methods: Hashcode () and Equals () * Step: * First Compare hash value * If same, continue next, compare address value or go to equals () * If different, add directly to the collection * * If the class does not There are overrides for both methods, which are used by default with the object () method. * While the string class overrides the Hashcode () and Equals () methods. So, it can remove the same string of content, leaving only one. */public class Setdemo {public static void main (string[] args) {//Create collection Object set<string> set = new Hashset<string> ( );//add Element Set.add ("Hello"), Set.add ("World"), Set.add ("Java"); Set.add ("World");//Iterate over the collection element for (String Str:set) { System.out.println (str);}}}
4. Store your custom objects
package com;public class student {private string name;private int age; Public student () {}public student (string name,int age) {This.name = name;this.age = age;} Public string getname () {return name;} Public void setname (String name) {this.name = name;} Public int getage () {return age;} Public void setage (Int age) {this.age = age;} @Overridepublic int hashcode () {final int prime = 31;int result = 1;result = prime * result + age;result = prime * result + ((Name == null) ? 0 : name.hashcode ()); return result;} @Overridepublic boolean equals (object obj) {if (this == obj) return true;if (obj == null) return false;if (geTclass () != obj.getclass ()) return false; student other = (Student) obj;if (age != other.age) return false;if (Name == null) {if (Other.name != null) Return false;} else if (!name.equals (other.name)) return false;return true;} @Overridepublic string tostring () {return "Student [name=" + name + ", age=" + age + "]";}} package com;import java.util.hashset;import java.util.set;/** * Requirements: Store custom objects and preserve the uniqueness of the elements. * requirements: If the member variable values for two objects are the same, then the same element. * */public class setdemo2 {public static void main (String[] args) {//define Set Collection Object Set<student> set = new hashset<student> ();// Create student Object Student s1 = new student ("haha", 1); Student s2 = new student ("hehe", 2); Student s3 =&nbSp;new student ("hehe", 3); Student s4 = new student ("haha", 1); Student s5 = new student ("hehe", 1);//add Element Set.add (S1); Set.add (S2); Set.add (S3); Set.add (S4); Set.add (S5);//traverse the collection object for (Student stu : set) {System.out.println (Stu);}}}
Student [Name= hee, age=3]
Student [name= hehe, age=2]
Student [name= haha, age=1]
Student [Name= hee, age=1]
5.
This article is from the "11831428" blog, please be sure to keep this source http://11841428.blog.51cto.com/11831428/1863163
Java Collection Framework--set collection