[Java class set] _ set interface notes (instance test)

Source: Internet
Author: User

[Java class set] _ set interface notes

Objectives of this chapter:

Understanding the relationship between the set interface and the collection Interface

Common subclasses of the Set interface: treeset and hashset

Collection has now learned two types of interfaces: List and queue

Set interface definition:

The Set interface is also a sub-interface of the collection interface. However, unlike the collection or list interface, duplicate elements cannot be added to the set interface.

Set interface definition:

Public interface set <E> extends collection <E>

The main methods of the Set interface are the same as those of the collection interface.

The instance of the Set interface cannot perform bidirectional output like the list interface.

Common subclass of the Set Interface

Hash Storage: hashset

Ordered storage: treeset

Collection does not support bidirectional output, because the get () method is not provided, but the set and collection interfaces are defined in the same way. Therefore, bidirectional output is not supported.

Hashset: stores content in a hash mode, which has no order.

Import Java. util. hashset; import Java. util. set; public class hashsetdemo01 {public static void main (string [] ARGs) {set <string> allset = new hashset <string> (); allset. add ("A"); allset. add ("B"); allset. add ("C"); allset. add ("D"); allset. add ("e"); allset. add ("F"); allset. add ("G"); system. out. println (allset );}}

The content insertion order of the list interface is the order in which it is stored.

If you want to automatically sort all the content, you can use the second subclass of Set-treeset

 Import Java. util. treeset; import Java. util. set; public class treesetdemo01 {public static void main (string [] ARGs) {set 
  
    allset = new treeset 
   
     (); allset. add ("A"); allset. add ("B"); allset. add ("C"); allset. add ("D"); allset. add ("e"); allset. add ("F"); allset. add ("G"); system. out. println (allset) ;}}
   
  

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.