Read "Streaming Systems" 1&2, Streaming 101 Read "F1, a distributed SQL database that scales" Read "Zanzibar, Google’s Consistent, Global Authorization System" Read "Spanner, Google's Globally-Distributed Database" Read "Designing Data-intensive applications" 12, The Future of Data Systems IOS development with Swift Read "Designing Data-intensive applications" 10&11, Batch and Stream Processing Read "Designing Data-intensive applications" 9, Consistency and Consensus Read "Designing Data-intensive applications" 8, Distributed System Troubles Read "Designing Data-intensive applications" 7, Transactions Read "Designing Data-intensive applications" 6, Partitioning Read "Designing Data-intensive applications" 5, Replication Read "Designing Data-intensive applications" 3&4, Storage, Retrieval, Encoding Read "Designing Data-intensive applications" 1&2, Foundation of Data Systems Three cases of binary search TAMU Operating System 2 Memory Management TAMU Operating System 1 Introduction Overview in cloud computing 2 TAMU Operating System 7 Virtualization TAMU Operating System 6 File System TAMU Operating System 5 I/O and Disk Management TAMU Operating System 4 Synchronization TAMU Operating System 3 Concurrency and Threading TAMU Computer Networks 5 Data Link Layer TAMU Computer Networks 4 Network Layer TAMU Computer Networks 3 Transport Layer TAMU Computer Networks 2 Application Layer TAMU Computer Networks 1 Introduction Overview in distributed systems and cloud computing 1 A well-optimized Union-Find implementation, in Java A heap implementation supporting deletion TAMU Advanced Algorithms 3, Maximum Bandwidth Path (Dijkstra, MST, Linear) TAMU Advanced Algorithms 2, B+ tree and Segment Intersection TAMU Advanced Algorithms 1, BST, 2-3 Tree and Heap TAMU AI, Searching problems Factorization Machine and Field-aware Factorization Machine for CTR prediction TAMU Neural Network 10 Information-Theoretic Models TAMU Neural Network 9 Principal Component Analysis TAMU Neural Network 8 Neurodynamics TAMU Neural Network 7 Self-Organizing Maps TAMU Neural Network 6 Deep Learning Overview TAMU Neural Network 5 Radial-Basis Function Networks TAMU Neural Network 4 Multi-Layer Perceptrons TAMU Neural Network 3 Single-Layer Perceptrons Princeton Algorithms P1W6 Hash Tables & Symbol Table Applications Stanford ML 11 Application Example Photo OCR Stanford ML 10 Large Scale Machine Learning Stanford ML 9 Anomaly Detection and Recommender Systems Stanford ML 8 Clustering & Principal Component Analysis Princeton Algorithms P1W5 Balanced Search Trees TAMU Neural Network 2 Learning Processes TAMU Neural Network 1 Introduction Stanford ML 7 Support Vector Machine Stanford ML 6 Evaluate Algorithms Princeton Algorithms P1W4 Priority Queues and Symbol Tables Stanford ML 5 Neural Networks Learning Princeton Algorithms P1W3 Mergesort and Quicksort Stanford ML 4 Neural Networks Basics Princeton Algorithms P1W2 Stack and Queue, Basic Sorts Stanford ML 3 Classification Problems Stanford ML 2 Multivariate Regression and Normal Equation Princeton Algorithms P1W1 Union and Find Stanford ML 1 Introduction and Parameter Learning

Java Programming 4, Tool Class

2017-05-21

Tool Class

  • java.lang, automatically imported
  • java.util
  • java.io
  • java.awt, javax.swing, GUI
  • java.net
  • java.sql

JDK API DOC

doc

download

more

Object class

  1. equals()
    • == is reference equal
    • equals() is content; one.equals(another)
    • If override equals(), need to override hashCode()
  2. getClass()
    • a final method, cannot be overloaded
  3. toString() return a string of the object
  4. finalize()
  5. thread: notify(), notifyAll(), wait()

Wrapper

Wrapper for primitive data type to ref type, like Integer

  1. Constant: Integer.MAX_VALUE, Double.NaN, Double.POSITIVE_INFINITY
  2. valueOf(String), toString()
    • convert
  3. get value: xxxxValue()
    • intValue() for Integer
  4. value is immutable
  5. Override toString(), equals()
  6. Double: parseDouble(), max, min

Boxing and Unboxing

  • Integer I = 5;
    • I = Integer.valueOf(5);
  • int i = I;
    • i = I.intValue();

Math

  • E
  • PI
  • abs()
  • floor()
  • exp()
  • remainder(double f1, double f2)
  • log()
  • min(float a, float b)
  • random(): 0 - 1
  • rint(): 4.4 to 4; 4.51 to 5
  • sqrt()
  • sin(), cos(), tan(), asin(), acon(), atan()

System

  • System.getProperty(String name)
  • System.getProperties()
  • add property while running java -D varName = varValue MyProg

String

  • String class is immutable
  • StringBuffer, StringBuilder can be mutable

Ops:

  • Return a new instance: concat, replace, replaceAll, substring, toLowerCase, toUpperCase, trim, toString
  • Search: endsWith, startsWith, indexOf, lastIndexOf
  • Compare: equals, equalsIgnoreCase
  • charAt(pos), length
  • format: %1$,8.5f %No$sign...

StringBuffer

Constructor

  • StringBuffer()
  • StringBuffer(int capacity)
  • StringBuffer(String initialString)

Methods

  • append
  • insert
  • reverse
  • setCharAt
  • setLength

String split

  • java.util.StringTokenizer
  • Constructor: StringTokenizer(String str, String delim);

Method:

  • public int countTokens(): no. of splits
  • public boolean hasMoreTokens()
  • public String nextToken()

Date

  1. Calendar
    • get an instance: Calendar.getInstance()
    • get(DAY_OF_MONTH), .getDisplayName(DAY_OF_WEEK)
    • .set, .add(HOUR,1), .roll(MONTH,5)
    • .setTime(date), .getTime()
    • use fields and methods directly: import static java.util.Calendar.*;
  2. Date
    • new Date(), new Date(System.currentTimeMillis())
    • .setTime(long), .getTime()
  3. SimpleDateFormat("yyyy-MM-ddHH:mm:ss")
    • .format, .parse

Time

  • java.time.*
  • java.time.format.*

Class:

  • Instant
  • Clock
  • Duration
  • LocalDateTime
  • LocalDate
  • LocalTime
    • .of, .parse, .format, .plus, .minus

Collection API

3 interface, w sub interfaces:

  • Collection
    • List: remember order, allow duplicates
    • Set: no order, not allow duplicates
  • Map
    • key-value pair

Methods:

  • add(element : Object) : boolean
  • remove(element : Object) : boolean
  • size() : int
  • isEmpty() : boolean
  • contains(element : Object) : boolean
  • iterator() : Iterator

Structure:

  • Collection
    • Set
      • HashSet
      • TreeSet
    • List
      • Vector
        • Stack
      • ArrayList
      • LinkedList
    • Queue
      • LinkedList
      • PriorityQueue
      • ArrayQueue

List

Linear list: List<Type> name = new LinkedList<>();

Class:

  • ArrayList
  • LinkedList

Interface:

public interface List<E> extends Collection<E> {
    E get(int index);
    E set(int index, E element);
    void add(int index, E element);
    E remove(int index);
    int indexOf(Object o);    
}

Iterator

All Collection can generate.

  • Iterator iterator= iterable.iterator();
  • while( iterator.hasNext()) doSomething( iterator.next());
  • equivalent to enhanced for

Stack

  • LIFO
  • public Object push(Object item)
  • public Object pop()
  • public boolean empty()
  • Stack<Type> name = new Stack<>();

Queue

  • FIFO
  • LinkedList
  • Insert: add(e), offer(e) return value
  • Remove: remove(), poll() return value
  • Examine: element(), peek() return value

Set

  • HashSet
  • TreeSet

No duplicates

  • hashCode() not the same
  • If same, use == and equals() having unequal results

Map

Can get sets:

  • entrySet()
  • keySet()
  • values()

Realization:

  • HashMap
  • TreeMap

Ops:

  • Map<String,String> map = new TreeMap<String,String>();
  • .put(key, value)
  • .get(key)
  • entry.getKey()
  • entry.getValue()
  • .keySet()
  • .entrySet().iterator()

Sort

  • Arrays class can be used to sort and search array
    • Arrays.asList(10, 7, 6, 5, 9) return a list object
    • sort(), binarySearch(); do sort() first before binarySearch()
      • Arrays.<String>sort(s);
  • Collection
    • Colletions.sort( school, new ComparatorName())
    • or Colletions.sort( school, (p1,p2)-> p1.age-p2.age)

Generic

Must be ref type

Class

import java.util.*;

class GenericTreeClass {
	public static void main(String[] args){
		TNode<String> t = new TNode<>("Roo");
		t.add("Left"); t.add("Middle"); t.add("Right");
		t.getChild(0).add("aaa");
		t.getChild(0).add("bbb");
		t.traverse();
	}
}

class TNode<T>
{
  private T value;
  private ArrayList<TNode<T>> children = new ArrayList<>();

  TNode(T v) { this.value = v; }
  public T getValue() { return this.value; }
  public void add(T v) {
    TNode<T> child = new TNode<>(v);
    this.children.add(child);
  }
  public TNode<T> getChild(int i) {
    if ((i < 0) || (i > this.children.size())) return null;
    return (TNode<T>)this.children.get(i);
  }

  public void traverse() {
    System.out.println(this.value);
    for (TNode child : this.children)
      child.traverse();
  }
}

Method

import java.util.*;

class GenericMethod {
	public static void main(String[] args){
		Date date = BeanUtil.<Date>getInstance("java.util.Date");
		System.out.println(date);
	}
}

class BeanUtil{
	public static <T> T getInstance( String clzName ){
		try
		{
			Class c = Class.forName(clzName);
			return (T) c.newInstance();
		}
		catch (ClassNotFoundException ex){}
		catch (InstantiationException ex){}
		catch (IllegalAccessException ex){}
		return null;
	}
}

Limit on type

  • ?: all: reverse(List<?> list)
  • extends: addAll(Collection<? extends E> col)
  • super: fill(List<? super T> list, T obj), fruit for the list, can have apple.. in it

Exhaust algo, brute force

for(;;){ if(); }

Iterative algo

while() { x = f(x); }

Recursive algo

f(n){f(n-1);}

Back-track

x++; if(...) x--;


Creative Commons License
Melon blog is created by melonskin. This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License.
© 2016-2019. All rights reserved by melonskin. Powered by Jekyll.