还是这个靠谱!
Method Summary
int
Set the position of the first result to retrieve.
Set the maximum number of results to retrieve.
Calendar value, TemporalType temporalType)
Bind an instance of java.util.Calendar to a positional parameter.
Date value, TemporalType temporalType)
Bind an instance of java.util.Date to a positional parameter.
Object value)
Bind an argument to a positional parameter.
PriorityQuery实现类:
PriorityQuery不允许插入null元素,还需对队列的元素进行排序:
1:自然排序:集合中的元素必须实现Comparable接口,而且应该是同一个类的多个实例,否则可能导致ClassCastException异常。
2.定制排序。不要求队列元素实现Comparable接口。创建PriorityQuery队列时,传入一个Comparator对象,该对象负责对所以元素进行排序。
实例:
package com.haixu.query;
import java.util.PriorityQueue;
/*
* Query测试类内部方法
* */
public class QueryTest {
public static void main(String[] args) {
PriorityQueue pq = new PriorityQueue();
//添加元素
pq.offer("Haixu");
pq.offer("Success");
pq.offer("Hard work");
//输出元素内容。
System.out.println(pq);
//取出列表头部元素
System.out.println(pq.poll());
System.out.println(pq);
//取出列表头部元素
System.out.println(pq.peek());
System.out.println(pq);
//注意poll 和 peek 的区别!
}
}
java.util
Interface Deque
Type Parameters: //元素类型
E - the type of elements held in this collection //E-在元素在集合中的类型
All Superinterfaces:
All Known Subinterfaces: //子接口
Deque
extends Queue
A linear collection that supports element insertion and removal at both ends. The name
deque is short for "double ended queue" (双端队列)and is usually pronounced "deck". Most Deque implementations place no fixed limits on the number of elements they may contain, but this interface supports capacity-restricted deques as well as those with no fixed size limit.
//所有的线性集合都支持在尾部对元素的插入和删除操作,
This interface defines methods to access the elements at both ends of the deque. Methods are provided to insert, remove, and examine the element. Each of these methods exists in two forms: one throws an exception if the operation fails, the other returns a special value (either null or false, depending on the operation). The latter form of the insert operation is designed specifically for use with capacity-restricted Deque implementations; in most implementations, insert operations cannot fail.
The twelve methods described above are summarized in the following table:
太费劲了,不翻译了,自己看吧,反正都很简单!
This interface extends the Queue interface. When a deque is used as a queue, FIFO (First-In-First-Out) behavior results. Elements are added at the end of the deque and removed from the beginning. The methods inherited from the Queue interface are precisely equivalent to Deque methods as indicated in the following table:
Deques can also be used as LIFO (Last-In-First-Out) stacks. This interface should be used in preference to the legacy Stack class. When a deque is used as a stack, elements are pushed and popped from the beginning of the deque. Stack methods are precisely equivalent to Deque methods as indicated in the table below:
ArrayDeque当成栈来使用!
package com.haixu.query;
import java.util.ArrayDeque;
/*
* ArrayDeque 类内部方法应用
* 使用方法类似于Stack
* */
public class ArrayDequeTest {
public static void main(String[] args) {
ArrayDeque stack = new ArrayDeque();
//基本使用的方法类似与Query类
stack.push("Come on");
stack.push("Haixu");
stack.push("you will success");
System.out.println(stack);
System.out.println(stack.peek());
System.out.println(stack);
System.out.println(stack.poll());
System.out.println(stack);
}
}
实例:
package com.haixu.query;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
/**
* 测试各个线性集合的性能
* */
public class PerformanceTest {
public static void main(String[] args) {
//创建字符数组
String [] arr = new String[900000];
for(int i=0; i<900000; i++){
arr[i] = String.valueOf(i);
}
//将所有元素存入ArrayList集合中
ArrayList a1 = new ArrayList();
for(int i=0; i<900000; i++){
a1.add(arr[i]);
}
//将所有元素存入LinkedList集合中
LinkedList l1 = new LinkedList();
for(int i=0; i<900000; i++){
l1.add(arr[i]);
}
//迭代方位ArrayList集合的所有元素,并输出迭代时间
Long start = System.currentTimeMillis();
for(Iterator it = a1.iterator(); it.hasNext();){
it.next();
}
System.out.println("迭代ArrayList集合元素的时间:" + (System.currentTimeMillis() - start));
//迭代访问LinkedList集合的所有元素,并输出迭代时间
start = System.currentTimeMillis();
for(Iterator it = l1.iterator(); it.hasNext();){
it.next();
}
System.out.println("迭代LinkedList集合元素的时间:" + (System.currentTimeMillis() - start));
}
}
多次运行以上程序,会发现ArrayList集合的时间略大于LinkedList集合
因此在使用List集合是有以下建议: