[152 days] Shangxue High Cream Java 300 video essence notes (119-120)

Keywords: Java

Be careful (2017.7.7)

  1. Teacher Pei Xin talks so fast!What a high density of content!How difficult it is to digest!

  2. I find it impossible to achieve 339 quantities by focusing on quality and not deceiving myself. Take your time, first adjust to 333, then add 1 lesson every 3 days.

  3. I found that the best way to deal with a topic that I don't know how to start is to start with the simplest method, which starts with the end and splits up constantly. In the last seven cycles, I have trained this simple thinking model.

Set 119: Custom Implementation Iterator, Deep Iterator, Iterator Principles, Object-Oriented Implementation

Why is an iterator an internal class?

A: Save memory and improve efficiency.As shown in the example below, instead of using internal classes, each iteration requires a re-creation of a container object, unlike internal classes, which can save memory by simply re-invoking the iterator's method to create a new internal class object.

Example 1: Cases where internal classes are not used

package test119;

import java.util.Iterator;

public class MyArrayList2 {
    
    private String[] elem = {"a","b","c"};
    private int cursor = -1;
    private int size = elem.length;
    
    
    public boolean hasNext(){
        return cursor!=size-1;
    }
    
    public String next(){
        cursor++;
        return elem[cursor];
    }
    
    public void remove(){
        //Write implementation here
    }

    
    public static void main(String[] args){
        //Traverse the first pass to create a container object
        MyArrayList2 list = new MyArrayList2();
        while(list.hasNext()){
            String str = list.next();
            System.out.println(str);
        }
        //Traverse the second pass to create a container object
        MyArrayList2 list2 = new MyArrayList2();
        while(list2.hasNext()){
            String str = list2.next();
            System.out.println(str);
        }
        
    }
}

Example 2: Cases where internal classes are used

package test119;

import java.util.Iterator;

public class MyArrayList2 {
    
    private String[] elem = {"a","b","c"};
    

    private class M implements Iterator<String>{
        private int cursor = -1;
        private int size = elem.length;
        
        public boolean hasNext(){
            return cursor!=size-1;
        }
        
        public String next(){
            cursor++;
            return elem[cursor];
        }
        
        public void remove(){
            //Write implementation here
        }
    }
    
    public Iterator<String> iterator(){
        return new M();
    }
    
    public static void main(String[] args){
        //Traverse the first pass to create an iterator
        MyArrayList2 list = new MyArrayList2();
        Iterator<String> itr = list.iterator();
        while(itr.hasNext()){
            String str = itr.next();
            System.out.println(str);
        }
        //Walk through the second pass to create an iterator.
        Iterator<String> itr2 = list.iterator();
        while(itr2.hasNext()){
            String str = itr2.next();
            System.out.println(str);
        }
        
    }
}

Example 3: Further improvement: Rewrite to anonymous inner class

package test119;

import java.util.Iterator;

public class MyArrayList3 {
    
    private String[] elem = {"a","b","c"};
        
    public Iterator<String> iterator(){
        return new Iterator<String>(){ //Since the above example M was used only once, anonymous internal classes are used directly here
            private int cursor = -1;
            private int size = elem.length;
            
            public boolean hasNext(){
                return cursor!=size-1;
            }
            
            public String next(){
                cursor++;
                return elem[cursor];
            }
            
            public void remove(){
                //Write implementation here
            }
        };
    }
    
    public static void main(String[] args){
        MyArrayList3 list = new MyArrayList3();
        Iterator<String> itr = list.iterator();
        while(itr.hasNext()){
            String str = itr.next();
            System.out.println(str);
        }    
    }
}

Here is a supplement to Anonymous Internal Classes: Just look at the first part

Example 4: Further improvement: using enhanced for cycle

  1. The java.lang.Iterable<T>interface must be implemented to override the iterator method using an enhanced for loop.

  2. Note the distinction between Iterable<T>interfaces and Iterator<T>interfaces

  3. Important methods within the Iterable <T>interface: Iterator <T> iterator ();

  4. Important methods within the Iterator <E>interface:

    1. boolean hasNext();

    2. E next();

    3. default void remove();

package test119;

import java.util.Iterator;

public class MyArrayList4 implements Iterable<String>{
    
    private String[] elem = {"a","b","c"};
        
    public Iterator<String> iterator(){
        return new Iterator<String>(){ //Since the above example M was used only once, anonymous internal classes are used directly here
            private int cursor = -1;
            private int size = elem.length;
            
            public boolean hasNext(){
                return cursor!=size-1;
            }
            
            public String next(){
                cursor++;
                return elem[cursor];
            }
            
            public void remove(){
                //Write implementation here
            }
        };
    }
    
    public static void main(String[] args){
        MyArrayList4 list = new MyArrayList4();
        Iterator<String> itr = list.iterator();
        for(String temp:list){
            System.out.println(temp);
        }
    }
}

Collection 120: HashMap, Classic Storage Sorting Ideas

package test120;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/**
 * @author wangtao
 * Two sorting ideas for storing data in a Map
 */
public class Test {
    public static void main(String[] args){
        String test = "this is a cat and this is a dog";
        String[] arr = test.split(" ");
        
        Map<String,Letter> letters = new HashMap<String,Letter>();
        //Idea 1: Create containers for each key before loading data
//        for(String temp:arr){
//            if(!letters.containsKey(temp)){
//                letters.put(temp, new Letter());
//            }
//            Letter col = letters.get(temp);
//            col.setCount(col.getCount()+1);
//        }
        
        //Idea two: Meet a key, check if there is a container, if there is one, load the data directly; if not, create a container to load the data again.
//        for(String temp:arr){
//            if(!letters.containsKey(temp)){
//                Letter col = new Letter();
//                col.setCount(1);
//                letters.put(temp,col);
//            } else {
//                Letter col = letters.get(temp);
//                col.setCount(col.getCount()+1);
//            }
//        }
        
        //Another way to write Idea Two
        Letter col = null;
        for(String temp:arr){
            if(null==(col = letters.get(temp))){
                col = new Letter();
                col.setCount(1);
                letters.put(temp, col);
            }else{
                col.setCount(col.getCount()+1);
            }
        }
        
        
        
        //Traverse a Map that already has data stored
        Set<String> a = new HashSet<String>();
        a = letters.keySet();
        for(Iterator<String> itr = a.iterator();itr.hasNext();){
            String key = (String)itr.next();
            System.out.println("Word:"+key+"  frequency:"+letters.get(key).getCount());
        }
        
    }
}

Posted by mkubota on Sat, 15 Jun 2019 12:00:45 -0700