java集合
目录
1.ArrayList集合
ArrayList
是基于动态数组实现的列表。它允许存储重复元素,并且提供了对元素的随机访问。- 特点:支持快速的随机访问(通过索引),但在列表中间插入或删除元素时效率较低,因为这可能需要移动后续的元素。
图解
案例:ArrayList的简单使用
1.代码
package org.xiji.collection; import java.util.ArrayList; public class MyArrayList { public static void main(String[] args) { System.out.println("ArrayList的api简单使用"); System.out.println("ArrayList的初始化"); ArrayList<String> objects = new ArrayList<>(); System.out.println("ArrayList的添加"); objects.add("1"); System.out.println(objects.toString()); objects.add("2"); System.out.println(objects.toString()); objects.add("3"); System.out.println(objects.toString()); System.out.println("ArrayList的删除"); objects.remove(1); System.out.println(objects.toString()); } }
2.效果
2.LinkedList集合
LinkedList
是双向链表实现的列表。每个元素都是一个单独的对象,内部包含数据以及对其前后元素的引用。- 特点:在任意位置插入和删除操作非常高效,但随机访问较慢,因为它需要从头或尾开始遍历到指定位置。
图解
案例:LinkedList的简单使用
1.代码
package org.xiji.collection; import java.util.LinkedList; public class MyLinkedList { public static void main(String[] args) { System.out.println("LinkedList的api简单使用"); System.out.println("LinkedList的初始化"); LinkedList<String> strings = new LinkedList<>(); System.out.println("LinkedList的添加"); strings.add("1"); System.out.println(strings.toString()); strings.add("2"); System.out.println(strings.toString()); strings.add("3"); System.out.println(strings.toString()); System.out.println("LinkedList的获取"); System.out.println(strings.get(0)); System.out.println(strings.get(1)); System.out.println(strings.get(2)); System.out.println("LinkedList的修改"); strings.set(0,"4"); System.out.println(strings.toString()); strings.set(1,"5"); System.out.println(strings.toString()); strings.set(2,"6"); System.out.println(strings.toString()); System.out.println("LinkedList的删除"); strings.remove(1); System.out.println(strings.toString()); System.out.println("LinkedList的遍历"); for (String string : strings) { System.out.println(string); } } }
2.效果
3.Iterator 接口
Iterator
接口提供了一种方法来访问集合中的元素,而不需要暴露其底层表示。- 它定义了如
hasNext()
、next()
和remove()
等方法,用于迭代集合中的元素。是所有集合框架中用于遍历的标准方式之一。
案例:遍历ArrayList
1.代码
package org.xiji.collection; import java.util.ArrayList; public class MyArrayListF { public static void main(String[] args) { ArrayList<String> strings = new ArrayList<>(); strings.add("1"); strings.add("2"); strings.add("3"); //遍历 for (String string : strings) { System.out.println(string); } } }
2.效果
4.HashSet集合
HashSet
实现了Set接口,不允许存储重复元素。它是基于哈希表实现的。- 特点:不保证元素的顺序;查找、添加和移除元素通常具有很好的性能。
案例:HashSet的简单使用
1.代码
package org.xiji.collection; import java.util.ArrayList; import java.util.HashSet; public class MyHashSet { public static void main(String[] args) { System.out.println("HashSet的api简单使用"); System.out.println("HashSet的初始化"); String three = "星期三"; HashSet<String> strings = new HashSet<>(); //添加星期的名字 strings.add("星期一"); strings.add("星期二"); strings.add("星期三"); //输出 System.out.println(strings.toString()); System.out.println("HashSet的删除"); strings.remove("星期二"); System.out.println(strings.toString()); System.out.println("HashSet的遍历"); for (String string : strings) { System.out.println(string); } //添加星期三 strings.add("星期三"); strings.add("星期三"); System.out.println(strings.toString()); System.out.println("可以看到星期三并没有添加"); } }
2.效果
5.TreeSet集合
TreeSet
也是实现了Set接口的一个类,同样不允许有重复元素。它使用树结构存储元素。- 特点:保持元素按自然排序或者根据创建时提供的Comparator进行排序;可以高效地访问有序的数据集。
案例:TreeSet的简单使用
1.代码
package org.xiji.collection; import java.util.TreeSet; public class MyTreeSet { public static void main(String[] args) { System.out.println("TreeSet的api简单使用"); System.out.println("TreeSet的初始化"); String three = "星期三"; TreeSet<String> strings = new TreeSet<>(); //添加星期的名字 strings.add("星期一"); strings.add("星期二"); strings.add("星期三"); //输出 System.out.println(strings.toString()); System.out.println("TreeSet的删除"); strings.remove(three); System.out.println(strings.toString()); System.out.println("TreeSet的遍历"); for (String string : strings) { System.out.println(string); } //添加星期三 strings.add("星期三"); strings.add("星期三"); System.out.println(strings.toString()); System.out.println("我们添加有两次,但是只有一次被添加"); } }
2.效果
6.HaspMap集合
HashMap
提供了一个键值对存储结构,其中键是唯一的。它基于哈希表实现。- 特点:支持快速的查找、添加和删除操作;没有固定的顺序;键不能为null(除非使用特殊的构造函数)。
注:这个不重复是指的键名不重复
案例:HashMap的简单使用
1.代码
package org.xiji.collection; import java.util.HashMap; public class MyHashMap { public static void main(String[] args) { System.out.println("HashMap的api简单使用"); System.out.println("HashMap的初始化"); String three = "星期三"; HashMap<String,String> strings = new HashMap<>(); //添加星期的名字 strings.put("星期一","一"); strings.put("星期二","二"); strings.put("星期三","三"); //输出 System.out.println(strings.toString()); System.out.println("通过key获取value"); String s = strings.get("星期三"); System.out.println(s); System.out.println("通过key删除value"); strings.remove("星期三"); System.out.println(strings.toString()); System.out.println("HashMap的遍历"); for (String string : strings.keySet()) { System.out.println(string); } System.out.println("HashMap的添加"); strings.put("星期三",three); System.out.println(strings.toString()); System.out.println("HashMap的修改"); strings.put("星期三","四"); System.out.println(strings.toString()); //添加星期三 strings.put("星期三","三"); System.out.println(strings.toString()); System.out.println("可以看到星期三中的值为三,相当于把四给覆盖了"); } }
2.效果
7.TreeMap集合
TreeMap
类似于HashMap
,但它根据键的自然顺序或者通过自定义比较器进行排序。- 特点:适用于需要维持键的排序的情况;性能略低于
HashMap
,特别是当涉及到大量的插入和删除操作时。
案例:TreeMap的简单使用
1.代码
package org.xiji.collection; import java.util.TreeMap; public class MyTreeMap { public static void main(String[] args) { System.out.println("TreeMap的api简单使用"); System.out.println("TreeMap的初始化"); String three = "星期三"; TreeMap<String,String> strings = new TreeMap<>(); //添加星期的名字 strings.put("星期一","一"); strings.put("星期二","二"); strings.put("星期三","三"); //输出 System.out.println(strings); System.out.println("TreeMap的删除"); strings.remove("星期三"); System.out.println(strings); System.out.println("TreeMap的修改"); strings.put("星期一","一"); System.out.println(strings); System.out.println("TreeMap的遍历"); for(String key:strings.keySet()) { System.out.println(key+"-->"+strings.get(key)); } System.out.println("TreeMap的添加"); strings.put("星期三",three); System.out.println(strings); } }
2.效果
8.Properties集合
Properties
继承自Hashtable
,用来处理配置文件,主要用于保存键值对形式的属性列表。- 特点:适合读写.properties格式的文件;键和对应的值都应该是字符串类型;提供了方便的方法来加载和存储属性列表。
案例:Properies的简单使用
1.代码
package org.xiji.collection; import java.util.Properties; public class MyProperties { public static void main(String[] args) { System.out.println("Properties的简单使用"); System.out.println("Properties的初始化"); String three = "星期三"; Properties properties = new Properties(); properties.put("星期一","一"); properties.put("星期二","二"); properties.put(three,"三"); System.out.println(properties.toString()); System.out.println("通过key获取value"); System.out.println(properties.getProperty("星期一")); System.out.println("通过key获取value,如果key不存在,则返回默认值"); System.out.println(properties.getProperty("星期四","不存在")); System.out.println("通过key删除value"); properties.remove("星期三"); System.out.println(properties.toString()); System.out.println("Properties的遍历"); for (Object key : properties.keySet()) { System.out.println(key); } } }
2.效果
原文地址:https://blog.csdn.net/2301_76862031/article/details/142793738
免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!