Framework of the class set in Java. (2) (42nd sets)

Source: Internet
Author: User

The following is a java4android video tutorial from marschen.

The class set framework is the class and interface provided by JDK.
Main content of this set.

1. Collection and iterator Interfaces
2. How to Use set and hashset

Collection Interface
Boolean add adds an object to the set
Clear deletes all objects in the set.
Isempty determines whether the set is empty
Remove deletes an object reference from the collection.
Int size (); returns the number of elements in the set.

A sub-interface of collection is set.
The Set interface has an implementation class that is hastset.

Import Java. util. set; import Java. util. hashset; public class test {public static void main (string ARGs []) {hashset <string> hashset = new hashset <string> (); set <string> set = hashset; // set <stinrg> set = new hashset <string> (); Boolean b1 = set. isempty (); system. out. println (B1); set. add ("A"); set. add ("B"); set. add ("C"); set. add ("D"); Boolean b2 = set. isempty (); system. out. println (B2); int I = set. size (); system. out. println ("the length of the Set object before removing is" + I); // set. clear (); set. remove ("A"); Int J = set. size (); system. out. println ("the length of the Set object after removing is" + J );}}

Import Java. util. set; import Java. util. hashset; import Java. util. iterator; public class test1 {public static void main (string ARGs []) {// hashset <string> hashset = new hashset <string> (); // set <string> set = hashset; // iterator <-- collection <--- set <----- hashset // <--- list <---- arraylist // hasnext () Next () set <string> set = new hashset <string> (); set. add ("A"); set. add ("B"); set. add ("C"); set. add ("D"); // call the iterator method of the set object to generate an iterator object, which is used to discard the entire set object. iterator <string> it = set. iterator (); // generates an iterator object. /* Boolean b1 = it. hasnext (); If (B1) {string S = it. next (); system. out. println (s);} Boolean b2 = it. hasnext (); If (B2) {string S = it. next (); system. out. println (s);} */while (it. hasnext () {string S = it. next (); system. out. println (s );}}}


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.