疯狂Java学习笔记(38)-----------Query集
疯狂Java学习笔记(38)-----------Query集
Query集合用于模拟队列这种数据结构,队列通常是指“先进先出”(FIFO)的容器。
队列的头部保存队列的尾部,访问元素(poll)操作会返回队列头部的元素。通常,队列不允许随机访问队列的元素。
Query接口定义的几个方法:
void add(Object e):将制定的元素加入到此队列的尾部.
Object element(): 获得对猎头部的元素,但是不删除此元素。
boolean offer(Object e):将指定元素加入此队列的尾部。当使用又有容量限制的队列时,此方法通常比add(Object e)方法更好。
Object poll():获得队列头部的元素并删除元素。如果队列为空。返回null;
Object peek():获得队列头部的元素,但不删除元素,如果队列为空。返回null;
还是这个靠谱!
Method Summary | |
---|---|
int |
executeUpdate() Execute an update or delete statement. |
List |
getResultList() Execute a SELECT query and return the query results as a List. |
Object |
getSingleResult() Execute a SELECT query that returns a single result. |
Query |
setFirstResult(int startPosition) Set the position of the first result to retrieve. |
Query |
setFlushMode(FlushModeType flushMode) Set the flush mode type to be used for the query execution. |
Query |
setHint(String hintName, Object value) Set an implementation-specific hint. |
Query |
setMaxResults(int maxResult) Set the maximum number of results to retrieve. |
Query |
setParameter(int position, Calendar value, TemporalType temporalType) Bind an instance of java.util.Calendar to a positional parameter. |
Query |
setParameter(int position, Date value, TemporalType temporalType) Bind an instance of java.util.Date to a positional parameter. |
Query |
setParameter(int position, Object value) Bind an argument to a positional parameter. |
Query |
setParameter(String name, Calendar value, TemporalType temporalType) Bind an instance of java.util.Calendar to a named parameter. |
Query |
setParameter(String name, Date value, TemporalType temporalType) Bind an instance of java.util.Date to a named parameter. |
Query |
setParameter(String name, Object value) Bind an argument to a named 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<String> pq = new PriorityQueue<String>();
- //添加元素
- 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 的区别!
- }
- }
Interface Deque<E>
-
- Type Parameters: //元素类型
-
E
- the type of elements held in this collection //E-在元素在集合中的类型
- All Superinterfaces:
- Collection<E>, Iterable<E>, Queue<E>
- All Known Subinterfaces: //子接口
- BlockingDeque<E>
- All Known Implementing Classes: //实现类
- ArrayDeque, ConcurrentLinkedDeque, LinkedBlockingDeque, LinkedList
public interface Deque<E> extends Queue<E>
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:
太费劲了,不翻译了,自己看吧,反正都很简单!
First Element (Head) | Last Element (Tail) | |||
Throws exception | Special value | Throws exception | Special value | |
Insert | addFirst(e) |
offerFirst(e) |
addLast(e) |
offerLast(e) |
Remove | removeFirst() |
pollFirst() |
removeLast() |
pollLast() |
Examine | getFirst() |
peekFirst() |
getLast() |
peekLast() |
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:
Queue Method | Equivalent Deque Method |
add(e) |
addLast(e) |
offer(e) |
offerLast(e) |
remove() |
removeFirst() |
poll() |
pollFirst() |
element() |
getFirst() |
peek() |
peekFirst() |
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:
Stack Method | Equivalent Deque Method |
push(e) |
addFirst(e) |
pop() |
removeFirst() |
peek() |
peekFirst() |
ArrayDeque当成栈来使用!
- <span style="font-size:18px;">
- package com.haixu.query;
- import java.util.ArrayDeque;
- /*
- * ArrayDeque 类内部方法应用
- * 使用方法类似于Stack
- * */
- public class ArrayDequeTest {
- public static void main(String[] args) {
- ArrayDeque<String> stack = new ArrayDeque<String>();
- //基本使用的方法类似与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);
- }
- }
- </span>
实例:
- 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<String> a1 = new ArrayList<String>();
- for(int i=0; i<900000; i++){
- a1.add(arr[i]);
- }
- //将所有元素存入LinkedList集合中
- LinkedList<String> l1 = new LinkedList<String>();
- for(int i=0; i<900000; i++){
- l1.add(arr[i]);
- }
- //迭代方位ArrayList集合的所有元素,并输出迭代时间
- Long start = System.currentTimeMillis();
- for(Iterator<String> it = a1.iterator(); it.hasNext();){
- it.next();
- }
- System.out.println("迭代ArrayList集合元素的时间:" + (System.currentTimeMillis() - start));
- //迭代访问LinkedList集合的所有元素,并输出迭代时间
- start = System.currentTimeMillis();
- for(Iterator<String> it = l1.iterator(); it.hasNext();){
- it.next();
- }
- System.out.println("迭代LinkedList集合元素的时间:" + (System.currentTimeMillis() - start));
- }
- }
多次运行以上程序,会发现ArrayList集合的时间略大于LinkedList集合
因此在使用List集合是有以下建议:
转载自:https://blog.****.net/u011225629/article/details/45848819