JAva 09キューQueueとDeque
24226 ワード
Queue Deque.
Enumeration
Hashtable Hashtable Properties( )
( 、 、 、 ) WeakHashMap
IdentitvHashMap EnumMap
:
-Guava:Google Collection
-Apache:Commons Collection
:
- ( )
- :FIFO, 。 - : LIFO, 。 , 。 : add(e)、offer(e), remove()、poll(), element()、peek()
- ( ) : addFirst(e)、offerFirst(e)、push(e), addLast(e)、offerLast(e)、add(e)、offer(e), removeFirst()、pollFirst()、remove()、pop()、poll(), removeLast()、pollLast(), getFirst()、peekFirst()、element()、peek(), getLast()、peekLast()
/**
*
*/
public class Demo01 {
/*public interface Queue extends Collection {//jdk
boolean add(E e);
boolean offer(E e);
E remove();
E poll();
E element();
E peek();
} */
public static void main(String[] args) {
Queue que =new ArrayDeque();
//
for(int i=0;i<10;i++){
final int num =i;
que.offer(new Request(){// , final 。
@Override
public void deposit() { System.out.println(" "+num+" , , :"+(Math.random()*10000));
}
});
}
dealWith(que);
}
//
public static void dealWith(Queue que){//
Request req =null;
while(null!=(req=que.poll())){
req.deposit();
}
}
}
interface Request{
//
void deposit();
}
public class Demo02 {
public static void main(String[] args) {
MyStack backHistory =new MyStack(3);
backHistory.push("www.baidu.com");
backHistory.push("www.google.com");
backHistory.push("www.sina.com");
backHistory.push("www.bjsxt.cn");
System.out.println(" :"+backHistory.size());
//
String item=null;
while(null!=(item=backHistory.pop())){
System.out.println(item);
}
/*
:www.sina.com
www.google.com
www.baidu.com*/
}
}
Enumeration :
Iterator ,Iterator Enumeration。Enumeration jdk1.5 。
public class Demo01 {
public static void main(String[] args) {
Vector vector =new Vector();
vector.add("javase");
vector.add("html");
vector.add("oracle");
// Vector
/* public Enumeration elements() {//jdk
return new Enumeration() {
int count = 0;
public boolean hasMoreElements() {
return count < elementCount;
}
public E nextElement() {
synchronized (Vector.this) {
if (count < elementCount) {
return (E)elementData[count++];
}
}
throw new NoSuchElementException("Vector Enumeration");
}
};
} */
Enumeration en =vector.elements();//
while(en.hasMoreElements()){
System.out.println(en.nextElement());
}
}
}
/**
* Enumeration
* StringTokenizer String split()
* ,split() ,
* StringTokenizer(String str, String delim)
*/
public class Demo02 {
public static void main(String[] args) {
String emailStr="[email protected];[email protected];[email protected]";
StringTokenizer token =new StringTokenizer(emailStr,";");// Enumeration 。 hasMoreElements nextElement 。
//
while(token.hasMoreElements()){
System.out.println(token.nextElement());
}
}
}
Hashtable:Map , HashMap 。
HashMap , , null, null, AbstrctMap。
Hashtable ( ), , null, Dictionary( )。
Properties Hashtable ,Properties , 。
:
setProperty(String key, String value)
getProperty(String key)
getProperty(String key, String defaultValue)
.properties
store(OutputStream out, String comments)
store(Writer writer, String comments)
load(InputStream inStream)
load(Reader reader)
.xml
storeToXML(OutputStream os, String comment)// UTF-8
storeToXML(OutputStream os, String comment,String encoding)
loadFromXML(InputStream in)
:
1. :windows ,Linux Unix /
2. , ,
3. :
:
.class.getResourceAsStream("/"),
Thread.currentThread().getContextClassLoader().getResourceAsStream("")
import java.util.Properties;
/**
* Properties ,
* 1、Properties , key value
* 2、
* setProperty(String key, String value)
* getProperty(String key, String defaultValue)
* @author Administrator
*
*/
public class Demo01 {
public static void main(String[] args) {
// Properties
Properties pro =new Properties();
//
pro.setProperty("driver", "oracle.jdbc.driver.OracleDriver");
pro.setProperty("url", "jdbc:oracle:thin:@localhost:1521:orcl");
pro.setProperty("user", "scott");
pro.setProperty("pwd", "tiger");
//
String url =pro.getProperty("url","test");//
System.out.println(url);
}
}
import java.util.Properties;
/**
* Properties ( )
* ( ):
*
* 1、.properties
* store(OutputStream out, String comments)
store(Writer writer, String comments)
2、.xml
storeToXML(OutputStream os, String comment) :UTF-8
storeToXML(OutputStream os, String comment, String encoding)
* @author Administrator
*
*/
public class Demo02 {
public static void main(String[] args) throws FileNotFoundException, IOException {
//
Properties pro =new Properties();
//
pro.setProperty("driver", "oracle.jdbc.driver.OracleDriver");
pro.setProperty("url", "jdbc:oracle:thin:@localhost:1521:orcl");
pro.setProperty("user", "scott");
pro.setProperty("pwd", "tiger");
// e:/others :
pro.store(new FileOutputStream(new File("e:/others/db.properties")), "db ");//others , pro 。
/*
#db\u914D\u7F6E//
#Mon Jul 21 13:03:09 CST 2014
user=scott
url=jdbc\:oracle\:thin\:@localhost\:1521\:orcl
driver=oracle.jdbc.driver.OracleDriver
pwd=tiger*/
pro.storeToXML(new FileOutputStream(new File("e:/others/db.xml")), "db ");
/*key value
olor:#008000;text-decoration:underline;">http://java.sun.com/dtd/properties.dtd">
db
scott
jdbc:oracle:thin:@localhost:1521:orcl
oracle.jdbc.driver.OracleDriver
tiger
*/
// , 。 3 。
pro.store(new FileOutputStream(new File("db.properties")), "db ");
pro.store(new FileOutputStream(new File("src/db.properties")), "db ");
pro.store(new FileOutputStream(new File("src/com/bjsxt/others/pro/db.properties")), "db ");
}
}
import java.util.Properties;
/**
* Properties , 。
* :
*
* load(InputStream inStream)
load(Reader reader)
loadFromXML(InputStream in)
*/
public class Demo03 {
public static void main(String[] args) throws FileNotFoundException, IOException {
Properties pro=new Properties();
//
pro.load(new FileReader("e:/others/db.properties"));
//
// class ( ), ,src
pro.load(new FileReader("src/com/bjsxt/others/pro/db.properties"));
System.out.println(pro.getProperty("user", "bjsxt"));
}
}
import java.util.Properties;
/**
*
* bin
*/
public class Demo04 {
public static void main(String[] args) throws IOException {
Properties pro =new Properties();
// , / (class bin ) ,Demo04.class.getResourceAsStream("/") ( src )。
pro.load(Demo04.class.getResourceAsStream("/com/bjsxt/others/pro/db.properties"));
//Thread.currentThread() (main ),getContextClassLoader ,Thread.currentThread().getContextClassLoader("") ( src ),class bin
pro.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("com/bjsxt/others/pro/db.properties"));
System.out.println(pro.getProperty("user", "bjsxt"));
}
}