Java Language Collection schema (set rule set)

Source: Internet
Author: User

There is a sequence of objects with no duplicate elements in the collection.

Java provides the appropriate classes and interfaces to store such elements as--set rule sets.

Here's an example to learn the set collection schema, which contains comments:

Package com.brucezhang.test;  
Import Java.util.HashSet;  
Import Java.util.Iterator;  
Import Java.util.LinkedHashSet;  
      
Import Java.util.Set;  
        public class Settest {/** * @param args */public static void main (string[] args) { The TODO auto-generated method stub//hashset is a collection structure that stores data that is not as efficient as inserting data into the order///not in sequence set<st  
              
        ring> set = new hashset<string> ();  
        Set.add ("Beijing");  
        Set.add ("Shanghai");  
        Set.add ("Hefei");  
        Set.add ("Dalian");  
        /** * Because it is a collection schema, when inserting two identical elements, only a * */Set.add ("Guangzhou") is stored;  
              
        Set.add ("Guangzhou");  
              
SYSTEM.OUT.PRINTLN (set);  
              
Iterator iterator = Set.iterator ();  
while (Iterator.hasnext ()) {//System.out.println (Iterator.next () + "");         }//For (Object element:set) {// System.out.println (element.tostring () + ""); * * * * When you need to use sequential storage, you need to use the Linkedhashset architecture, but not high efficiency * * */Set<s  
              
        tring> Set2 = new linkedhashset<string> ();  
        Set2.add ("Beijing");  
        Set2.add ("Shanghai");  
        Set2.add ("Hefei");  
        Set2.add ("Dalian");  
        /** * Because it is a collection schema, when inserting two identical elements, only a * */Set2.add ("Guangzhou") is stored;  
              
        Set2.add ("Guangzhou");  
              
System.out.println (Set2);  
              
Iterator iterator = Set.iterator ();  
while (Iterator.hasnext ()) {//System.out.println (Iterator.next () + "");  
}//For (Object Element:set2) {//System.out.println (element.tostring () + ""); //      }  
    }  
      
}

The results of the operation are as follows:

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.