在Java编程中,集合框架是一个极其重要的部分,它为处理数据提供了丰富的工具。本文将详细介绍Java集合框架中的50个实用方法,帮助你在各种数据存储需求中游刃有余。
1. ArrayList.add(E e)
向列表的末尾添加元素。
ArrayList<String> list = new ArrayList<>();
list.add("Element1");
list.add("Element2");
2. ArrayList.get(int index)
返回列表中指定位置的元素。
String element = list.get(1);
3. ArrayList.remove(int index)
删除列表中指定位置的元素。
list.remove(1);
4. ArrayList.size()
返回列表中的元素数量。
int size = list.size();
5. ArrayList.isEmpty()
判断列表是否为空。
boolean isEmpty = list.isEmpty();
6. ArrayList.contains(Object o)
判断列表是否包含指定元素。
boolean contains = list.contains("Element1");
7. ArrayList.indexOf(Object o)
返回指定元素的索引,如果不存在,返回-1。
int index = list.indexOf("Element1");
8. ArrayList.lastIndexOf(Object o)
返回指定元素的最后一个索引,如果不存在,返回-1。
int lastIndex = list.lastIndexOf("Element1");
9. ArrayList.clear()
清空列表中的所有元素。
list.clear();
10. ArrayList.set(int index, E element)
用指定元素替换列表中指定位置的元素。
list.set(1, "NewElement");
11. ArrayList.subList(int fromIndex, int toIndex)
返回列表中指定范围的子列表。
List<String> sublist = list.subList(1, 3);
12. LinkedList.add(E e)
向链表的末尾添加元素。
LinkedList<String> linkedList = new LinkedList<>();
linkedList.add("Element1");
13. LinkedList.get(int index)
返回链表中指定位置的元素。
String element = linkedList.get(1);
14. LinkedList.remove(int index)
删除链表中指定位置的元素。
linkedList.remove(1);
15. LinkedList.size()
返回链表中的元素数量。
int size = linkedList.size();
16. LinkedList.isEmpty()
判断链表是否为空。
boolean isEmpty = linkedList.isEmpty();
17. LinkedList.contains(Object o)
判断链表是否包含指定元素。
boolean contains = linkedList.contains("Element1");
18. LinkedList.indexOf(Object o)
返回指定元素的索引,如果不存在,返回-1。
int index = linkedList.indexOf("Element1");
19. LinkedList.lastIndexOf(Object o)
返回指定元素的最后一个索引,如果不存在,返回-1。
int lastIndex = linkedList.lastIndexOf("Element1");
20. LinkedList.clear()
清空链表中的所有元素。
linkedList.clear();
21. LinkedList.set(int index, E element)
用指定元素替换链表中指定位置的元素。
linkedList.set(1, "NewElement");
22. LinkedList.subList(int fromIndex, int toIndex)
返回链表中指定范围的子链表。
List<String> sublist = linkedList.subList(1, 3);
23. HashSet.add(E e)
向集合中添加元素。
HashSet<String> set = new HashSet<>();
set.add("Element1");
24. HashSet.contains(Object o)
判断集合是否包含指定元素。
boolean contains = set.contains("Element1");
25. HashSet.size()
返回集合中的元素数量。
int size = set.size();
26. HashSet.isEmpty()
判断集合是否为空。
boolean isEmpty = set.isEmpty();
27. HashSet.clear()
清空集合中的所有元素。
set.clear();
28. HashSet.remove(Object o)
从集合中删除指定元素。
set.remove("Element1");
29. TreeSet.add(E e)
向集合中添加元素,并按照元素的自然顺序排序。
TreeSet<String> treeSet = new TreeSet<>();
treeSet.add("Element1");
30. TreeSet.contains(Object o)
判断集合是否包含指定元素。
boolean contains = treeSet.contains("Element1");
31. TreeSet.size()
返回集合中的元素数量。
int size = treeSet.size();
32. TreeSet.isEmpty()
判断集合是否为空。
boolean isEmpty = treeSet.isEmpty();
33. TreeSet.clear()
清空集合中的所有元素。
treeSet.clear();
34. TreeSet.remove(Object o)
从集合中删除指定元素。
treeSet.remove("Element1");
35. HashMap.put(K key, V value)
将键值对存入映射中。
HashMap<String, String> map = new HashMap<>();
map.put("Key1", "Value1");
36. HashMap.get(Object key)
根据键获取值。
String value = map.get("Key1");
37. HashMap.containsKey(Object key)
判断映射中是否包含指定键。
boolean containsKey = map.containsKey("Key1");
38. HashMap.containsValue(Object value)
判断映射中是否包含指定值。
boolean containsValue = map.containsValue("Value1");
39. HashMap.size()
返回映射中的键值对数量。
int size = map.size();
40. HashMap.isEmpty()
判断映射是否为空。
boolean isEmpty = map.isEmpty();
41. HashMap.clear()
清空映射中的所有键值对。
map.clear();
42. HashMap.remove(Object key)
从映射中删除指定键。
map.remove("Key1");
43. TreeMap.put(K key, V value)
将键值对存入映射中,并按照键的自然顺序排序。
TreeMap<String, String> treeMap = new TreeMap<>();
treeMap.put("Key1", "Value1");
44. TreeMap.get(Object key)
根据键获取值。
String value = treeMap.get("Key1");
45. TreeMap.containsKey(Object key)
判断映射中是否包含指定键。
boolean containsKey = treeMap.containsKey("Key1");
46. TreeMap.containsValue(Object value)
判断映射中是否包含指定值。
boolean containsValue = treeMap.containsValue("Value1");
47. TreeMap.size()
返回映射中的键值对数量。
int size = treeMap.size();
48. TreeMap.isEmpty()
判断映射是否为空。
boolean isEmpty = treeMap.isEmpty();
49. TreeMap.clear()
清空映射中的所有键值对。
treeMap.clear();
50. TreeMap.remove(Object key)
从映射中删除指定键。
treeMap.remove("Key1");
通过以上50个实用方法的介绍,相信你已经对Java集合框架有了更深入的了解。在实际开发中,合理运用这些方法,能够让你更加高效地处理数据存储需求。
