Collection Frame _06_hashset Collection

Source: Internet
Author: User
/*
|--set: Elements are unordered (the order in and out is not necessarily consistent) and elements cannot be duplicated. ,
	|--hashset: The underlying data structure is a hash table. is not thread safe. Different steps.
			HashSet is how to guarantee the uniqueness of the element.
			is done by the two methods of the element, hashcode and equals.
			if the element's Hashcode value is the same, the equals is judged to be true.
			equals is not invoked if the hashcode value of the element is different.

			Note that for actions such as determining whether an element exists, as well as deleting it, the dependent method is the hashcode and equals method of the element.
	|--treeset:

The set set is functionally consistent with the collection.
* *
class Hashsetdemo 
{public
	static void sop (Object obj)
	{
		System.out.println (obj);
	} Public
	static void Main (string[] args) 
	{
		HashSet hs = new HashSet ();
		SOP (Hs.add ("Java01"));
		SOP (Hs.add ("Java01"));
		Hs.add ("Java02");
		Hs.add ("java03");
		Hs.add ("java03");
		Hs.add ("Java04");
		Iterator it = Hs.iterator ();
		while (It.hasnext ())
		{
			sop (It.next ());
		}
	}
}


HashSet Store Custom Elements

/* Go to the HashSet collection to make a custom object name and the same age as the same person, repeating elements.
	* * Class Hashsettest {public static void sop (Object obj) {System.out.println (obj);

		public static void Main (string[] args) {hashset hs = new HashSet ();
		Hs.add (New Person ("A1", 11));
		Hs.add (New Person ("A2", 12));
		Hs.add (New Person ("A3", 13));
		Hs.add (New Person ("A2", 12));

		Hs.add (New person ("A4", 14));

		SOP ("A1:" +hs.contains (New Person ("A2", 12));

		Hs.remove (New person ("A4", 13));

		Iterator it = Hs.iterator ();
			while (It.hasnext ()) {person P = (person) it.next ();
		SOP (P.getname () + "::" + p.getage ());
	}} class Hashsetperson {private String name;

	private int age;
		Hashsetperson (String name, int age) {this.name = name;
	This.age = age;
		public int hashcode () {System.out.println (THIS.name + "... hashcode");
	return Name.hashcode () + age * 37; public boolean equals (Object obj) {if (!

		obj instanceof person), return false;
		Hashsetperson p = (hashsetperson) obj; SysteM.out.println (THIS.name + "... equals ..." + p.name);
	Return This.name.equals (p.name) && this.age = = P.age;
	Public String GetName () {return name;
	public int getage () {return age; }
}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.