Java集合--LinkedList使用示例

LinkedList也像ArrayList一样实现了基本的List接口,但是它执行某些操作(在List中间插入和删除)时比ArrayList更加高效,但是随即访操作方面却要逊色一些。

LinkedList还添加了可以使其用作栈、队列、双端队列的方法

示例代码:

package com.trs.collection;

import java.util.LinkedList;


/**
 * LinkedList用法示例
 * @author xiayunan
 * @date   2018年7月8日
 *
 */
public class LinkedListFeatures {
	public static void main(String[] args) {
		LinkedList cityList = new LinkedList(); 
		cityList.add("合肥");
		cityList.add("南京");
		cityList.add("上海");
		cityList.add("杭州");
		cityList.add("芜湖");
		cityList.add("武汉");
		System.out.println(cityList);
		System.out.println("cityList.getFirst():"+cityList.getFirst());
		System.out.println("cityList.element():"+cityList.element());
		System.out.println("cityList.peek():"+cityList.peek());
		
		System.out.println("cityList.remove():"+cityList.remove());
		System.out.println(cityList);
		System.out.println("cityList.removeFirst():"+cityList.removeFirst());
		System.out.println(cityList);
		System.out.println("cityList.poll():"+cityList.poll());
		System.out.println(cityList);
		cityList.addFirst("长沙");
		System.out.println("After addFirst():"+cityList);
		cityList.offer("广州");
		System.out.println("After offer():"+cityList);
		cityList.add("深圳");
		System.out.println("After add():"+cityList);
		cityList.addLast("恩施");
		System.out.println("After addLast():"+cityList);
		System.out.println("Remove addLast():"+cityList.removeLast());
		System.out.println(cityList);
	}
}

运行结果:

Java集合--LinkedList使用示例_第1张图片

你可能感兴趣的:(Java-集合框架)