Scott の 博客 Scott の 博客
首页
  • Data Structure and Algorithm
  • Java
  • 面试
  • Drafts
  • C++
  • 前端文章

    • JavaScript
  • 学习笔记

    • 《JavaScript教程》
    • 《JavaScript高级程序设计》
    • 《Vue》
    • 《React》
    • 《TypeScript 从零实现 axios》
    • 《Git》
    • TypeScript
    • JS设计模式总结
  • HTML
  • CSS
  • 技术文档
  • GitHub技巧
  • Nodejs
  • 博客搭建
  • 学习
  • 面试
  • 心情杂货
  • 实用技巧
  • 友情链接
关于
收藏
  • 分类
  • 标签
  • 归档
GitHub (opens new window)

Scott

恋爱中
首页
  • Data Structure and Algorithm
  • Java
  • 面试
  • Drafts
  • C++
  • 前端文章

    • JavaScript
  • 学习笔记

    • 《JavaScript教程》
    • 《JavaScript高级程序设计》
    • 《Vue》
    • 《React》
    • 《TypeScript 从零实现 axios》
    • 《Git》
    • TypeScript
    • JS设计模式总结
  • HTML
  • CSS
  • 技术文档
  • GitHub技巧
  • Nodejs
  • 博客搭建
  • 学习
  • 面试
  • 心情杂货
  • 实用技巧
  • 友情链接
关于
收藏
  • 分类
  • 标签
  • 归档
GitHub (opens new window)
  • Data Structure and Algorithm

  • Java

    • Java基础知识&面试题总结
    • Java
    • Control Flow
    • Clean Coding
    • Debugging and Deployment
    • Untitled
    • Refactor towards OOSD
    • Inheritance
    • Untitled
    • Exceptions
    • Generics
    • Collections
      • Overview of Collections Framework
      • The Need for Iterables
      • The Iterable Interface
      • The Iterator Interface
      • The Collection Interface
      • The List Interface
      • The Comparable Interface
      • The Comparator Interface
      • The Queue Interface
      • The Set Interface
      • The Map Interface
    • Lambda-Expression
    • Streams
    • Concurrency and Multi-threading
    • The Executive Framework
    • 4
    • 1
  • c++

  • 面试

  • Bilibili_Java

  • Python

  • All kinds of Drafts

  • High Integrity Information System

  • 左神算法课

  • 个人笔记
  • Java
Scott
2022-06-14
目录

Collections

  1. Overview of Collections Framework
  2. The Need for Iterables
  3. The Iterable Interface
  4. The Iterator Interface
  5. The Collection Interface
  6. The List Interface
  7. The Comparable Interface
  8. The Comparator Interface
  9. The Queue Interface
  10. The Set Interface
  11. The Map Interface

# Overview of Collections Framework

image-20220120104757559

  • Green boxes are interfaces
  • Blue boxes are classes that implement those interfaces
image-20220120174314776
  • Collection: represent a collection of objects to support (add/remove/get) operations
    • In a collection we cannot access elements by their index
  • List: extends the collection interface and gives us that additional functionality
  • Queue: represent a queue of objects
    • Process jobs in the order we receive them
  • Set: represents a unique list of values
  • Map: represents a set of key-value pairs

# The Need for Iterables

public class Main {
    public static void main(String[] args) {
        var list = new GenerticList<String>();
        list.add("a");
        list.add("b");
        for (var item : list.items) 
            System.out.println(item);
        list.items[0] = "a";
        System.out.println(list.item.length);
    }
}

public class GenericList<T> implements Iterable<T> {
    // make it public
  	public T[] items = (T[]) new Object[10];
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
  • However, by making items public, the main class and GenericList classes has unnecessary coupling
  • This makes future maintenance and upgrades very hard
  • For example, if we replace T[] with ArrayList<T>, then the code in line 8 and nine will have compilation error

# The Iterable Interface

  • This interface represents an object that is iterable and can be used in a for each statement
  • We can iterate or loop over it without knowing anything about its implementation detail
    • Don't care about its data structure
public class Main {
    public static void main(String[] args) {
        var list = new GenerticList<String>();
		var iterator = list.iterator();
        while (iterator.hasNext()) {
   			var current = iterator.next(); // return current object 
            System.out.println(current);
        }    
        
        // or simply 
        for (String item : list)
            System.out.println(item);
        // because the `for-each loop` is a syntactical sugar 
        //  over the iterator object
        // Java will convert our code to use an iterator object 
    }
}

public class GenericList<T> implements Iteraable<T> {
    @Override 
    public Iterator<T> iterator() {
        
    }
}
    
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

# The Iterator Interface

  • An iterator is an object that we use to iterate over an iterable
public class GenericList<T> implements Iteraable<T> {
    @Override 
    public Iterator<T> iterator() {
        return new ListIterator(this);
    }
    
    privat class ListIterator implements Iterator<T> {
        private GenericList<T> list;
    	private int index;

        // Constructor
        public ListIterator(GenericList<T> list) {
      		this.list = list;
    }

   		@Override
    	public boolean hasNext() {
      		return (index < list.count);
    	}

    	@Override
    	public T next() {
      		return list.items[index++];
    	}
    }
}

public class Main {
    public static void main(String[] args) {
        var list = new GenerticList<String>();
        list.add("a");
        list.add("b");
        for (String item : list)
            System.out.println(item);
    }
}



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
  • We need to implement this iterator as a private nested class inside this generator list class

# The Collection Interface

  • This interface extends the iterable interface and add additional functionality
    • Therefore, elements in the collection are iterable
  • It represents an object that can act like a container or a collection of objects
  • The operations that the collection interface declares
    • Add/remove an object
    • Check for the existence of an object
    • ...
// it is a generic interface 
interface Collection<E>
1
2
  • E stands for element
    • This is a convention for collection semantic, because the collection can have multiple elements
public class CollectionDemo {
	public static void show() {
    	Collection<String> collection = new ArrayList<>();
    	collection.add("a");
    	collection.add("b");
    	collection.add("c");

    	// Add multiple items in one go
    	Collections.addAll(collection, "a", "b", "c");

    	var size = collection.size();

    	collection.remove("a");
        
    	var containsA = collection.contains("a");

    	collection.clear();
        
    	var isEmpty = collection.isEmpty();

        // default object array
    	Object[] objectArray = collection.toArray();
        
        // convert to string array 
    	String[] stringArray = collection.toArray(new String[0]);

    	Collection<String> other = new ArrayList<>();
    	other.addAll(collection);
    	System.out.println(collection == other); // false, two different object in memory
    	System.out.println(collection.equals(other)); // true
  	}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32

# The List Interface

  • It allows us to work with an ordered collection and access objects using the index
  • One of the class that will be using most of the time is the ArrayList class, which is the implementation of a dynamic array
    • It uses an array to store objects
    • If the array gets full, it will automatically resize the array
  • LinkedList
    • Based on the linked list data structure
public class ListDemo {
  	public static void show() {
    	List<String> list = new ArrayList<>();
    	list.add("a");
    	list.add("b");
    	list.add("c");

    	// Add an item at a given index
    	list.add(0, "!");

        // We can add multiple items in one go
        Collections.addAll(list, "a", "b", "c");

        // get and set
        var first = list.get(0);
        list.set(0, "!!");

        // delete
        list.remove(0);

        // search 
        var index = list.indexOf("a");
        var lastIndex = list.lastIndexOf("a");

        // section | return a new list
        System.out.println(list.subList(0, 2));
    }
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29

# The Comparable Interface

  • This is not flexible because it only compares objects by their names
public class Main {
    public static void main(String[] args) {
        List<Customer> customers = new ArrayList<>();
        customers.add(new Customer("b"));
        customers.add(new Customer("a"));
        customers.add(new Customer("c"));
        
        Collections.sort(customers);
    }
}

public class Customer implements Comparable<Customer> {
	private String name;
    private String email;

    public Customer(String name, String email) {
        this.name = name;
        this.email = email;
    }

    @Override
    public int compareTo(Customer other) {
        return name.compareTo(other.name);
    }

    @Override
    public String toString() {
        return name;
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30

# The Comparator Interface

public class Main {
    public static void main(String[] args) {
        List<Customer> customers = new ArrayList<>();
        customers.add(new Customer("b", "e3"));
        customers.add(new Customer("a", "e2"));
        customers.add(new Customer("c", "e1"));
        
        Collections.sort(customers, new EmailComparator()); // specify the comparator
    }
}

// use a new comparator to change comparison logic more easily 
public class EmailComparator implements Comparator<Customer> {
    @Override
    public int compare(Customer o1, Customer o2) {
        return o1.getEmail().compareTo(o2.getEmail());
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

# The Queue Interface

  • It provides additional operations for working with a queue of objects

  • It is used in situations where we have a resources that can be shared amongst many consumers

  • e.g.

    • Printer of the office
    • It takes the job one by one and process them
  • ArrayDeque

    • Double ended queue
    • Two ends
    • Items can enter the tube from either end
  • PriorityQueue

    • Each item gets an priority
    • Priority determines the position of the item
    • Items with a higher priority are moved to the front
public class QueueDemo {
    public static void show() {
        Queue<String> queue = new ArrayDeque<>();
        queue.add("c");
        queue.add("a");
        queue.add("b");
        queue.offer("d"); // diff: most of the time the same
        // for ArrayDeque with limited size, `add` throws an exeception, 
        // 	`offer` returns false

        // move the element in the front and return it
        var front = queue.remove(); // the same as queue.poll()
        // poll() will return null if the queue is empty

        front = queue.element(); // the same as queue.peek()

        System.out.println(front);
        System.out.println(queue);

        // We have alternative methods that don't
        // throw an exception:

        // offer() similar to add()
        // poll() similar to remove()
        // peek() similar to element()
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27

# The Set Interface

  • It represents a collection without duplications
  • It is a great way of storing a list of unique values
  • The Set interface does not guarantee the order of elements, it only guarantee the uniqueness
public class SetDemo {
    public static void show() {
        Set<String> set1 =
            new HashSet<>(Arrays.asList("a", "b", "c"));

        Set<String> set2 =
            new HashSet<>(Arrays.asList("b", "c", "d"));

        // Union
        set1.addAll(set2);

        // Intersection
        set1.retainAll(set2);

        // Difference
        set1.removeAll(set2);
        
        // remove duplicates in the collection 
        Collection<String> collection = new ArrayList<>();
        Collections.addAll(collection, "a", "b", "c", "c");
        Set<String> set = new HashSet<>(collection);
        System.out.println(set); // [a, b, c]
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

# The Map Interface

public class MapDemo {
    public static void show() {
        var c1 = new Customer("a", "e1");
        var c2 = new Customer("b", "e2");

        Map<String, Customer> map = new HashMap<>();
        map.put(c1.getEmail(), c1);
        map.put(c2.getEmail(), c2);

        var exists = map.containsKey("e1");

        var unknown = new Customer("Unknown", "");
        var customer = map.get("e1");
        customer = map.getOrDefault("e1", unknown);

        map.replace("e1", new Customer("a++", "e1"));

        for (var key : map.keySet())
            System.out.println(key);

        for (var value : map.values())
            System.out.println(value);

        for (var entry : map.entrySet())
            System.out.println(entry); // print 'key=value'
        
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
上次更新: 2022/12/04, 16:55:22
Generics
Lambda-Expression

← Generics Lambda-Expression→

最近更新
01
day01-Java基础语法
08-31
02
1
08-29
03
路线
08-01
更多文章>
Theme by Vdoing | Copyright © 2019-2022 Evan Xu | MIT License
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式
×