Android's set interface usage

Source: Internet
Author: User

Recently encountered on the project, to sort the date in the database, check out the Java API found that the set interface can achieve its own effect, studied a bit

Set interface

set does not allow the same elements to be included, and if an attempt is made to add two identical elements to the same collection, the Add method returns False. The
set determines that two objects are the same, not using the = = operator, but by the Equals method. That is, as long as two objects are returned True,set with the Equals method, the two objects will not be accepted. The

HashSet
HashSet has the following characteristics
 The order of the elements is not guaranteed, and the order may vary
 Not synchronized
 The collection element can be null, but only one null
When an element is deposited into a hashset union, HashSet calls the object's Hashcode () method to get the Hashcode value of the object, and then determines where the object is stored in hashcode based on the HashSet value.
Simply put, the hashset set determines that two elements are equal by two objects that are compared by the Equals method, and that the Hashcode () method of two objects returns equal
Note that if you want to put an object in HashSet, Override the Equals method of the corresponding class for the object, and you should override its Hashcode () method. The rule is that if two objects return true through the Equals method, their hashcode should also be the same. In addition, the attribute used in the object as the equals comparison criterion should be used to calculate the value of the hashcode. The

Linkedhashset
Linkedhashset collection also determines where an element is stored, based on the hashcode value of the element, but it maintains the order of the elements using the linked list. This makes the elements appear to be saved in an insertion order, that is, when the collection is traversed, Linkedhashset will access the elements of the collection in the order in which they are added. The
Linkedhashset performance is better than hashset when iterating through all the elements in the set, but the performance is slightly inferior to hashset at insert time. The

TreeSet class
TreeSet is the only implementation class for the SortedSet interface, and TreeSet ensures that the collection element is in the sorted state. TreeSet supports two sorting methods, natural sorting and custom sorting, in which the natural sort is the default sorting method. The object that is added to the treeset should be the same class.

TreeSet determines that two objects are unequal by two objects returning false through the Equals method, or by comparing the CompareTo method without returning 0


Natural Sort
Natural sorting uses the CompareTo (Object obj) method to sort the elements to compare the size relationships between elements, and then arranges the elements in ascending order.
Java provides a comparable interface that defines a CompareTo (object obj) method that returns an integer value that implements the object of the interface to compare the size.
The Obj1.compareto (obj2) method returns 0, indicating that the two objects being compared are equal, and if a positive number is returned, the OBJ1 is greater than obj2, and if it is negative, obj1 is less than obj2.
If we always return true for the Equals method of two objects, then the CompareTo method returned by the two objects should return a 0
Custom sorting

Natural sorting is based on the size of the collection elements, in ascending order, if you want to customize the sort, you should use the comparator interface to implement the int compare (T o1,t O2) method

Android Test Program:

Package Com.example.zzset;import Java.util.hashset;import Java.util.linkedhashset;import java.util.Set;import Java.util.treeset;import Android.app.activity;import Android.os.bundle;import Android.view.Menu;import Android.widget.textview;public class Mainactivity extends Activity {set<string> sl=new linkedhashset<string > (); Set<string> sh=new hashset<string> (); Set<string> st=new treeset<string> ();  String userName = "2016,2013,2012,2011,2011,2014,2015,2016"; TextView TEXTSL; TextView Text1; TextView Textsh;    TextView textst;        @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (R.layout.activity_main);        TEXTSL = (TextView) This.findviewbyid (R.id.text);        Text1 = (TextView) This.findviewbyid (R.ID.TEXTVIEW1);        Textsh = (TextView) This.findviewbyid (R.ID.TEXTVIEW2);                TEXTST = (TextView) This.findviewbyid (R.ID.TEXTVIEW3); String[] Uernames=usernaMe.split (","); for (String name:uernames) {sl.add (name);} Textsl.settext ("textsl-->" +sl.tostring ()); String[] Uernamesh=username.split (","); for (String Name:uernamesh) {sh.add (name);} Textsh.settext ("textsh-->" +sh.tostring ()); String[] Uernamest=username.split (","); for (String name:uernamest) {st.add (name);} Textst.settext ("textst-->" +st.tostring ()); StringBuffer sb=new StringBuffer (); for (String str:sl) {sb.append (str). Append (",");    Text1.settext ("TEXTSL re-stitching--" +SB); }    }
Test results




The results can be seen


linkedhashset Remove duplicate elements , element position unchanged

HashSet Remove Duplicate elements , change the position of the elements

TreeSet Remove duplicate elements , sort after output


Reference: http://www.cnblogs.com/Terry-greener/archive/2011/12/02/2271707.html

Android's set interface usage

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.