Common methods for Set interfaces
The set set consists of the implementation classes of the set interface and the set interface. The set interface inherits the collection interface, so it contains all the methods of the collection interface. Common methods in the set interface are as follows:
Because duplicate values are not allowed in the Set collection, you can use the AddAll () method in the Set collection to add the collection collection to the Set collection and remove the duplicate values.
Implementation class for the set interface
To use a set collection, you typically declare it as a set type and then instantiate it through the implementation class of the set interface. The implementation class of the set interface is commonly used with HashSet and TreeSet classes. The syntax format is as follows:
Set<string>collset=new hashset<string> ();
Set<string>collset2=new treeset<string> ();
Because the objects in the set collection are unordered, the result of traversing the set collection is not the same as the order in which the set is inserted.
PackageOrg.hanqi.array;ImportJava.util.*; Public classTest2 { Public Static voidMain (string[] args) {//int i;//int[]m=new int[5];//set<int>d=//HashSystem.out.println ("TreeSet"); Set<string>s=NewHashset<string>(); HashSet<string>s1=NewHashset<string>(); if(S.add ("a") {System.out.println ("Save a Success"); } s.add (A); S.add ("B"); S.add (C); S.add ("D"); S.add (E); S.add ("F"); S.add ("G"); S.addall (NULL); if(!s.add ("a") {System.out.println ("Save a Failed"); } for(String t:s) {System.out.println (t+" "); } if(S.remove ("K") {System.out.println ("Removal succeeded"); } Else{System.out.println ("Removal Failed"); } System.out.println ("Size of S" +s.size ()); Iterator<string>it=S.iterator (); while(It.hasnext ()) {String T=It.next (); if(T.equals ("C") ) {it.remove (); } Else{System.out.println (t); }} System.out.println ("Size of S" +s.size ()); System.out.println ("TreeSet"); Set<string>s2=NewTreeset<string>(); S2.add ("B"); S2.add (A); S2.add (C); S2.add ("G"); S2.add (E); S2.add ("F"); S2.add ("D"); for(String t:s2) {System.out.println (t+" "); } }}TreeSet, HashSet
HashSet can save Null,treeset cannot save null, and TreeMap is called internally.
Common methods for map interfaces
The map interface provides an object that maps keys to values. A map cannot contain duplicate keys, and each key can only map one value at a maximum. The map interface also provides common methods for collections, such as clear (), IsEmpty (), size (), and so on, including the common methods shown
Because the elements in the map collection are stored by key, value, to get the specified key or value value in the collection, you need to obtain the key collection or value collection by the appropriate method, and then traverse the key collection or the value collection to get the specified value.
The implementation class of the Map interface
The common implementation classes of the map interface are HashMap and TreeMap. It is generally recommended to implement a map collection using the HashMap implementation class, because the map collection implemented by the HashMap class is more efficient for adding and deleting mappings. HashMap is implemented based on the map interface of hash table, and the mapping relation of hashmap is quickly searched by hash code, and the map collection implemented by HashMap class is more efficient for adding or deleting mappings, and the mapping relationship in TreeMap is in a certain order. If you want the objects in the Map collection to be in a certain order, you should implement the Map collection using the TreeMap class.
1.HashMap class
This class is based on the implementation of the map interface of the hash table, which provides all the optional mapping operations and allows NULL values and NULL keys, but must guarantee the uniqueness of the keys. HashMap a fast lookup of the mapping relationships within the hash code. This class does not guarantee the order of the mappings, and in particular does not guarantee that the order is constant.
2.TreeMap class
This class not only implements the map interface, but also implements the Java.util.SortedMap interface, so the mapping relationship in the set has a certain order. However, the performance of the TreeMap analogy HashMap class is poor in adding, deleting, and locating mapping relationships. Because the mappings in the map collection implemented by the TreeMap class are arranged according to the key object in a certain order, the key object is not allowed to be null.
You can instantiate the map collection through the HashMap class, and then create an instance of the TreeMap class that completes the same mapping relationship when sequential output is required.
PackageOrg.hanqi.array;ImportJava.util.*; Public classTest3 { Public Static voidMain (string[] args) {Map<string, string>m=NewHashmap<string, string>(); M.put ("0533", "Zibo"); M.put ("0531", "Jinan"); M.put ("0532", "Qingdao"); System.out.println ("Length =" +m.size ()); M.put ("0534", "Qingdao");//values can be repeated, keys cannot be duplicatedM.put (NULL,NULL); //lengthSystem.out.println ("length =" +m.size ()); //removedSystem.out.println ("0533=" +m.get ("0533")); //if key exists if(M.containskey ("0533") {System.out.println ("Key 0533 already exists"); } //value is present if(M.containsvalue ("Zibo") {System.out.println ("Value Zibo exists"); } //Traverse for(String k:m.keyset ()) {System.out.println (k+"="+M.get (k)); } }}HashMap, TreeMap
Collection Class Set\map