行业知识
Java-常用集合
2026-10-29 20:28:34
来源:互联网转载
Jva常用集合
- 一、Java 集合框架体系
- 二、Collection接口和方法
- 1. List接口
- List 接口主要实现类:ArrayList
- List 的实现类之二:LinkedList
- List 的实现类之三:Vector
- 2. Set接口
- Set 主要实现类:HashSet
- Set 实现类之二:LinkedHashSet
- Set 实现类之三:TreeSet
- 三、Map的接口和方法
- 1. Map中Key和Value的特点
- 2. Map接口的常用方法
- 3. Map 的主要实现类:HashMap
- 4. Map 实现类之二:LinkedHashMap
- 5. Map 实现类之三:TreeMap
- 6. Map 实现类之四:Hashtable
- Hashtable 和 HashMap 的区别
- 7. Map 实现类之五:Properties
- 四、Collections工具���
一、Java 集合框架体系
Java 集合可分为 Collection 和 Map 两大体系:
- Collection 接口:用于存储一个一个的数据,也称单列数据集合。
- List 子接口:用来存储有序的、可以重复的数据(主要用来替换数组,"动态"数组)
实现类:ArrayList(主要实现类)、LinkedList、Vector
- Set 子接口:用来存储无序的、不可重复的数据(类似于高中讲的"集合")
实现类:HashSet(主要实现类)、LinkedHashSet、TreeSet
- Map 接口:用于存储具有映射关系“key-value 对”的集合,即一对一对的数据,也称双列数据集合。(类似于高中的函数、映射。(x1,y1) —> y = f(x) )
- 实现类:HashMap(主要实现类)、LinkedHashMap、TreeMap、Hashtable、Properties
Collection接口的继承树:
Map接口的继承树:
二、Collection接口和方法
JDK 不提供此接口的任何直接实现,而是提供更具体的子接口(如:Set 和 List)去实现。
Collection 接口是 List 和 Set 接口的父接口,该接口里定义的方法既可用于操作 Set 集合,也可用于操作 List 集合。
- Collection接口方法:
- 添加:
- add(E obj):添加元素对象到当前集合中
- addAll(Collection other):添加other集合的所有元素到当前集合中,即 this = this ∪ other
- 判断:
- int size():获取当前集合中实际存储的元素个数
- boolean isEmpty():判断当前集合是否为空集合
- boolean contains(Object obj):判断当前集合中是否存在与 obj 对象equals返回true的元素
- boolean containsAll(Collection coll):判断 coll 集合中的元素是否在当前集合中都存在。即 coll 集合是否是当前集合的“子集”
- boolean equals(Object obj):判断当前集合与 obj 是否相等
- 删除:
- void clear():清空集合元素
- boolean remove(Object obj) :从当前集合中删除第一个找到的与 obj 对象 equals 返回 true 的元素。
- boolean removeAll(Collection coll):从当前集合中删除所有与 coll 集合中相同的元素。即 this = this - this ∩ coll
- boolean retainAll(Collection coll):从当前集合中删除两个集合中不同的元素,使得当前集合仅保留与 coll 集合中的元素相同的元素,即当前集合中仅保留两个集合的交集,即 this = this ∩ coll
- 其他:
- Object[] toArray():返回包含当前集合中所有元素的数组
- hashCode():获取集合对象的哈希值
- iterator():返回迭代器对象,用于集合遍历
1. List接口
List 集合类中元素有序、且可重复,集合中的每个元素都有其对应的顺序索引。
List集合可以理解为一个可扩展的数组。
List 集合存储数据,就像银行门口客服,给每一个来办理业务的客户分配序号:第一个来的是“张三”,客服给他分配的是 0;第二个来的是“李四”,客服给他分配的 1;以此类推,最后一个序号应该是“总人数-1”。
- JDK API 中 List 接口的实现类常用的有:ArrayList、LinkedList 和 Vector。
以下是一些List接口操作集合元素的方法:
- 插入元素:
- void add(int index, Object ele):在 index 位置插入 ele 元素
- boolean addAll(int index, Collection eles):从 index 位置将 eles 中的所有元素添加进来
- 获取元素:
- Object get(int index):获取指定 index 位置的元素
- List subList(int fromIndex, int toIndex):返回从 fromIndex 到 toIndex 位置的子集合
- 获取元素索引:
- int indexOf(Object obj):返回 obj 在集合中首次出现的位置
- int lastIndexOf(Object obj):返回 obj 在当前集合中末次出现的位置
- 删除和替换元素:
- Object remove(int index):移除指定 index 位置的元素,并返回此元素
- Object set(int index, Object ele):设置指定 index 位置的元素为ele
package com.example.list;import java.util.ArrayList;import java.util.List;public class TestListMethod { public static void main(String[] args) { // 创建 List 集合对象 List list = new ArrayList(); // 往 尾部添加 指定元素 list.add("图图"); list.add("小美"); list.add("不高兴"); System.out.println(list); // ["图图","小美","不高兴"] // add(int index,String s) 往指定位置添加 list.add(1,"没头脑"); // ["图图","没头脑","小美","不高兴"] System.out.println(list); // String remove(int index) 删除指定位置元素 返回被删除元素 System.out.println("删除索引位置为 2 的元素"); System.out.println(list.remove(2)); System.out.println(list); // ["图图","没头脑","不高兴"] // String set(int index,String s) // 在指定位置 进行 元素替代(改) list.set(0, "三毛"); System.out.println(list); // String get(int index) 获取指定位置元素 // 跟 size() 方法一起用 来 遍历的 for(int i = 0;i System.out.println(list.get(i)); } //还可以使用增强 for for (String string : list) { System.out.println(string); } }} HashMap map = new HashMap(); map.put("许仙", "白娘子"); map.put("董永", "七仙女"); map.put("牛郎", "织女"); map.put("许仙", "小青"); System.out.println("所有的 key:"); Set keySet = map.keySet(); for (Object key : keySet) { System.out.println(key); } System.out.println("所有的 value:"); Collection values = map.values(); for (Object value : values) { System.out.println(value); } System.out.println("所有的映射关系:"); Set entrySet = map.entrySet(); for (Object mapping : entrySet) { //System.out.println(entry); Map.Entry entry = (Map.Entry) mapping; System.out.println(entry.getKey() + "-" + entry.getValue()); }} /* * 自然排序举例 * */ @Test public void test1(){ TreeMap map = new TreeMap(); map.put("CC",45); map.put("MM",78); map.put("DD",56); map.put("GG",89); map.put("JJ",99); Set entrySet = map.entrySet(); for(Object entry : entrySet){ System.out.println(entry); } } /* * 定制排序 * * */ @Test public void test2(){ //按照 User 的姓名的从小到大的顺序排列 TreeMap map = new TreeMap(new Comparator() { @Override public int compare(Object o1, Object o2) { if(o1 instanceof User && o2 instanceof User){ User u1 = (User)o1; User u2 = (User)o2; return u1.name.compareTo(u2.name); } throw new RuntimeException("输入的类型不匹配"); } }); map.put(new User("Tom",12),67); map.put(new User("Rose",23),"87"); map.put(new User("Jerry",2),88); map.put(new User("Eric",18),45); map.put(new User("Tommy",44),77); map.put(new User("Jim",23),88); map.put(new User("Maria",18),34); Set entrySet = map.entrySet(); for(Object entry : entrySet){ System.out.println(entry); } }}class User implements Comparable{ String name; int age; public User(String name, int age) { this.name = name; this.age = age; } public User() { } @Override public String toString() { return "User{" + "name='" + name + '\'' + ", age=" + age + '}'; } /* 举例:按照 age 从小到大的顺序排列,如果 age 相同,则按照 name 从大到小的顺序排列 * */ @Override public int compareTo(Object o) { if(this == o){ return 0; } if(o instanceof User){ User user = (User)o; int value = this.age - user.age; if(value != 0){ return value; } return -this.name.compareTo(user.name); } throw new RuntimeException("输入的类型不匹配"); }} Properties properties = System.getProperties(); String fileEncoding = properties.getProperty("file.encoding");//当前源文件字符编码 System.out.println("fileEncoding = " + fileEncoding);}@Testpublic void test02() { Properties properties = new Properties(); properties.setProperty("user","songhk"); properties.setProperty("password","123456"); System.out.println(properties);}@Testpublic void test03() throws IOException { Properties pros = new Properties(); pros.load(new FileInputStream("jdbc.properties")); String user = pros.getProperty("user"); System.out.println(user);} @Test public void test01(){ /* public static list) * 反转指定列表 List 中元素的顺序。 */ List list = new ArrayList(); Collections.addAll(list,"hello","java","world"); System.out.println(list); Collections.reverse(list); System.out.println(list); }@Test public void test04(){ /* public static void shuffle(List list) * List 集合元素进行随机排序,类似洗牌,打乱顺序 */ List list = new ArrayList(); Collections.addAll(list,"hello","java","world"); Collections.shuffle(list); System.out.println(list); }@Test public void test05() { /* public static
- 插入元素:
- JDK API 中 List 接口的实现类常用的有:ArrayList、LinkedList 和 Vector。
- 添加:
- Collection接口方法:
- 实现类:HashMap(主要实现类)、LinkedHashMap、TreeMap、Hashtable、Properties
- List 子接口:用来存储有序的、可以重复的数据(主要用来替换数组,"动态"数组)
- Collection 接口:用于存储一个一个的数据,也称单列数据集合。
上一篇:vultr服务器怎么登录?
下一篇:c怎么解析服务器xml数据?