發表文章

Java(物件導向) 與 SQL 的概念衝突

圖片
Java(物件導向) 與 SQL 的概念衝突 The paradigm mismatch : Java : public class User { String username; String address; Set billingDetails; // Accessor methods (getter/setter), business methods, etc. } public class BillingDetails { String account; String bankname; User user; // Accessor methods (getter/setter), business methods, etc. } SQL : create table USERS ( USERNAME varchar(15) not null primary key, ADDRESS varchar(255) not null ); create table BILLINGDETAILS ( ACCOUNT varchar(15) not null primary key, BANKNAME varchar(255) not null, USERNAME varchar(15) not null, foreign key (USERNAME) references USERS ); 1. The problem of granularity (SQL 無法自訂 data type) The most glaringly obvious problem with the current implementation is that you’ve designed an address as a simple String value. In most systems, it’s necessary to store street, city, state, country, and ZIP code information se

Binary Search Tree - 2

圖片
Binary Search Tree - 2 Traversing the Tree Traversing a tree means visiting each node in a specified order . There are three simple ways to traverse a tree. preorder inorder postorder Inorder Traversal : The method needs to do only three things : Call itself to traverse the node’s left subtree . Visit the node. Call itself to traverse the node’s right subtree . Implement inorder traversal by recursion . //pseudo code public static void main(String args[]) { inOrder(root); } private void inOrder(Node localRoot) { if(localRoot != null) { inOrder(localRoot.leftChild); System.out.print(localRoot.iData + “ “); inOrder(localRoot.rightChild); } } Example : Preorder Traversal : The method needs to do only three things : Visit the node. Call itself to traverse the node’s left subtree . Call itself to traverse the node’s right subtree

Java Concurrency

圖片
Java Concurrency 如何使用 Thread : 實作 Runnable Interface : public class HelloRunnable implements Runnable { @Override public void run() { System.out.println("Hello from a thread!"); } public static void main(String args[]) { (new Thread(new HelloRunnable())).start(); } } 繼承 Thread Class : public class HelloThread extends Thread { @Override public void run() { System.out.println("Hello from a thread!"); } public static void main(String args[]) { (new HelloThread()).start(); } } Thread 的運作方式 : Java 程式為從 main 方法開始執行,並依照程式碼的順序由上往下執行。 而 main 方法事實上就是先啟用一個 Thread 並在記憶體中規劃一個 Stack,將程式碼放入其中,把需要呼叫的 Method 依序放入 Stack 中。 當我們在 main 方法中 new 一個新的 Thread 並呼叫 start() 時,這個 Thread 也會在記憶體中規劃一個新的 Stack,並將 run() 的程式碼放入 Stack 中。 Thread 的執行順序是由 CPU 控制的,故每次執行順序都不盡相同。 不同 Thread 使用相同的 Object : 在 Java 中,只會有一個 Heap 區,用來儲存所有被 instance 的 object,不同的 Thread 可以使

Binary Search Tree - 1

圖片
Binary Search Tree Binary Search Tree What Is a Tree ? A tree consists of nodes connected by edges. The lines (edges) between the nodes represent the way the nodes are related. Typically, there is one node in the top row of a tree, with lines connecting to more nodes on the second row, even more on the third, and so on. Thus, trees are small on the top and large on the bottom. Tree Terminology Path : Think of someone walking from node to node along the edges that connect them. Root : The node at the top of the tree is called the root. There is only one root in a tree. Parent : Any node (except the root) has exactly one edge running upward to another node. Child : Any node may have one or more lines running downward to other nodes. Leaf : A node that has no children is called a leaf node or simply a leaf. Subtree : Any node may be considered to be the root of a subtree. Visiting : A node is visited when prog

Linked List

圖片
Linked List Linked List In a linked list, each data item is embedded in a link. A link is an object of a class called something like Link. Each Link object contains a reference (usually called next) to the next link in the list. What is references type ? In Java a Link object doesn’t really contain another Link object. The next field of type Link is only a reference to another link , not an object. A reference is a number that refers to an object. (It’s the object’s address in the computer’s memory.) class Link { public int iData; // data item (key) public double dData; // data item public Link next; // next link in list // constructor public Link(int id, double dd) { iData = id; // initialize data dData = dd; // (‘next’ is automatically set to null) } // display ourself public void displayLink() { System.out.print(“{“ + iData + “, “ + dData + “} “); } } class LinkList { private Lin

資料庫 - Indexing 介紹

圖片
DataBase - Indexing Indexing 介紹 Index 可以想成是一種儲存在資料庫(DataBase)裡的資料結構(Data Structure),Index會對映一個或多個欄位上(field or column),並存一個相對應欄位值的指標來指向欄位值<data value, data rid>。 Primary index : 代表被設定index的欄位本身就是表單的Primary Key,由於Primary Key本身就會有一個預設的index(由資料庫自動生成),所以此時的index會直接帶入原本的值。 Secondary index : 為一般的index其值須自行設定。 用途 : 加快搜尋特定資料(ex : where age = 10; where age > 18 and age < 30)的速度。

Introduction to Redis

圖片
Redis Introduction to Redis is a DataBase No-Sql DataBase work on memory store data by Key - Value pair very quick for get data