1、List(接上级--常用方法示例补充)
1.4 常用的方法
1.4.2 LinkedList(JDK8)
LinkedList是Java中一个实现了List接口和Deque接口的类,它采用链表结构存储数据,支持高效的插入和删除操作。
LinkedList中的所有方法及使用
使用示例:
1、构造方法
LinkedList<String> list = new LinkedList<>();
2、添加元素
// 在链表末尾添加元素
list.add("Apple");
// 在指定索引位置插入元素
list.add(0, "Banana");
3、删除元素
// 删除第一个元素(头节点)
String removedHead = list.removeFirst();
// 删除最后一个元素(尾节点)
String removedTail = list.removeLast();
// 删除特定对象首次出现的位置
if (list.contains("Apple")) {
list.removeFirstOccurrence("Apple");
}
// 删除特定对象最后一次出现的位置
if (list.contains("Apple")) {
list.removeLastOccurrence("Apple");
}
// 移除链表中的第一个元素
list.remove();
// 根据索引删除元素
list.remove(0);
// 移除指定元素(首次出现的)
list.remove("Apple"); // 如果存在的话,移除Apple
// 移除所有指定元素集合
list.removeAll(java.util.Arrays.asList("Apple", "Banana")); // 移除所有Apple和Banana(如果存在)
4、获取元素
// 获取第一个元素(头节点)
String firstItem = list.getFirst();
// 获取最后一个元素(尾节点)
String lastItem = list.getLast();
// 获取索引位置的元素
String itemAtIndex = list.get(0);
5、设置元素
// 设置第一个元素(头节点)
list.setFirst("Cherry");
// 设置最后一个元素(尾节点)
list.setLast("Cherry");
// 根据索引设置元素
list.set(0, "Cherry");
6、遍历和查找元素
// 使用迭代器遍历
Iterator<String> iterator = list.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
// 使用for-each循环遍历
for (String element : list) {
System.out.println(element);
}
// 查找元素是否存在
boolean contains = list.contains("Cherry");
// 获取元素的索引
int index = list.indexOf("Cherry");
// 查找指定元素最后一次出现的索引
int lastIndex = list.lastIndexOf("Banana"); // 查找Banana的最后一个索引(如果只有一个,则和indexOf一样)
7、其他操作
// 获取列表大小(元素数量)
int size = list.size();
// 清空列表
list.clear();
// 判断列表是否为空
boolean isEmpty = list.isEmpty();
// 在链表头部添加元素
list.addFirst("Orange");
// 在链表尾部添加元素
list.addLast("Grape");
// 从链表头部弹出并返回元素
String poppedHead = list.pop();
// 从链表尾部移除并返回元素
String polledTail = list.pollLast();
// 检查链表是否包含另一个集合的所有元素
boolean allElementsPresent = list.containsAll(anotherList);
// 移除链表中所有与另一个集合相同的元素
list.removeAll(anotherList);
// 截取子列表
List<String> sublist = list.subList(fromIndex, toIndex);
8、排序操作
由于LinkedList实现了List接口,因此可以使用Collections.sort()进行排序,但请注意这将创建一个新的ArrayList来完成排序,并在排序后替换原链表内容。
Collections.sort(list);
如果是对某个实体类,需要根据不同的字段或逻辑来排序,也可以创建一个Comparator
:
例:
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedList;
public class LinkedListExample {
public static void main(String[] args) {
LinkedList<Person> list = new LinkedList<>();
list.add(new Person("Alice", 25));
list.add(new Person("Bob", 20));
list.add(new Person("Charlie", 30));
// 自定义排序,按照年龄升序排序
Collections.sort(list, new Comparator<Person>() {
@Override
public int compare(Person p1, Person p2) {
return Integer.compare(p1.age, p2.age);
}
});
// 输出: [Bob, Alice, Charlie] 此处Person类需重写toString方法
System.out.println(list);
}
}
class Person {
String name;
int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public String toString() {
return name + " " + age;
}
}
或者是使用 Java 8 的 Lambda 表达式进行排序:
import java.util.Collections;
import java.util.LinkedList;
import java.util.Comparator;
public class LinkedListExample {
public static void main(String[] args) {
LinkedList<Person> list = new LinkedList<>();
list.add(new Person("Alice", 25));
list.add(new Person("Bob", 20));
list.add(new Person("Charlie", 30));
// 使用 Lambda 表达式进行自定义排序,按照年龄升序排序 :Person类需有getAge方法
Collections.sort(list, Comparator.comparingInt(Person::getAge));
// 输出: [Bob, Alice, Charlie]
System.out.println(list);
}
}
class Person {
String name;
int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public int getAge() {
return age;
}
@Override
public String toString() {
return name + " " + age;
}
}