Java EE Fundamentals (18)/collections

Source: Internet
Author: User

1. Collection Framework (Map set overview and Features)
    • A:map Interface Overview
      • Check the API to know:
        • Object that maps a key to a value
        • A map cannot contain duplicate keys
        • Each key can be mapped to at most one value
    • B:map interface and collection interface are different
      • Map is a double row, collection is a single row
      • The key of the map is unique, and the collection sub-system set is unique.
      • The data structure value of the map collection is valid for the key, regardless of the value; The data structure of the collection collection is valid for the element
2. Collection framework (Overview of the functions of the map collection)
    • Overview of features of the A:map collection
      • A: Add features
        • V put (K key,v value): Adds an element.
          • If the key is stored for the first time, the element is stored directly, returning NULL
          • If the key does not exist for the first time, replace the previous value with a value and return the previous value
      • B: Delete Feature
        • void Clear (): Removes all key-value pair elements
        • V Remove (Object key): Deletes the key-value pair element according to the key, and returns the value
      • C: Judging function
        • Boolean ContainsKey (Object key): Determines whether the collection contains the specified key
        • Boolean Containsvalue (Object value): Determines whether the collection contains the specified value
        • Boolean IsEmpty (): Determines whether the collection is empty
      • D: Get Features
        • Set<map.entry<k,v>> EntrySet ():
        • V get (Object key): Gets the value based on the key
        • Set KeySet (): Gets the collection of all the keys in the collection
        • Collection values (): Gets a collection of all values in the collection
      • E: Length function
        • int size (): Returns the number of key-value pairs in the collection
3, the collection frame (the map collection of the traversal key to find the value)
    • A: Key to find the value of ideas:
      • Gets the collection of all keys
      • Iterate through the collection of keys and get to each key
      • Find Values by key
    • B: Case Demo

      • The value of the traversal key for the map collection

        HashMap<String, Integer> hm = new HashMap<>();hm.put("张三", 23);hm.put("李四", 24);hm.put("王五", 25);hm.put("赵六", 26);/*Set<String> keySet = hm.keySet();         //获取集合中所有的键Iterator<String> it = keySet.iterator();    //获取迭代器while(it.hasNext()) {                       //判断单列集合中是否有元素    String key = it.next();                 //获取集合中的每一个元素,其实就是双列集合中的键    Integer value = hm.get(key);            //根据键获取值    System.out.println(key + "=" + value);  //打印键值对}*/for(String key : hm.keySet()) {             //增强for循环迭代双列集合第一种方式    System.out.println(key + "=" + hm.get(key));}
4. Set FRAME (the key value of the traversal of the map collection to find the key and value of the object)
    • A: Key value pairs object to find key and value ideas:
      • Gets the collection of all key-value pairs of objects
      • Iterates over a collection of key-value pairs of objects, getting to each key-value pair of objects
      • Find keys and values for objects based on key values
    • B: Case Demo

      • The key value of the traversal of the map collection for the object to find the keys and values

        HashMap<String, Integer> hm = new HashMap<>();hm.put("张三", 23);hm.put("李四", 24);hm.put("王五", 25);hm.put("赵六", 26);/*Set<Map.Entry<String, Integer>> entrySet = hm.entrySet(); //获取所有的键值对象的集合Iterator<Entry<String, Integer>> it = entrySet.iterator();//获取迭代器while(it.hasNext()) {    Entry<String, Integer> en = it.next();              //获取键值对对象    String key = en.getKey();                               //根据键值对对象获取键    Integer value = en.getValue();                          //根据键值对对象获取值    System.out.println(key + "=" + value);}*/for(Entry<String,Integer> en : hm.entrySet()) {    System.out.println(en.getKey() + "=" + en.getValue());}

C: Source Code Analysis

5. Set FRAME (HashMap collection key is a case where the student value is string)
    • A: Case Demo
      • HashMap Collection key is a case where the student value is string
6. Collection Framework (overview and use of Linkedhashmap)
    • A: Case Demo
      • Features of Linkedhashmap
        • The bottom is the linked list is implemented to ensure how to save how to take
7. Set FRAME (TreeMap collection key is a case where the student value is string)
    • A: Case Demo
      • TreeMap Collection key is a case where the student value is string
8. Collection frame (number of occurrences of each character in the statistics string)
    • A: Case Demo

      • Requirement: Count the number of occurrences of each character in the string str = "AAAABBBCCCCCCCCCC"; char[] arr = Str.tochararray (); Converts a string into a character array hashmap<character, integer> HM = new hashmap<> (); To create a two-column collection store key and value

        for(char c : arr) {                                 //遍历字符数组    /*if(!hm.containsKey(c)) {                      //如果不包含这个键        hm.put(c, 1);                               //就将键和值为1添加    }else {                                         //如果包含这个键        hm.put(c, hm.get(c) + 1);                   //就将键和值再加1添加进来    }    //hm.put(c, !hm.containsKey(c) ? 1 : hm.get(c) + 1);    Integer i = !hm.containsKey(c) ? hm.put(c, 1) : hm.put(c, hm.get(c) + 1);            }for (Character key : hm.keySet()) {                 //遍历双列集合    System.out.println(key + "=" + hm.get(key));}
9, set FRAME (set nested hashmap nested HashMap)
    • A: Case Demo
      • Nesting nested hashmap hashmap nested sets
10. Set FRAME (the difference between HashMap and Hashtable)
    • A: Face Test
      • The difference between HashMap and Hashtable
        • Hashtable is a JDK1.0 version, is thread-safe, inefficient, HashMap is JDK1.2 version, is thread insecure, high efficiency
        • Hashtable can not store null keys and null values, HASHMAP can store null keys and Null values
    • B: Case Demo
      • The difference between HashMap and Hashtable
11, the Collection Framework (Collections Tool class overview and common methods explained)
    • A:collections class overview
      • Tool classes for collection operations
    • B:collections member Methods
    • public static <T> void sort(List<T> list)public static <T> int binarySearch(List<?> list,T key)public static <T> T max(Collection<?> coll)public static void reverse(List<?> list)public static void shuffle(List<?> list)
12. Set frame (simulating the landlord shuffle and licensing)
  • A: Case Demo

      • Simulate bucket landlord shuffle and licensing, cards are not sorted

         //buy a pair of poker string[] num = {"A", "2", "3", "4", "5", "6", "7", "8", "9", "Ten", "J", "Q" , "K"}; string[] color = {"Square slice", "Plum Blossom", "Red Peach", "spades"}; arraylist<string> poker = new arraylist<> (); for (string s1:color) {for (string s2:num) {Poker.ad    D (s1.concat (S2)); }}poker.add ("Xiao Wang");p Oker.add ("King");//Shuffle collections.shuffle (poker);//licensing arraylist<string> Gaojin = new Arraylist<> (); arraylist<string> Longwu = new arraylist<> (); arraylist<string> me = new arraylist<> (); arraylist<string> Dipai = new arraylist<> (); for (int i = 0; i < poker.size (); i++) {if (I >= poker.siz    E ()-3) {Dipai.add (Poker.get (i));    }else if (i% 3 = = 0) {Gaojin.add (Poker.get (i));    }else if (i% 3 = = 1) {Longwu.add (Poker.get (i));    }else {me.add (Poker.get (i)); }}//See card System.out.println (Gaojin); System.out.println (LONGWU); System.out.println (Me);
        System.out.println (Dipai);  
13, set FRAME (simulation of the Bucket landlord shuffle and licensing and sorting cards in the schematic diagram)
    • A: Drawing Demo
      • Drawing description Sorting principle
14, set FRAME (simulation of the Bucket landlord shuffle and licensing and sorting cards to achieve the code)
  • A: Case Demo
    • Code implementation for simulating bucket landlords shuffling and licensing and sorting cards
  •     Buy a deck of cards string[] num = {"3", "4", "5", "6", "7", "8", "9", "Ten", "J", "Q", "K", "A", "2"};    string[] color = {"Square slice", "Plum Blossom", "Red Peach", "spades"};          Hashmap<integer, string> HM = new hashmap<> ();            Storage index and poker arraylist<integer> list = new arraylist<> ();                                          Store index int index = 0;                   The starting value of the index for (string s1:num) {for (string s2:color) {hm.put (index, S2.concat (S1));                                Add index and poker to HashMap list.add (index);        Adds an index to the ArrayList collection index++;    }} hm.put (Index, "Xiao Wang");    List.add (index);    index++;    Hm.put (Index, "King");    List.add (index);    Shuffle collections.shuffle (list);    Licensing treeset<integer> Gaojin = new treeset<> ();    treeset<integer> Longwu = new treeset<> ();    treeset<integer> me = new treeset<> ();    treeset<integer> Dipai = new treeset<> (); for (int i = 0; i < List.size ();                         i++) {if (I >= list.size ()-3) {Dipai.add (List.get (i));        Adding an index from the list collection to the TreeSet collection automatically sorts}else if (i% 3 = = 0) {Gaojin.add (List.get (i));        }else if (i% 3 = = 1) {Longwu.add (List.get (i));        }else {me.add (List.get (i));    }}//See card Lookpoker ("Gao Jin", Gaojin, HM);    Lookpoker ("Dragon Five", Longwu, HM);    Lookpoker ("Feng Jia", Me, HM); Lookpoker ("Hole cards", Dipai, HM);} public static void Lookpoker (String name,treeset<integer> Ts,hashmap<integer, string> HM) {System.out.prin    T (name + "The card is:");    for (Integer index:ts) {System.out.print (Hm.get (index) + ""); } System.out.println ();}
15. Set frame (generic fixed bottom boundary)
    • ? Super E

Java EE Fundamentals (18)/collections

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.