[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) ;}}