Learn from teacher Wang Set (eight): Set interface and HashSet class

Source: Internet
Author: User
Tags set set

Set interface and HashSet class
Speaker: Wang Shaohua QQ Group No.: 483773664
Learning Goals

1, understand the characteristics of the set interface

2, Master HashSet class use

First, set interface

The elements in the set set are unordered

The set collection does not allow the same elements to be included, and if an attempt is made to add two identical elements to the same set set, the addition fails, the Adding method returns False, and the new element is not added

Second, HashSet class

HashSet is a typical implementation of set, and most of the time, using a set set is the use of HashSet

(a) HashSet characteristics 1, can not guarantee the order of the elements, the order may be changed
1234567891011121314 public class Test {    public static void main(String[] args) {        String name1 = "孙悟空";        String name2 = "猪八戒";        String name3 = "沙僧";        String name4 = "唐僧";        Set nameSet = new HashSet();        nameSet.add(name1);        nameSet.add(name2);        nameSet.add(name3);        nameSet.add(name4);        System.out.println(nameSet);    }}

650) this.width=650; "border=" 0 "src=" http://s3.51cto.com/wyfs02/M01/80/2E/wKiom1c6csPxCqKxAAAR3zMfxts652.png " data_ue_src= "E:\My knowledge\temp\3e650684-ebcf-44a9-96aa-317da3ed6577.png" >

2, HashSet is not synchronous, if more than one thread accesses a hashset, it must be guaranteed by code to synchronize 3, can store null
1 nameSet.add(null);
(ii) Common methods

650) this.width=650; "border=" 0 "src=" http://s3.51cto.com/wyfs02/M01/80/2B/wKioL1c6c6vCq2abAAA9fcNRJxg399.png " data_ue_src= "E:\My knowledge\temp\3cf79371-1e12-4ce9-8c5e-dc0307479c51.png" >

12345678910 public class TestAdd {    public static void main(String[] args) {        Set set = new HashSet();        set.add(new String("隔壁老王"));        //因为两个对象通过equals方法比较相等,所以添加失败        boolean bool = set.add(new String("隔壁老王"));        System.out.println("bool:"+bool);        System.out.println(set);    }}

650) this.width=650; "border=" 0 "src=" http://s3.51cto.com/wyfs02/M02/80/2E/wKiom1c6csOSXTFWAAActAv3_Yg554.png " data_ue_src= "E:\My knowledge\temp\74c30693-0116-4bf4-96d2-82b261771bf9.png" >

Third, the video address

Http://edu.51cto.com/course/course_id-6028.html





From for notes (Wiz)

Learn from teacher Wang Set (eight): Set interface and HashSet class

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.