写在前面的话:读书破万卷,编码如有神
--------------------------------------------------------------------
下文主要对java.util.LinkedList<E>的5个队列操作进行介绍,主要内容包括:
1、LinkedList常用的5个队列操作介绍
参考内容:
1、JDK源码(1.7)
--------------------------------------------------------------------
1、LinkedList常用的5个队列操作介绍
(1) E peek()
功能: 获取,但不移除此双端队列的头部元素(如果此双端队列为空,则返回 null)
示例代码:
1 import java.util.LinkedList; 2 3 public class LinkedListDemo { 4 public static void main(String[] args) { 5 /*********测试LinkedList的'E peek()'方法的使用**********/ 6 7 //创建一个LinkedList对象 8 LinkedList<Student> linkedList = new LinkedList<Student>(); 9 10 //创建一个Student对象,并将其添加到LinkedList对象中 11 Student stu1 = new Student(1,"zhangsan",20); 12 linkedList.add(stu1); 13 14 //创建一个Student对象,并将其添加到LinkedList对象中 15 Student stu2 = new Student(2,"lisi",21); 16 linkedList.add(stu2); 17 18 //创建一个Student对象,并将其添加到LinkedList对象中 19 Student stu3 = new Student(3,"wangwu",22); 20 linkedList.add(stu3); 21 System.out.println("linkedList:" + linkedList); 22 23 System.out.println("linkedList.peek():" + linkedList.peek()); 24 System.out.println("linkedList:" + linkedList); 25 } 26 } 27 28 运行结果: 29 linkedList:[Student [stuId=1, stuName=zhangsan, stuAge=20], Student [stuId=2, stuName=lisi, stuAge=21], Student [stuId=3, stuName=wangwu, stuAge=22]] 30 linkedList.peek():Student [stuId=1, stuName=zhangsan, stuAge=20] 31 linkedList:[Student [stuId=1, stuName=zhangsan, stuAge=20], Student [stuId=2, stuName=lisi, stuAge=21], Student [stuId=3, stuName=wangwu, stuAge=22]]
源代码如下:
1 public E peek() { 2 //引用f指向此双端队列的头部 3 final Node<E> f = first; 4 //返回此双端队列的头部元素 5 return (f == null) ? null : f.item; 6 }
(2) E element()
功能: 获取,但不移除此双端队列所表示的队列的头部
示例代码:
1 import java.util.LinkedList; 2 3 public class LinkedListDemo { 4 public static void main(String[] args) { 5 /*********测试LinkedList的'E element()'方法的使用**********/ 6 7 //创建一个LinkedList对象 8 LinkedList<Student> linkedList = new LinkedList<Student>(); 9 10 //创建一个Student对象,并将其添加到LinkedList对象中 11 Student stu1 = new Student(1,"zhangsan",20); 12 linkedList.add(stu1); 13 14 //创建一个Student对象,并将其添加到LinkedList对象中 15 Student stu2 = new Student(2,"lisi",21); 16 linkedList.add(stu2); 17 18 //创建一个Student对象,并将其添加到LinkedList对象中 19 Student stu3 = new Student(3,"wangwu",22); 20 linkedList.add(stu3); 21 System.out.println("linkedList:" + linkedList); 22 23 System.out.println("linkedList.element():" + linkedList.element()); 24 System.out.println("linkedList:" + linkedList); 25 } 26 } 27 28 运行结果: 29 linkedList:[Student [stuId=1, stuName=zhangsan, stuAge=20], Student [stuId=2, stuName=lisi, stuAge=21], Student [stuId=3, stuName=wangwu, stuAge=22]] 30 linkedList.element():Student [stuId=1, stuName=zhangsan, stuAge=20] 31 linkedList:[Student [stuId=1, stuName=zhangsan, stuAge=20], Student [stuId=2, stuName=lisi, stuAge=21], Student [stuId=3, stuName=wangwu, stuAge=22]]
源代码如下:
1 /* 2 获取,但不移除此双端队列所表示的队列的头部(如果此队列为空,会抛出异常) 3 */ 4 public E element() { 5 //调用内部方法getFirst()实现 6 return getFirst(); 7 } 8 9 public E getFirst() { 10 final Node<E> f = first; 11 if (f == null) 12 throw new NoSuchElementException(); 13 return f.item; 14 }
(3) E poll()
功能: 获取并移除此双端队列所表示的队列的头部(如果此双端队列为空,则返回 null)
示例代码:
1 import java.util.LinkedList; 2 3 public class LinkedListDemo { 4 public static void main(String[] args) { 5 /*********测试LinkedList的'E poll()'方法的使用**********/ 6 7 //创建一个LinkedList对象 8 LinkedList<Student> linkedList = new LinkedList<Student>(); 9 10 //创建一个Student对象,并将其添加到LinkedList对象中 11 Student stu1 = new Student(1,"zhangsan",20); 12 linkedList.add(stu1); 13 14 //创建一个Student对象,并将其添加到LinkedList对象中 15 Student stu2 = new Student(2,"lisi",21); 16 linkedList.add(stu2); 17 18 //创建一个Student对象,并将其添加到LinkedList对象中 19 Student stu3 = new Student(3,"wangwu",22); 20 linkedList.add(stu3); 21 System.out.println("linkedList:" + linkedList); 22 23 System.out.println("linkedList.poll():" + linkedList.poll()); 24 System.out.println("linkedList:" + linkedList); 25 } 26 } 27 28 运行结果: 29 linkedList:[Student [stuId=1, stuName=zhangsan, stuAge=20], Student [stuId=2, stuName=lisi, stuAge=21], Student [stuId=3, stuName=wangwu, stuAge=22]] 30 linkedList.poll():Student [stuId=1, stuName=zhangsan, stuAge=20] 31 linkedList:[Student [stuId=2, stuName=lisi, stuAge=21], Student [stuId=3, stuName=wangwu, stuAge=22]]
源代码如下:
1 /* 2 获取并移除此双端队列所表示的队列的头部;如果此双端队列为空,则返回 null 3 */ 4 public E poll() { 5 //引用f指向此双端队列的头部 6 final Node<E> f = first; 7 //获取并移除头部元素(调用unlinkFirst方法实现) 8 return (f == null) ? null : unlinkFirst(f); 9 }
(4) E remove()
功能: 获取并移除此双端队列所表示的队列的头部(如果此双端队列为空,则抛出异常)
示例代码:
import java.util.LinkedList; public class LinkedListDemo { public static void main(String[] args) { /*********测试LinkedList的'E remove()'方法的使用**********/ //创建一个LinkedList对象 LinkedList<Student> linkedList = new LinkedList<Student>(); //创建一个Student对象,并将其添加到LinkedList对象中 Student stu1 = new Student(1,"zhangsan",20); linkedList.add(stu1); //创建一个Student对象,并将其添加到LinkedList对象中 Student stu2 = new Student(2,"lisi",21); linkedList.add(stu2); //创建一个Student对象,并将其添加到LinkedList对象中 Student stu3 = new Student(3,"wangwu",22); linkedList.add(stu3); System.out.println("linkedList:" + linkedList); System.out.println("linkedList.remove():" + linkedList.remove()); System.out.println("linkedList:" + linkedList); } } 运行结果: linkedList:[Student [stuId=1, stuName=zhangsan, stuAge=20], Student [stuId=2, stuName=lisi, stuAge=21], Student [stuId=3, stuName=wangwu, stuAge=22]] linkedList.remove():Student [stuId=1, stuName=zhangsan, stuAge=20] linkedList:[Student [stuId=2, stuName=lisi, stuAge=21], Student [stuId=3, stuName=wangwu, stuAge=22]]
源代码如下:
1 /* 2 获取并移除此双端队列所表示的队列的头部(如果此双端队列为空,则抛出异常) 3 */ 4 public E remove() { 5 //调用removeFirst方法实现 6 return removeFirst(); 7 }
(5) boolean offer(E e)
功能:将指定元素插入此双端队列所表示的队列,如果成功,则返回 true
示例代码:
1 import java.util.LinkedList; 2 3 public class LinkedListDemo { 4 public static void main(String[] args) { 5 /*********测试LinkedList的'boolean offer(E e)'方法的使用**********/ 6 7 //创建一个LinkedList对象 8 LinkedList<Student> linkedList = new LinkedList<Student>(); 9 10 //创建一个Student对象,并将其添加到LinkedList对象中 11 Student stu1 = new Student(1,"zhangsan",20); 12 linkedList.add(stu1); 13 14 //创建一个Student对象,并将其添加到LinkedList对象中 15 Student stu2 = new Student(2,"lisi",21); 16 linkedList.add(stu2); 17 18 //创建一个Student对象,并将其添加到LinkedList对象中 19 Student stu3 = new Student(3,"wangwu",22); 20 linkedList.add(stu3); 21 System.out.println("linkedList:" + linkedList); 22 23 24 Student stu4 = new Student(4,"oopp",22); 25 System.out.println("linkedList.remove():" + linkedList.offer(stu4)); 26 System.out.println("linkedList:" + linkedList); 27 } 28 } 29 30 运行结果: 31 linkedList:[Student [stuId=1, stuName=zhangsan, stuAge=20], Student [stuId=2, stuName=lisi, stuAge=21], Student [stuId=3, stuName=wangwu, stuAge=22]] 32 linkedList.remove():true 33 linkedList:[Student [stuId=1, stuName=zhangsan, stuAge=20], Student [stuId=2, stuName=lisi, stuAge=21], Student [stuId=3, stuName=wangwu, stuAge=22], Student [stuId=4, stuName=oopp, stuAge=22]]
源代码如下:
1 /* 2 将指定元素插入此双端队列所表示的队列,如果成功,则返回 true 3 */ 4 public boolean offer(E e) { 5 //调用内部方法add实现 6 return add(e); 7 }
--------------------------------------------------------------------
java.util.LinkedList<E>系列文章
java.util.LinkedList<E>(1) java.util.LinkedList<E>(2) java.util.LinkedList<E>(3)
java.util.LinkedList<E>(4) java.util.LinkedList<E>(5) java.util.LinkedList<E>(6)
java.util.LinkedList<E>(7) java.util.LinkedList<E>(8)
--------------------------------------------------------------------
相关知识
java.util.Collection<E> java.util.AbstractCollection<E> java.util.List<E>
java.util.AbstractList<E> java.util.Iterator<E> java.util.ListIterator<E>