java fourteenth chapter of the Map


 First, the characteristics Map collection:
 * 
 * 1.Map collections and Collection collection, it does not matter
 * 
 * 2.Map elements of the collection is in pairs (husband and wife)
 elements * Collection is a collection of independent existence (single relationship)
 * 
 * 3.Map not duplicate set of elements (a key value of the element can not be repeated)
 * 
 * summarized:
 * collection we generally referred to as single collection set
 * Map called dual column set we set
 * Second, the common Map interface implementation class
 * 
 * HashMap <K, V>: the underlying structure is a hash table, (inconsistence of the access sequence) disordered
 * 
 * 
 * a LinkedHashMap <K, V>: + consistent hashing table lists the underlying structure, ordered (sequential access )
 * where <K, V> are two generic, where K and V may be the same or different
 * K represents the type of bond, V represents the type of the value
 * 
 * All of the above implementation classes, to ensure a unique key of (key can not be repeated), then we need to rewrite this type of K hashCode and equals method
 * such as: K is of type String, Integer ... (type java provided), then we do not need to control him
 * K is of type Person, Dog and other custom type then we need to override hashCode and equals method
 * 
 * Third, the common method defined in the Map interface:
 * 
 * 1. Increase:
 * public V PUT (Key K, V value ); // add an element to the Map collection (key-value pairs)
 * returns: it means that the value of the new key-value pairs that covered the old key value pairs
 * If there is no coverage, the return value is null
 * 
 * 2. delete:
 * public V the Remove (Object key); // delete a key-value pair (delete according to the key)
 * 
 * 3. change: actually put method, as long as the time to put a set of keys and a map of the Central Plains and some key duplication, can achieve the purpose of the change
 * 
 * 4. investigation
 * public V get (object key) ; // to find the key according to a key value corresponding
 
 
 public interface the InterfaceA {
    public abstract void SHOWA ();
    
    interface the InterfaceB internal interface {//
        public void showB abstract ();
    }
}

// define a class to implement an interface

class MyClass1 implements InterfaceA{

    @Override
    public void showA() {
        // TODO Auto-generated method stub
        
    }

//    @Override
//    public void showA() {
//        // TODO Auto-generated method stub
//        
//    }
}

class MyClass2 implements InterfaceA.InterfaceB{

    @Override
    public void showB() {
        // TODO Auto-generated method stub
        
    }
    
}


/ *
 * 1. Because Map Collection collections and no collection of inheritance
 * map collection is not an iterator
 *  
 * 2.java provides a way to map the other two iterations of the collection:
 * one way: to find ways to key values:
 * Gets a collection of map.keySet all keys ();
 * Second way: the way of key-value pairs
 * public the Set <Map.Entry <K, V >> entrySet ()
 * /
 
 // create an internal class object
        // OuterClass. OI = new new OuterClass was InnerClass () InnerClass new new ();.
        the Map <String, Integer> = new new the HashMap Map <String, Integer> ();
        map.put ( "Joe Smith", 18 is);
        map.put ( "John Doe ", 28);
        map.put (" Wang Wu ", 38);
        map.put (" Zhao six ", 48);
        // get 1 entry map collection collection
        Set <Map.Entry <String, Integer >> entries = map.entrySet ();
        // 2 by traversing the entries of the Set iterator set
        the Iterator <of Map.Entry <String, Integer >> entries.iterator IT = ();
        //. 3 traverse
        the while (it.hasNext ()) {
            of Map.Entry < String, Integer> = it.next entry ();
            // a property entry has two
            String Key = entry.getKey ();
            Integer value = entry.getValue ();
            System.out.println (Key + "=" + value);
            
        }
        
 
 * exercise 1: use map memory: the key is student number, the value of a student object, student object has attributes (name, age)
 * 
 * 
 * exercise 2: map storage: student key (name, age ) is the student's own home address.
 Map * <Student, String>
 
 public class MapDemo {
    public static void main (String [] args) {
        // define a Map collection of key storage for students (name, age) is the student's own home address.
        Map <Student, String> = new new Map the HashMap <Student, String> ();
        // add data to set
        map.put (new Student ( "Baoqiang", 40), "Beijing outer rings");
        map.put ( new Student ( "Nicholas Tse", 50), "Beijing 180 outside the ring");
        map.put (new Student ( "Ma Rong", 45), "Shanghai Jiao Tong Road");
        map.put (new Student ( "Guo Degang" 55), "Guangzhou Devon community");
        map.put (new new Student ( "Ma Rong", 45), "my home");
        // determine the key repeat not repeat the Map, through hashCode and equals method
        // if I asked a student's name and age, that it is the same as a student
        // loop through the collection entrySet keySet
        // 1 Gets a collection of entry.
        the Set <Map.Entry <student, String >> entries it EnumMap.entrySet = ();
        // 2. iterates over the foreach
        for (of Map.Entry <Student,String> entry : entries) {
            Student key = entry.getKey();
            String value = entry.getValue();
            System.out.println(key+"="+value);
        }
    }
}


/ *
 * A LinkedHashMap: + hash linked list table structure
 * elements are ordered
 
  and Map Collection can be nested
 * 
 * the ArrayList <the ArrayList <String >>
 * 
 * the ArrayList <Map <K, V >>
 * 
 * Map <String , ArrayList <String >>
 * 
 * the Map <String, the Map <String, student >>
 * 
 * For example, our student blog Chi Chuan (JavaEE have the UI Android iOS)
 * the Map <school, students> UI = new HashMap <school, students> ();
 *
 * the Map <school, students> JavaEE = new HashMap <student number, student> ();
 *
 * the Map <school, students> android = new HashMap <student number, student> ();
 *
 *        
 * ArrayList <the Map <student number, student Al = new new .... >>
 * al.Add (the UI);
 * Al.add (JavaEE);
 Al.Add * (Android);
 * 
 * the Map <School name, Map <student number, student = new new .... the Map >>
 * map.put ( "the UI College", the UI);
 * map.put ( "JavaEE Institute ", JavaEE-);
 * map.put (" Andrews Institute ", Android);
 * 
 *         
 * 
 * 
 * 
 * /
public class MapMapDemo {

    static void main public (String [] args) {
        // the TODO Auto-Generated Method Stub
        // Create map. 1 a collection of INSTITUTE UI.
        the Map <String, Student> = new new UIMap the HashMap <String, Student> ();
        UIMap. put ( "dark horse 001", new Student ( "Mary", 18));
        uiMap.put ( "dark horse 003", new Student ( "little America", 19));
        uiMap.put ( "dark horse 004", new Student ( "rain", 20));
        // create a JavaEE 2 College map collection of
        the map <String, Student> eeMap = new new HashMap <String, Student> ();
        eeMap.put ( "dark horse 001", new Student ( "verses", 18 is));
        eeMap.put ( "horse 002", new Student ( "small public",. 19));
        eeMap.put ( "horse 004", new Student ( "tiger", 20));
        // create a 3 android College map collection
        Map <String, Student> anMap = new HashMap <String, Student> ();
        anMap.put ( "horse 002", new Student ( "small K", 18 is));
        anMap.put ( "horse 003", new Student ( "small Q",. 19));
        anMap.put ( "horse 004" , new Student ( "small a", 20));
        . 4 // define a set of all college store names and map object
        map <String, map <String, Student >> map = new HashMap <String, map <String, >> Student ();
        map.put ( "the UI College", UIMap);
        map.put ( "EE Academy", eeMap);
        map.put ( "AN College", anMap);
        // traverse the map is printed each students
        //1.keySet () 2.entrySet ()
        the Set <Map.Entry <String, the Map <String, student >>> entries = EnumMap.entrySet ();
        // 2 entries traversing iterator, foreach.
        iterator <the Map .Entry <String, Map <String, Student>>> it = entries.iterator();
        //标准代码
        while(it.hasNext()){
            Map.Entry<String,Map<String, Student>> entry = it.next();
            String key = entry.getKey();
            Map<String, Student> valueMap = entry.getValue();
            //遍历value这个map集合 keySet entrySet
            Set<Map.Entry<String, Student>>  xyentries = valueMap.entrySet();
            //迭代器 foreach
            Iterator<Map.Entry<String, Student>> xyit = xyentries.iterator();
            //标准代码
            while(xyit.hasNext()){
                Map.Entry<String, Student> xyentry = xyit.next();
                String xykey = xyentry.getKey();
                Student xyValue = xyentry.getValue();
                //System.out.println("学院,学号,学生名字,Students ages ");
                System.out.println(key+"-"+xykey+"-"+xyValue.getName()+"-"+xyValue.getAge());
            }
                
        }
        
        
        
        
    }

}


* Method Collections two collections of tools:
 * 
 * public static void Sort (List <T> List); // in natural order (123 321 abc cba) sorting a set of List
 * 
 * public static void shuffle (List <? > list); // set in order to disrupt
 
 * variable parameters: the type of data here that the variable parameters, but not the number of parameters
 * new characteristics after the JDK1.5
 * 
 * format: public void showNum (int. NUM ..) {
 * 
 *}
 * 
 * Note:
 * when the last method has a plurality of parameters, and wherein there is a variable parameter, the variable parameter to be written to parameter
 *     
 
 public class VariableArgDemo01 {

    static void main public (String [] args) {
        // the TODO Auto-Generated Method Stub
// Show (. 1);
// Show (1,2);
// Show (l, 2,3);
        Show (. 1, 2,3,4,5);
    }
    // definition method with variable parameters
    public static void Show (COUNT int, int NUM ...) {
            // how to obtain the parameters passed in the actual process variable is essentially a parameter array
            System.out.println (num.length);
            for (int I = 0; I <num.length; I ++) {
                System.out.println (NUM [I]);
            }
        }
    }


/ *
 * 1. Define a set of custom rules map
 * 
 * 2 generates a card (ID is stored)
 * 
 * 3. shuffling (set numbers)
 * 
 * 4. Licensing (Reference is made)
 * 
 * the landlord and requires three players cards sorted (sort method)
 * 
 * See card 6. (the number of map values from the set of custom)
 * 
 * 
 * /
public class DouDiZhuDemo {

    static void main public (String [] args) {
        // the TODO Auto-Generated Method Stub
        // map. 1 is a set of defined rules.
        the Map <Integer, String> new new map a LinkedHashMap = <Integer, String> ();
        // 2. define a set of licensing
        the ArrayList <Integer> = new new cards the ArrayList <Integer> ();
        // add the card value card suit +
        String [] nums = { "3 ", "4", "5", "6", ". 7", ". 8", ". 9", "10", "J", "Q", "K", "A", "2"};
        String [] Colors = { "♠", "♥" , "♣", "♦"};
        // splicing a card 
        int ID = 54 is; 1234 //
        for (String NUM: the nums) {
            for (String Color:colors) {
                String card = color+num;
                map.put(id, card);
                cards.add(id);
                ID-;
                //System.out.println(card);
            }
        }
        // Add King Wang
        map.put (2, "small S");
        cards.add (2);
        map.put (. 1, "a large S ");
        cards.add (. 1);
        // shuffling. 3.
        Collections.shuffle (cards);
        .. 4 licensing //
        // define three sets
        ArrayList <Integer> p1 = new ArrayList <Integer> ();
        the ArrayList <Integer> = new new P2 the ArrayList <Integer> ();
        the ArrayList <Integer> = new new P3 the ArrayList <Integer> ();
        // set of cards defined landlord
        the ArrayList <Integer> = new new DP the ArrayList <Integer> ();
        / / 54 traversing card
        for (int i = 0; i <cards.size(); i++) {
            Card cards.get = Integer (I);
            // if the last three are not sent, saved to the card set landlord
            // I 53 is 52 is 51 is
            IF (I> = 51 is) {
                // does not send
                dp.add ( card);
                
            } the else {
                // 0. 1 p1 p1 P2 2 P3. 3
                IF (I. 3% == 0) {
                    // p1 to licensing
                    p1.add (card);
                } the else IF (I ==. 3. 1%) {
                    p2.add (Card);
                } the else {
                    p3.add (Card);
                }
            }
        }
        .. 5 // Sort
        the Collections.sort (P1);
        the Collections.sort (P2);
        The Collections.sort (P3);
        the Collections.sort (DP);
        // read the cards
        lookCards (P1, Map);
        lookCards (P2, Map);
        lookCards (P3, Map);
        lookCards (DP, Map);
    }
    / *
     * See card
     * /
    public static void lookCards (the ArrayList <Integer> cards, the Map <Integer, String> map) {
        // remove the print from the map according to each of a number
        for (Integer ID: cards) {
            String = card map. GET (ID);
            of System.out.print (Card + "");
        }
        System.out.println ();
    }
    

}

/*

Map There are two methods: boolean containsKey: determining whether the specified value included in the collection Map 

HashSet: the underlying hash algorithm.
LinkedHashSet: the bottom of the list is to achieve, but also ensures that the only element, and HashSet same principle.

TreeSet: the bottom is binary tree algorithm.
Generally do not need to sort the elements in the development of storage, all mostly used in the development of HashSet, HashSet is more efficient
TreeSet more during the interview, ask you several ways to sort , and some sort of difference?

 2. TreeSet summary
1). TreeSet features
(1) You can sort elements
       in two sorted.
(2). TreeSet elements to ensure the uniqueness of the basis for
       the realization of Comparable's compareTo method or compare Comparator, if these two methods return value is 0, then TreeSet considered as two elements. Set in accordance with the rules of uniqueness, it can not be added to this collection in a TreeSet repeated elements.
2). Two TreeSet sort
(1). Let the comparative element itself having
       the elements themselves to achieve Comparable interface and implemented to ensure that the inside of the element itself compareTo method with Comparative
(2) so that the container itself has a comparative
       when element Comparative itself does not have or is not provided in the comparative required, to create an instance when the TreeSet, the incoming interface comparator example implementation subclass. This Comparator subclass must implement the compare method.
 
(3) elements may be stored in
[1]. Custom class, and if the two methods override hashCode equals, the container can be stored HashSet
[2]. Custom class, if the implementation of the compareTo Comparable ( ) method, can be stored TreeSet container.
[Summary] If the custom class and only override hashCode equals, but also realized the compareTo, then the elements of this class may be stored in a container HashSet, TreeSet may be stored in a container.

The Map
    HashMap: the underlying hash algorithm for key
        LinkedHashMap: the bottom is the list, for the key.
    
    TreeMap: the bottom is binary tree algorithm for key (in development HashMap more).
Collection
     List (access order, the index can be repeated )
        ArrayList: the underlying implementation is an array, thread-safe, quick to find and modify, add and delete slower.
        LinkedList: the bottom of the list is implemented, thread-safe, add and delete faster, find and modify slower
        Vector: bottom array is implemented, thread-safe, no matter CRUD are slow.
    
    If you find and modify multi-use ArrayList.
    If you add and delete multi-use LinkedList.
    If you have more, with ArrayList
    the Set (unordered access, no index, can not be repeated)
        HashSet
            LinkedHashSet
        TreeSet

        
// Get the number of characters in each string
public class Demo11 {
    public static void main (String [] args) {
        String SBC = "kalsdjfoisoigjsljlfklkadaooaijfa";
        // string into a character array
        char [] chs = sbc .toCharArray ();
        // traverse the array of characters, one by one to compare, but to calculate the number, then use that set of good points Map, Why Map, you can display, 'a', 1 'B'2?
        the Map < character, Integer> = new new Map the HashMap <> ();
        
        // traverse the array of characters
        for (char c: CHS) {
            ? // Map determining there is no key in the c
            Boolean in Flag = map.containsKey (c);
            // there are representatives of map key is c, then the number you need ++
            IF (Flag) {
                // plus 1, according to the key value to find in the original amount
                map.put (c, map.get (c) +1) ;
            } the else {
                // map is a collection of useless inside this character, if that is not the case, then that is the first time.
                Map.put (c, 1);
            }
            
        }
        System.out.println (map);
        
    }
}

Guess you like

Origin www.cnblogs.com/haizai/p/11108864.html
Recommended