疯狂java笔记_疯狂Java学习笔记(38)-----------Query集合

还是这个靠谱!

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);

}

}

13be5bcf7d0c2c96dc8575519e662c57.png

实例:

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集合是有以下建议:

6fd67e06b341efc8bc1b48ccdbe8fc8a.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值