显示标签为“java”的博文。显示所有博文
显示标签为“java”的博文。显示所有博文

3/18/2009

Collections Framework (1)


Collection — the root of the collection hierarchy. A collection represents a group of objects known as its elements. The Collection interface is the least common denominator that all collections implement and is used to pass collections around and to manipulate them when maximum generality is desired.


Set — a collection that cannot contain duplicate elements.
List — an ordered collection (sometimes called a sequence).

Lists can contain duplicate elements. The user of a List generally has precise control over where in the list each element is inserted and can access elements by their integer index (position).


Queue — a collection used to hold multiple elements prior to processing. Besides basic Collection operations, a Queue provides additional insertion, extraction, and inspection operations.


Queues typically, but do not necessarily, order elements in a FIFO (first-in, first-out) manner. Among the exceptions are priority queues, which order elements according to a supplied comparator or the elements' natural ordering. Whatever the ordering used, the head of the queue is the element that would be removed by a call to remove or poll. In a FIFO queue, all new elements are inserted at the tail of the queue. Other kinds of queues may use different placement rules. Every Queue implementation must specify its ordering properties.


Map — an object that maps keys to values. A Map cannot contain duplicate keys; each key can map to at most one value.

Read more...

9/21/2008

Python和JAVA 速度比较一例

接上次的素数判断问题,python的速度令人不敢恭维:
算法:查找1000000内有多少个素数的最“朴素”的算法。
说明:查找1000000内是因为,时间....

Read more...

8/26/2008

用JAVA操作ClearCase

本文关注怎么用JAVA处理ClearCase(ClearCase是rational公司开发的配置管理工具,了解详情点这里).在了解了怎么用JAVA.LANG.RUNTIME以后,主要的事情还是输入输出流,同步,以及字符串的处理.
下面这段代码演示了如何调用ClearCase的命令行:
[coolcode,lang="java",linenum="off"]
public String exec(String command) throws IOException {
Process process = Runtime.getRuntime().exec(command);

StringWriter outWriter = new StringWriter();
StringWriter errWriter = new StringWriter();
ProcessOutputReader outReader = new ProcessOutputReader(
process.getInputStream(),
outWriter);
ProcessOutputReader errReader = new ProcessOutputReader(
process.getErrorStream(),
errWriter);

Thread outReaderThread = new Thread(outReader);
Thread errReaderThread = new Thread(errReader);
outReaderThread.start();
errReaderThread.start();

synchronized (outReaderThread) {
if (outReaderThread.isAlive()) {
try {
outReaderThread.wait();
} catch (InterruptedException ie) {}
}
}
synchronized (errReaderThread) {
if (errReaderThread.isAlive()) {
try {
errReaderThread.wait();
} catch (InterruptedException ie) {}
}
}

String out = outWriter.toString();
String err = errWriter.toString();

return out
+(out.length() > 0 && err.length() > 0 ? "\n":"")
+err;
}
[/coolcode]
其中ProcessOutputReader类的定义:
[coolcode,lang="java",linenum="off"]
class ProcessOutputReader implements Runnable {
private InputStream _readFrom;
private Writer _writeTo;
ProcessOutputReader( InputStream readFrom, Writer writeTo) {
_readFrom = readFrom;
_writeTo = writeTo;
}
public void run() {
InputStreamReader bir = new InputStreamReader( _readFrom );
char[] ca = new char[100];
int numRead = 0;while ( numRead != -1 ) {

try {
numRead = bir.read(ca);
if ( numRead > 0 ) {
_writeTo.write(ca, 0, numRead);
}
} catch ( IOException e ) {
numRead = -1;
}
}
}
}
[/coolcode]
调用以上的方法:
[coolcode,lang="java",linenum="off"]
public static void main(String[] args) throws Exception {
RuntimeExec re = new RuntimeExec();
String result = re.exec("cleartool ls -s /XX/XX/XX...");
String result1=re.exec("cleartool lsvtr -g /XX/XX/XX...");
System.out.println(result);
System.out.println(result1);
}
[/coolcode]
上面代码的几点说明:
1.Runtime.getRuntime().exec(command) 通过JAVA提供的Runtime直接运行ClearCase的命令.Runtime是JAVA提供的,用于当前应用程序和运行环境交互,getRuntime()方法返回的就是当前Runtime实例.
2./XX/XX/XX... 代表目标文件夹或者文件.
3.cleartool lsvtr -g这个ClearCase命令可以打开图形界面.

如果想要获得Config Spec(不知道什么是Config Spec?点击这里了解),则应该这样调用:
[coolcode,lang="java",linenum="off"]
String catcs = re.exec("cleartool catcs");
[/coolcode]
在取得Config Spec后,剩下的都是字符串操作.

Read more...

8/07/2008

JAVA项目中常用开源包

本文介绍一些在JAVA项目中几乎必不可少的开源包.包括XML处理,测试,日志等方面.

Read more...

7/28/2008

java中transient关键字

transient
  Java语言的关键字,用来表示一个域不是该对象串行化的一部分。当一个对象被串行化的时候,transient型变量的值不包括在串行化的表示中,然而非transient型的变量是被包括进去的。

Read more...

7/14/2008

Endo-Testing: Unit Testing with Mock Objects

1.Introduction

Unit testing is a fundamental practice in Extreme Programming , but most nontrivial
code is difficult to test in isolation. You need to make sure that you test one feature at a
time, and you want to be notified as soon as any problem occurs. Normal unit testing is hard
because you are trying to test the code from outside.

Read more...

5/28/2008

四种语言的unicode处理简述

本文讲述了在JAVA, Perl, Python, Ruby语言中,unicode的处理

Read more...