input and output
文本界面的输入输出
1. 使用Scanner类
java.util.Scanner
nextInt();
nextDouble();
next();
Scanner scanner = new Scanner(System.in);
int a = scanner.nextInt();
System.out.printf("%d\n", a);
2. use java.io
System.in.read()
System.out.print()
输入一行
BufferedReader in = new BufferedReader(new InputStreamReader( System.in ));
s = in.readLine();
ss = in.readLine();
n = Integer.parseInt( ss );
d = Double.parseDouble( ss );
图形界面的输入输出
文本框 TextField 输入
标签 Label 输出
按钮Button 执行命令
首先需要创建一个Frame
Showing posts with label Useful. Show all posts
Showing posts with label Useful. Show all posts
Tuesday, August 16, 2016
java learning part1
path: 命令所在路径 (javac, java etc.)
classpath: 所要引用的类所在的路径
set path=.;c:\jdk\bin...
set path: 查看当前path
javac -cp libxx.jar xxx.java
java -cp libxx.jar xxx
//临时设一下classpath, 用-cp
使用package时的编译
文件和路径一致
程序中使用package语句
使用import语句
javac -d classes src\edu\uiuc\tds\ui\*.java src\edu\uiuc\tds\util\*.java src\edu\uiuc\tds\*.java
java -cp classes edu.uiuc.tds.PackageTest
运行applet
javac xxx.java
appletViewer xxx.html
applet替代物: Flash, SilverLight, javascript, HTML5
javac 编译
java 运行
javaw 运行图形界面
appletViewer 运行applet程序
jar 打包工具
javadoc 生成文档
javap 查看类信息及反汇编
jar打包
javac A.java
jar cvfm A.jar A.man A.class (c create, v显示详情verbose, m表示生成清单文件, f表示指定文件名
java A.jar
A.man一般命名为MANIFEST.MF
javadoc -d 目录名 xxx.java
/** */
classpath: 所要引用的类所在的路径
set path=.;c:\jdk\bin...
set path: 查看当前path
javac -cp libxx.jar xxx.java
java -cp libxx.jar xxx
//临时设一下classpath, 用-cp
使用package时的编译
文件和路径一致
程序中使用package语句
使用import语句
javac -d classes src\edu\uiuc\tds\ui\*.java src\edu\uiuc\tds\util\*.java src\edu\uiuc\tds\*.java
java -cp classes edu.uiuc.tds.PackageTest
运行applet
javac xxx.java
appletViewer xxx.html
applet替代物: Flash, SilverLight, javascript, HTML5
javac 编译
java 运行
javaw 运行图形界面
appletViewer 运行applet程序
jar 打包工具
javadoc 生成文档
javap 查看类信息及反汇编
jar打包
javac A.java
jar cvfm A.jar A.man A.class (c create, v显示详情verbose, m表示生成清单文件, f表示指定文件名
java A.jar
A.man一般命名为MANIFEST.MF
javadoc -d 目录名 xxx.java
/** */
Sunday, June 5, 2016
Iterator
Iterator, an interface in java.util for iterating sequence of objects.
An Iterator is like a bookmark.
Just as you can have many bookmarks in a book, you can have many Iterators iterating over the same data structure, each one independent of the others.
One Iterator can advance without disturbing other Iterators that are iterating over the same data structure.
The first time next() is called on a newly constructed Iterator, it returns the first item in the sequence. Each subsequent time next() is called, it returns the next item in the sequence.
After the Iterator has returned every item in the sequence, every subsequent call to next() throws an exception and halts with an error message. (I find this annoying; I would prefer an interface in which next() returns null. The Java library designers disagree.)
To help you avoid triggering an exception, hasNext() returns true if the Iterator has more items to return, or false if it has already returned every item in the sequence. It is usually considered good practice to check hasNext() before calling next(). (In the next lecture we'll learn how to catch exceptions; that will give us an alternative way to prevent our program from crashing when next() throws an exception.)
There is usually no way to reset an Iterator back to the beginning of the sequence. Instead, you construct a new Iterator.
Most data structures that support Iterators "implement" another interface in
java.util called "Iterable".
怎么用,一般来说,如果想遍历一个数据结构DS,就call DS.iteratre(); 这个method会constructs and returns DSIterator whose fields are initialized so it is ready to return the first item in DS.
例子:
public interface Iterator {
boolean hasNext();
Object next();
void remove(); // The remove() method is optional.
}
An Iterator is like a bookmark.
Just as you can have many bookmarks in a book, you can have many Iterators iterating over the same data structure, each one independent of the others.
One Iterator can advance without disturbing other Iterators that are iterating over the same data structure.
The first time next() is called on a newly constructed Iterator, it returns the first item in the sequence. Each subsequent time next() is called, it returns the next item in the sequence.
After the Iterator has returned every item in the sequence, every subsequent call to next() throws an exception and halts with an error message. (I find this annoying; I would prefer an interface in which next() returns null. The Java library designers disagree.)
To help you avoid triggering an exception, hasNext() returns true if the Iterator has more items to return, or false if it has already returned every item in the sequence. It is usually considered good practice to check hasNext() before calling next(). (In the next lecture we'll learn how to catch exceptions; that will give us an alternative way to prevent our program from crashing when next() throws an exception.)
There is usually no way to reset an Iterator back to the beginning of the sequence. Instead, you construct a new Iterator.
Most data structures that support Iterators "implement" another interface in
java.util called "Iterable".
public interface Iterable {
Iterator iterator();
}
怎么用,一般来说,如果想遍历一个数据结构DS,就call DS.iteratre(); 这个method会constructs and returns DSIterator whose fields are initialized so it is ready to return the first item in DS.
例子:
/* list/SListIterator.java */
package list;
import java.util.*;
public class SListIterator implements Iterator {
SListNode n;
public SListIterator(SList l) {
n = l.head;
}
public boolean hasNext() {
return n != null;
}
public Object next() {
if (n == null) {
/* We'll learn about throwing exceptions in the next lecture. */
throw new NoSuchElementException(); // In java.util
}
Object i = n.item;
n = n.next;
return i;
}
public void remove() {
/* Doing it the lazy way. Remove this, motherf! */
throw new UnsupportedOperationException("Nice try, bozo."); // In java.lang
}
}
/* list/SList.java */
package list;
import java.util.*;
public class SList implements Iterable {
SListNode head;
int size;
public Iterator iterator() {
return new SListIterator(this);
}
[other methods here]
}
Tuesday, May 17, 2016
Popular web application frameworks
- Ruby on Rails (Ruby)
- Play (Java and Scala)
- ASP.NET MVC (Microsoft)
- Django (Python)
- Sinatra (Ruby) (very light-weight)
- Symfony (PHP)
- Sails.js (Node.js, JavaScript)
Content management systems (CMSs):
WordPress, Drupal, Joomla!
The MVC Design pattern
Model-View-Controller model
- Decouples data (model) and presentation (view)
- A controller handles requests, and coordinates between the model and the view
- More robust applications, easier to maintain
MVC control flow:
1. User interface (view) awaits user input
2. User provides input, e.g., clicks a button
3. Controller handles the input event -> some action (handler or callback) understandable by the model
4. Controller notifies the model, possibly changing the model's state
5. Controller notifies the view (if it needs to be updated). Back to step 1.
- Play (Java and Scala)
- ASP.NET MVC (Microsoft)
- Django (Python)
- Sinatra (Ruby) (very light-weight)
- Symfony (PHP)
- Sails.js (Node.js, JavaScript)
Content management systems (CMSs):
WordPress, Drupal, Joomla!
The MVC Design pattern
Model-View-Controller model
- Decouples data (model) and presentation (view)
- A controller handles requests, and coordinates between the model and the view
- More robust applications, easier to maintain
MVC control flow:
1. User interface (view) awaits user input
2. User provides input, e.g., clicks a button
3. Controller handles the input event -> some action (handler or callback) understandable by the model
4. Controller notifies the model, possibly changing the model's state
5. Controller notifies the view (if it needs to be updated). Back to step 1.
Tuesday, March 22, 2016
Java技能
Java:
1. OO, 常用Java API (collection, multithreads, I/O (NIO), Socket, JDBC, XML, reflection)。
2. JSP and Servlet (Java web). JSTL/EL。监听器,过滤器(Web组件)。MVC架构模式进行java web项目开发。
3. Spring IoC container。 AOP原理。Spring框架管理各种web组件。
4. Hibernate, MyBatis等ORM框架。熟悉他们的核心API。对hibernate的关联映射,继承映射,组件映射,缓存机制,事务管理,性能调优。
5. HTML/CSS/JavaScript web前端。JQuery和Bootstrap。Ajax在web项目中的应用。前端MVC框架(AngularJS)和JavaScript模板引擎(HandleBars)。
6. 常用的relatinal db (MySQL, Oracle)。熟练使用SQL和PL/SQL。
7. Design Pattern。GoF设计模式。UML。 TDD/DDD
8. Apache, NginX, Tomcat, WildFly, Weblogic等web服务器和应用服务器的使用。
9. 产品原型工具Axure。设计建模工具PowerDesigner和Enterprise Architect。Java开发环境Eclipse和IntelliJ。前端开发环境webstorm。版本控制svn和git。项目构建和管理工具Maven和Gradle。
项目:
本系统是X委托Y开发的用于Z的系统,系统包括A, B, C, D等模块。系统使用了java企业级开发的开源框架E以及前端技术F。表示层运用了G架构,使用H作为视图I作为控制器并实现了REST风格的请求;业务逻辑层运用了J模式,并通过K实现事务、日志和安全性等功能,通过L实现缓存服务;持久层使用了M封装CRUD操作,底层使用N实现数据存取。整个项目采用了P开发模型。
E: Spring
F: JQuery库及其插件,或者是Bootstrap框架。 SPA(单页应用)最佳方案是前端MVC框架(AngularJS)和JavaScript模板引擎(HandleBars)。
G: MVC (模型-视图-控制)。Spring MVC, Struts 2, JSF
J:事务脚本
K: AOP技术
L:memcached和Redis
M: 选择很多,Hibernate和MyBatis (一般来说增删改交给hibernate,复杂查询给MyBatis)
N: Relational DB (MySQL, Oracle, SQLServer etc.), 或者NoSQL(MongoDB, MemBase, BigTable),和其他大数据存取方案(GFS, HDFS)。
P: 瀑布模型,快速原型模型,增量模型,螺旋模型,喷泉模型,RAD模型。
版本控制: CVS/SVN/Git
自动构建:Ant/Maven/Ivy/Gradle
持续集成: Hudson/Jenkins
系统架构:
负载均衡服务器:F5, A10
应用服务器:
Http: Apache, NginX
Servlet: Tomcat, Resin
EJB容器: WildFly(JBoss Application Server), GlassFish,Weblogic, WebSphere
数据库服务器: MySQL / Oracle
第三方工具(插件)应用
图表工具: 基于jQuery的图标插件(jQchart, Flot, Charted)
报表工具: Pentaho Reporting, iReport, DynamicReports等
文档处理: POI, iText
工作流引擎:
1. OO, 常用Java API (collection, multithreads, I/O (NIO), Socket, JDBC, XML, reflection)。
2. JSP and Servlet (Java web). JSTL/EL。监听器,过滤器(Web组件)。MVC架构模式进行java web项目开发。
3. Spring IoC container。 AOP原理。Spring框架管理各种web组件。
4. Hibernate, MyBatis等ORM框架。熟悉他们的核心API。对hibernate的关联映射,继承映射,组件映射,缓存机制,事务管理,性能调优。
5. HTML/CSS/JavaScript web前端。JQuery和Bootstrap。Ajax在web项目中的应用。前端MVC框架(AngularJS)和JavaScript模板引擎(HandleBars)。
6. 常用的relatinal db (MySQL, Oracle)。熟练使用SQL和PL/SQL。
7. Design Pattern。GoF设计模式。UML。 TDD/DDD
8. Apache, NginX, Tomcat, WildFly, Weblogic等web服务器和应用服务器的使用。
9. 产品原型工具Axure。设计建模工具PowerDesigner和Enterprise Architect。Java开发环境Eclipse和IntelliJ。前端开发环境webstorm。版本控制svn和git。项目构建和管理工具Maven和Gradle。
项目:
本系统是X委托Y开发的用于Z的系统,系统包括A, B, C, D等模块。系统使用了java企业级开发的开源框架E以及前端技术F。表示层运用了G架构,使用H作为视图I作为控制器并实现了REST风格的请求;业务逻辑层运用了J模式,并通过K实现事务、日志和安全性等功能,通过L实现缓存服务;持久层使用了M封装CRUD操作,底层使用N实现数据存取。整个项目采用了P开发模型。
E: Spring
F: JQuery库及其插件,或者是Bootstrap框架。 SPA(单页应用)最佳方案是前端MVC框架(AngularJS)和JavaScript模板引擎(HandleBars)。
G: MVC (模型-视图-控制)。Spring MVC, Struts 2, JSF
J:事务脚本
K: AOP技术
L:memcached和Redis
M: 选择很多,Hibernate和MyBatis (一般来说增删改交给hibernate,复杂查询给MyBatis)
N: Relational DB (MySQL, Oracle, SQLServer etc.), 或者NoSQL(MongoDB, MemBase, BigTable),和其他大数据存取方案(GFS, HDFS)。
P: 瀑布模型,快速原型模型,增量模型,螺旋模型,喷泉模型,RAD模型。
版本控制: CVS/SVN/Git
自动构建:Ant/Maven/Ivy/Gradle
持续集成: Hudson/Jenkins
系统架构:
负载均衡服务器:F5, A10
应用服务器:
Http: Apache, NginX
Servlet: Tomcat, Resin
EJB容器: WildFly(JBoss Application Server), GlassFish,Weblogic, WebSphere
数据库服务器: MySQL / Oracle
第三方工具(插件)应用
图表工具: 基于jQuery的图标插件(jQchart, Flot, Charted)
报表工具: Pentaho Reporting, iReport, DynamicReports等
文档处理: POI, iText
工作流引擎:
Monday, March 14, 2016
常用java API
HashSet:
https://docs.oracle.com/javase/7/docs/api/java/util/HashSet.html
All Implemented Interfaces:
Serializable, Cloneable, Iterable<E>, Collection<E>, Set<E>
Methods:
boolean add(E e)
boolean contains(O o)
int size()
boolean isEmpty()
用处:
当不需要map的时候(比如只存数字便于查找),可以用HashSet。
https://docs.oracle.com/javase/7/docs/api/java/util/HashSet.html
- java.lang.Object
- java.util.AbstractCollection<E>
- java.util.AbstractSet<E>
- java.util.HashSet<E>
Methods:
boolean add(E e)
boolean contains(O o)
int size()
boolean isEmpty()
用处:
当不需要map的时候(比如只存数字便于查找),可以用HashSet。
Wednesday, February 24, 2016
Bit Manipulation
Bit Manipulation
常用operators:
~ unary bitwise complement operator, inverts a bit pattern
<< signed left shift operator
>> signed right shift operator
>>> unsigned right shift operator, a zero into the leftmost position
& bitwise AND
^ bitwise exclusive OR (必须有且只有一个为真才行)
| bitwise inclusive OR
特性:
(n&(n-1) == 0) true if n is power of 2. (or if n=1)
n^n = 0
可以用来判断两个词有没有重复字符
常见问题:
Leetcode 136 - Single Number
Leetcode 268 - Missing Number
Leetcode 318 - Maximum Product of Word Lengths
常用operators:
~ unary bitwise complement operator, inverts a bit pattern
<< signed left shift operator
>> signed right shift operator
>>> unsigned right shift operator, a zero into the leftmost position
& bitwise AND
^ bitwise exclusive OR (必须有且只有一个为真才行)
| bitwise inclusive OR
特性:
(n&(n-1) == 0) true if n is power of 2. (or if n=1)
n^n = 0
可以用来判断两个词有没有重复字符
常见问题:
Leetcode 136 - Single Number
Leetcode 268 - Missing Number
Leetcode 318 - Maximum Product of Word Lengths
Monday, February 22, 2016
Class and Project - Design and Refactoring
Goal: Design a class to support path finding through a maze
Questions:
1. What do I want to do with the graph?
2. What is the ration of edges to nodes? (adj list or matrix?)
3. How do I need to access to nodes/edges?
4. What properties do nodes and edges need to store?
Objects that make sense, whose data and methods go together.
Interfaces are clean; private data (or data structures) are not exposed.
Easy and fast to do the operations you want to do.
Methods are short and easy to read and understand.
DFS is not short!
Solution: refactor! - restruture code without changing functionality
Each method should have one task!
还可以创建一个MazeEdge class
以及一个Coordinate class来存coordinate。
Questions:
1. What do I want to do with the graph?
2. What is the ration of edges to nodes? (adj list or matrix?)
3. How do I need to access to nodes/edges?
4. What properties do nodes and edges need to store?
Objects that make sense, whose data and methods go together.
Interfaces are clean; private data (or data structures) are not exposed.
Easy and fast to do the operations you want to do.
Methods are short and easy to read and understand.
DFS is not short!
Solution: refactor! - restruture code without changing functionality
Each method should have one task!
还可以创建一个MazeEdge class
以及一个Coordinate class来存coordinate。
Friday, September 18, 2015
经验之谈
来源:http://www.mitbbs.com/article_t/Java/31146699.html
HTML就是一个协议,按照HTML标准写的文本浏览器可以识别,知道怎么显示。你在这个
网页上右键然后选view source,看到的就是HTML
JS就是javascript, 主要功能就是在客户端(浏览器)做事,相对于HTML是静态的,他
是动态的,可以改变客户端的component, 比如标题拉,文本框拉,什么的
CSS就是定义HTML显示的style, 比如字体大小拉,颜色啦,等等等等
JSON主要是数据传输用的,比如javascript和后端服务器传递信息就可以用json
XML和HTML很类似,但是可以自定义tag和结构
MySQL/Oracle都是relational database, 说白了就是存数据的。但是作为relational
database, 他需要满足一些标准。
你现在的阶段很多人都经历过,就是别人把框架布好,你可以往里填code。拉
一段具体的code出来能写,比如二叉树遍个历啥的,但是不知道这些code能干啥用,往
哪儿用,整体布局糊里糊涂。其实是初学者突然接触到大量知识不知从何下手的正常反
应,有点象盲人摸象,扣到了不少具体的知识点,但是对全局缺乏概念。
走出这个阶段有两个方法,一个是计算机科班出身的方法,每个知识点都学的很细,硬
学,几年下来,这些知识点自然就连起来了。这个方法的优点是基础打的扎实,缺点是
太花时间,很可能花了很多时间去搞没啥用的东西。另一个方法就是我常常推荐给转行
的朋友的,先不要纠结于学会具体的某个技术的某个细节,那些其实都很简单,学的快
忘得也快。先花点时间画个框架出来,搞清楚你要学的技术起到了什么作用,确定自己
理解了,再看那些具体的技术就清楚多了。
以你刚才列的那些东西为例子。一个典型的java based web application:
用户面对的是浏览器,你输入一个URL,浏览器从web server (apache, IIS, etc)那里
下载了HTML, CSS, JS。HTML一般是用来做些基本的网页显示,CSS决定他的style,JS
决定一些动态的东西,比如根据用户名来显示不同的内容。除了这些静态的内容,浏览
器也可能通过web server和它背后的app server对话(比如tomcat),进行一些服务器
端的处理(比如运行在tomcat内部的servlets)。这些web application难免要储存一些
数据,他们可以储存在database里(mysql, oracle, etc)。而servlet和database的读
写通过JDBC来完成。
有了这个基本的概念,下次看到JS,脑子里就明白,他是在浏览器上做动态处理的,看
到mysql就知道,他是backend 存储数据的。
等你把握了这个框架,每个知识点都有常见的扩展,比如js有常用的库,jQuery,
ExtJS, database要会最基本的sql query, 比如inner join, outer join, aggregate
function, etc., 你的servlet要在tomcat中运行,需要先以war的形式deploy。具体
的东西越是繁杂,你越要牢牢记住他们在一个大框架里起了什么作用,这样才不会糊里
糊涂,东一榔头西一棒子。
good luck!
HTML就是一个协议,按照HTML标准写的文本浏览器可以识别,知道怎么显示。你在这个
网页上右键然后选view source,看到的就是HTML
JS就是javascript, 主要功能就是在客户端(浏览器)做事,相对于HTML是静态的,他
是动态的,可以改变客户端的component, 比如标题拉,文本框拉,什么的
CSS就是定义HTML显示的style, 比如字体大小拉,颜色啦,等等等等
JSON主要是数据传输用的,比如javascript和后端服务器传递信息就可以用json
XML和HTML很类似,但是可以自定义tag和结构
MySQL/Oracle都是relational database, 说白了就是存数据的。但是作为relational
database, 他需要满足一些标准。
你现在的阶段很多人都经历过,就是别人把框架布好,你可以往里填code。拉
一段具体的code出来能写,比如二叉树遍个历啥的,但是不知道这些code能干啥用,往
哪儿用,整体布局糊里糊涂。其实是初学者突然接触到大量知识不知从何下手的正常反
应,有点象盲人摸象,扣到了不少具体的知识点,但是对全局缺乏概念。
走出这个阶段有两个方法,一个是计算机科班出身的方法,每个知识点都学的很细,硬
学,几年下来,这些知识点自然就连起来了。这个方法的优点是基础打的扎实,缺点是
太花时间,很可能花了很多时间去搞没啥用的东西。另一个方法就是我常常推荐给转行
的朋友的,先不要纠结于学会具体的某个技术的某个细节,那些其实都很简单,学的快
忘得也快。先花点时间画个框架出来,搞清楚你要学的技术起到了什么作用,确定自己
理解了,再看那些具体的技术就清楚多了。
以你刚才列的那些东西为例子。一个典型的java based web application:
用户面对的是浏览器,你输入一个URL,浏览器从web server (apache, IIS, etc)那里
下载了HTML, CSS, JS。HTML一般是用来做些基本的网页显示,CSS决定他的style,JS
决定一些动态的东西,比如根据用户名来显示不同的内容。除了这些静态的内容,浏览
器也可能通过web server和它背后的app server对话(比如tomcat),进行一些服务器
端的处理(比如运行在tomcat内部的servlets)。这些web application难免要储存一些
数据,他们可以储存在database里(mysql, oracle, etc)。而servlet和database的读
写通过JDBC来完成。
有了这个基本的概念,下次看到JS,脑子里就明白,他是在浏览器上做动态处理的,看
到mysql就知道,他是backend 存储数据的。
等你把握了这个框架,每个知识点都有常见的扩展,比如js有常用的库,jQuery,
ExtJS, database要会最基本的sql query, 比如inner join, outer join, aggregate
function, etc., 你的servlet要在tomcat中运行,需要先以war的形式deploy。具体
的东西越是繁杂,你越要牢牢记住他们在一个大框架里起了什么作用,这样才不会糊里
糊涂,东一榔头西一棒子。
good luck!
Jenkins
Setup master and slave machines
https://wiki.jenkins-ci.org/display/JENKINS/Step+by+step+guide+to+set+up+master+and+slave+machines
https://wiki.jenkins-ci.org/display/JENKINS/Step+by+step+guide+to+set+up+master+and+slave+machines
Friday, August 28, 2015
Java Heap Memory Size
Java Memory:
1. Java Heap Size
Place to store objects created by your Java application, this is where Garbage Collection takes place, the memory used by your Java application.
For a heavy Java process, insufficient Heap size will cause the popular java.lang.OutOfMemoryError: Java heap space.
-Xms<size> - Set initial Java heap size
-Xmx<size> - Set maximum Java heap size
$ java -Xms512m -Xmx1024m JavaApp
2. Perm Gen Size
Place to store your loaded class definition and metadata.
If a large code-base project is loaded, the insufficient Perm Gen size will cause the popular Java.Lang.OutOfMemoryError: PermGen.
-XX:PermSize<size> - Set initial PermGen Size.
-XX:MaxPermSize<size> - Set the maximum PermGen Size.
$ java -XX:PermSize=64m -XX:MaxPermSize=128m JavaApp
3. Java Stack Size
Size of a Java thread. If a project has a lot of threads processing, try reduce this stack size to avoid running out of memory.
-Xss = set java thread stack size
Command to find out heap size
$ java -XX:+PrintFlagsFinal -version | grep -iE 'HeapSize|PermSize|ThreadStackSize'
1. Java Heap Size
Place to store objects created by your Java application, this is where Garbage Collection takes place, the memory used by your Java application.
For a heavy Java process, insufficient Heap size will cause the popular java.lang.OutOfMemoryError: Java heap space.
-Xms<size> - Set initial Java heap size
-Xmx<size> - Set maximum Java heap size
$ java -Xms512m -Xmx1024m JavaApp
2. Perm Gen Size
Place to store your loaded class definition and metadata.
If a large code-base project is loaded, the insufficient Perm Gen size will cause the popular Java.Lang.OutOfMemoryError: PermGen.
-XX:PermSize<size> - Set initial PermGen Size.
-XX:MaxPermSize<size> - Set the maximum PermGen Size.
$ java -XX:PermSize=64m -XX:MaxPermSize=128m JavaApp
3. Java Stack Size
Size of a Java thread. If a project has a lot of threads processing, try reduce this stack size to avoid running out of memory.
-Xss = set java thread stack size
Command to find out heap size
$ java -XX:+PrintFlagsFinal -version | grep -iE 'HeapSize|PermSize|ThreadStackSize'
Thursday, August 27, 2015
常见数据结构操作和算法复杂度
Source: http://bigocheatsheet.com/
Legend
Excellent | Good | Fair | Bad | Horrible |
Data Structure Operations
Data Structure Time Complexity Space Complexity
Average Worst Worst
Access Search Insertion Deletion Access Search Insertion Deletion
Array O(1)O(n)O(n)O(n)O(1)O(n)O(n)O(n)O(n)
Stack O(n)O(n)O(1)O(1)O(n)O(n)O(1)O(1)O(n)
Singly-Linked List O(n)O(n)O(1)O(1)O(n)O(n)O(1)O(1)O(n)
Doubly-Linked List O(n)O(n)O(1)O(1)O(n)O(n)O(1)O(1)O(n)
Skip List O(log(n))O(log(n))O(log(n))O(log(n))O(n)O(n)O(n)O(n)O(n log(n))
Hash Table -O(1)O(1)O(1)-O(n)O(n)O(n)O(n)
Binary Search Tree O(log(n))O(log(n))O(log(n))O(log(n))O(n)O(n)O(n)O(n)O(n)
Cartesian Tree -O(log(n))O(log(n))O(log(n))-O(n)O(n)O(n)O(n)
B-Tree O(log(n))O(log(n))O(log(n))O(log(n))O(log(n))O(log(n))O(log(n))O(log(n))O(n)
Red-Black Tree O(log(n))O(log(n))O(log(n))O(log(n))O(log(n))O(log(n))O(log(n))O(log(n))O(n)
Splay Tree -O(log(n))O(log(n))O(log(n))-O(log(n))O(log(n))O(log(n))O(n)
AVL Tree O(log(n))O(log(n))O(log(n))O(log(n))O(log(n))O(log(n))O(log(n))O(log(n))O(n)
| Data Structure | Time Complexity | Space Complexity | |||||||
|---|---|---|---|---|---|---|---|---|---|
| Average | Worst | Worst | |||||||
| Access | Search | Insertion | Deletion | Access | Search | Insertion | Deletion | ||
| Array | O(1) | O(n) | O(n) | O(n) | O(1) | O(n) | O(n) | O(n) | O(n) |
| Stack | O(n) | O(n) | O(1) | O(1) | O(n) | O(n) | O(1) | O(1) | O(n) |
| Singly-Linked List | O(n) | O(n) | O(1) | O(1) | O(n) | O(n) | O(1) | O(1) | O(n) |
| Doubly-Linked List | O(n) | O(n) | O(1) | O(1) | O(n) | O(n) | O(1) | O(1) | O(n) |
| Skip List | O(log(n)) | O(log(n)) | O(log(n)) | O(log(n)) | O(n) | O(n) | O(n) | O(n) | O(n log(n)) |
| Hash Table | - | O(1) | O(1) | O(1) | - | O(n) | O(n) | O(n) | O(n) |
| Binary Search Tree | O(log(n)) | O(log(n)) | O(log(n)) | O(log(n)) | O(n) | O(n) | O(n) | O(n) | O(n) |
| Cartesian Tree | - | O(log(n)) | O(log(n)) | O(log(n)) | - | O(n) | O(n) | O(n) | O(n) |
| B-Tree | O(log(n)) | O(log(n)) | O(log(n)) | O(log(n)) | O(log(n)) | O(log(n)) | O(log(n)) | O(log(n)) | O(n) |
| Red-Black Tree | O(log(n)) | O(log(n)) | O(log(n)) | O(log(n)) | O(log(n)) | O(log(n)) | O(log(n)) | O(log(n)) | O(n) |
| Splay Tree | - | O(log(n)) | O(log(n)) | O(log(n)) | - | O(log(n)) | O(log(n)) | O(log(n)) | O(n) |
| AVL Tree | O(log(n)) | O(log(n)) | O(log(n)) | O(log(n)) | O(log(n)) | O(log(n)) | O(log(n)) | O(log(n)) | O(n) |
Array Sorting Algorithms
Algorithm Time Complexity Space Complexity
Best Average Worst Worst
Quicksort O(n log(n))O(n log(n))O(n^2)O(log(n))
Mergesort O(n log(n))O(n log(n))O(n log(n))O(n)
Timsort O(n)O(n log(n))O(n log(n))O(n)
Heapsort O(n log(n))O(n log(n))O(n log(n))O(1)
Bubble Sort O(n)O(n^2)O(n^2)O(1)
Insertion Sort O(n)O(n^2)O(n^2)O(1)
Selection Sort O(n^2)O(n^2)O(n^2)O(1)
Shell Sort O(n)O((nlog(n))^2)O((nlog(n))^2)O(1)
Bucket Sort O(n+k)O(n+k)O(n^2)O(n)
Radix Sort O(nk)O(nk)O(nk)O(n+k)
| Algorithm | Time Complexity | Space Complexity | ||
|---|---|---|---|---|
| Best | Average | Worst | Worst | |
| Quicksort | O(n log(n)) | O(n log(n)) | O(n^2) | O(log(n)) |
| Mergesort | O(n log(n)) | O(n log(n)) | O(n log(n)) | O(n) |
| Timsort | O(n) | O(n log(n)) | O(n log(n)) | O(n) |
| Heapsort | O(n log(n)) | O(n log(n)) | O(n log(n)) | O(1) |
| Bubble Sort | O(n) | O(n^2) | O(n^2) | O(1) |
| Insertion Sort | O(n) | O(n^2) | O(n^2) | O(1) |
| Selection Sort | O(n^2) | O(n^2) | O(n^2) | O(1) |
| Shell Sort | O(n) | O((nlog(n))^2) | O((nlog(n))^2) | O(1) |
| Bucket Sort | O(n+k) | O(n+k) | O(n^2) | O(n) |
| Radix Sort | O(nk) | O(nk) | O(nk) | O(n+k) |
Graph Operations
Node / Edge Management Storage Add Vertex Add Edge Remove Vertex Remove Edge Query
Adjacency list O(|V|+|E|)O(1)O(1)O(|V| + |E|)O(|E|)O(|V|)
Incidence list O(|V|+|E|)O(1)O(1)O(|E|)O(|E|)O(|E|)
Adjacency matrix O(|V|^2)O(|V|^2)O(1)O(|V|^2)O(1)O(1)
Incidence matrix O(|V| ⋅ |E|)O(|V| ⋅ |E|)O(|V| ⋅ |E|)O(|V| ⋅ |E|)O(|V| ⋅ |E|)O(|E|)
| Node / Edge Management | Storage | Add Vertex | Add Edge | Remove Vertex | Remove Edge | Query |
|---|---|---|---|---|---|---|
| Adjacency list | O(|V|+|E|) | O(1) | O(1) | O(|V| + |E|) | O(|E|) | O(|V|) |
| Incidence list | O(|V|+|E|) | O(1) | O(1) | O(|E|) | O(|E|) | O(|E|) |
| Adjacency matrix | O(|V|^2) | O(|V|^2) | O(1) | O(|V|^2) | O(1) | O(1) |
| Incidence matrix | O(|V| ⋅ |E|) | O(|V| ⋅ |E|) | O(|V| ⋅ |E|) | O(|V| ⋅ |E|) | O(|V| ⋅ |E|) | O(|E|) |
Heap Operations
Type Time Complexity
Heapify Find Max Extract Max Increase Key Insert Delete Merge
Linked List (sorted) -O(1)O(1)O(n)O(n)O(1)O(m+n)
Linked List (unsorted) -O(n)O(n)O(1)O(1)O(1)O(1)
Binary Heap O(n)O(1)O(log(n))O(log(n))O(log(n))O(log(n))O(m+n)
Binomial Heap -O(1)O(log(n))O(log(n))O(1)O(log(n))O(log(n))
Fibonacci Heap -O(1)O(log(n))O(1)O(1)O(log(n))O(1)
| Type | Time Complexity | |||||||
|---|---|---|---|---|---|---|---|---|
| Heapify | Find Max | Extract Max | Increase Key | Insert | Delete | Merge | ||
| Linked List (sorted) | - | O(1) | O(1) | O(n) | O(n) | O(1) | O(m+n) | |
| Linked List (unsorted) | - | O(n) | O(n) | O(1) | O(1) | O(1) | O(1) | |
| Binary Heap | O(n) | O(1) | O(log(n)) | O(log(n)) | O(log(n)) | O(log(n)) | O(m+n) | |
| Binomial Heap | - | O(1) | O(log(n)) | O(log(n)) | O(1) | O(log(n)) | O(log(n)) | |
| Fibonacci Heap | - | O(1) | O(log(n)) | O(1) | O(1) | O(log(n)) | O(1) | |
Big-O Complexity Chart
Subscribe to:
Posts (Atom)

