List,Set,Map接口

本文深入解析Java集合框架,包括Collection和Map的子类特性,如ArrayList、LinkedList、HashSet、TreeSet、HashMap和TreeMap的存储方式及常用方法,强调了不同集合在数据操作上的优势。

集合框架

各种数据结构封装类


数据结构的类型:
1.连续存储
2.链式(非连续)


java集合框架有两大类:
1.Collection(一个元素只存储一个对象)

list接口继承Collection接口
Set接口继承Collection接口

2.Map(一个元素存储key-value两个对象)

HashMap直接实现Map接口
TreeMap直接实现Map接口


List接口

List接口的两个实现类分别是ArrayListLinkedList


ArrayList

以动态数组的形式存储数据。
声明:ArrayList<E> arraylist = new ArrayList<E>();

其中E是一种泛型,表示某一种类型。可以是String,也可以是自定类型,看其中存储的数据类型,【但是不能为int和char,只能是它们的包装类:Integer和Character】

数组
ArrayList和Array的存储形式一致,因为ArrayList的底层实现就是使用Arrays.copyof().

这样的存储形式,保证了数据的有序性。便于查找,但是不便于添加,删除等操作。

而ArrayList和Array不同的是,它有自己的方法。用于操作数组。

ArrayList的常用方法

1:ArrayList.add(E e)
返回boolean类型,添加一个元素在列表末尾。

2: ArrayList.add(int index, E e);
返回void类型,在指定索引位置添加元素

3:ArrayList.addAll(Collection<? extends E> c);
返回boolen类型,将指定集合追加到列表的末尾

4:ArrayList.clear();
返回void类型,清除列表中的所有元素

5:ArrayList.get(int index);
返回E类型,通过指定索引返回指定索引元素。

6:ArrayList.isEmpty()
返回Boolean类型。判断列表中是否存在元素,Ture为不存在

7:ArrayList.remove(int index);
返回E类型,通过索引删除指定索引的元素。

8:ArrayList.remove(Object o)
返回boolean类型。通过匹配元素,删除匹配到的第一个元素。

9:ArrayList.set(int index, E element)
返回E类型。用指定元素替换指定索引位置的元素

10:ArrayList.size()
返回int类型,返回列表的元素个数。

LinkedList

以链表的形式存储数据
声明:LinkedList<E> linkedlist = new LinkedList<E>();
在这里插入图片描述

如上图所示,LinkedList是以链表的形式来存储数据的,和ArrayList不同的是:LinkedList方便添加、删除等操作,但是不便于遍历和查找。

在这里插入图片描述

从LinkedList的源码中可以看出它的成员变量有Node<E> firstNode<E> last
表示LinkedList的底层实现,是通过Node[节点]。
而通过节点来保存数据的话,只需要知道上一个节点和下一个节点。

故LinkedList方便于添加和删除,只需要改变上节点和下节点,不需要和ArrayList一样,要把每一个元素后移或前移。


Set接口

Set接口的两个实现类分别是HashSettreeSet

Set和List的区别在于,Set是无序无重复的。


HashSet

使用HashSet存储数据和List一致,只是不会存储重复数据。

News news1 = new News(1,"news1");
News news2 = new News(2,"news1");
News news3 = new News(3,"news1");
News news4 = new News(4,"news1");
Set<News> list = new HashSet<News>();
list.add(news1);
list.add(news3);
list.add(news2);
list.add(null);
list.add(news4);
System.out.println(list);

当用HashSet添加数据后,在输出列表,会发现数据是无序的。【】

TreeSet

TreeSet和HashSet的区别:TreeSet会按照自然规律或者重写的compare()方法排序

TreeSet<String> list = new TreeSet<String>();
list.add("1111");
list.add("2222");
list.add("4444");
list.add("7777");
list.add("6666");
list.add("9999");
list.add("3333");
list.add("8888");
System.out.println(list);

运行后,会发现输出列表的顺序都是排列好的。

当存储的数据类型为自定义类型时。需要实现比较器的接口,重写compare()方法。
方法1:自己新建一个类实现Comparator接口。然后实例化。传给TreeSet。

public class NewsComparator implements Comparator<News> {
	@Override
	public int compare(News o1, News o2) {
		return o1.getId() - o2.getId();
	}
}
News news1 = new News(1,"news1");
News news2 = new News(2,"news2");
News news3 = new News(3,"news3");
News news4 = new News(4,"news4");
NewsComparator nc = new NewsComparator();
TreeSet<News> list = new TreeSet<News>(nc);
list.add(news1);
list.add(news3);
list.add(news4);
list.add(news2);

方法2:直接在TreeSet中写匿名内部类

News news1 = new News(1,"news1");
News news2 = new News(2,"news2");
News news3 = new News(3,"news3");
News news4 = new News(4,"news4");
TreeSet<News> list = new TreeSet<News>(
	new Comparator<News>() {
	@Override
	public int compare(News o1, News o2) {
		return o1.getId() - o2.getId();
	}
});
list.add(news1);
list.add(news3);
list.add(news4);
list.add(news2);

方法3:用lambda表达式写匿名内部类

News news1 = new News(1,"news1");
News news2 = new News(2,"news2");
News news3 = new News(3,"news3");
News news4 = new News(4,"news4");
TreeSet<News> list = new TreeSet<News>((o1,o2)->o2.getId() - o1.getId());
list.add(news1);
list.add(news3);
list.add(news4);
list.add(news2);

Map

使用key-value【键值对】的方式存储数据的结构

Map的两个实现类:
1.HashMap
2.TreeMap

Map的存储结构图
在这里插入图片描述
采用键值对的形式,每一个key对应一个value,因为map中的key是以Set集合存储的,故Map的存储也是无序的。

Map的声明:
1.Map<Integer,String> map = new HashMap<Integer, String>();
2.Map<Integer,String> map = new TreeMap<Integer, String>();

Map中的常用方法:
1.map.clear();					//清除map中的所有映射【即所有元素】
2.map.containsKey(key);			//判断map中是否存在该key
3.map.containsValue(value)		//判断map中是否存在该value
4.map.forEach();					//遍历map
5.map.get(key);					//获取该key的value
6.map.isEmpty();					//判断map是否包含数据
7.map.keySet();					//返回key的Set集合
8.map.remove(key)				//移除某个key和所对应的value
9.map.replace(key,value)			//替换key所对应的value
10.map.size()					//返回map的键值对数目
11.map.put(key,value)			//在map中添加一个键值对,如果key重复,将替换之前的value
12.map.putAll(Map)				//将另一个Map添加到map中,如果key重复,将替换。

HashMap和TreeMap的区别:
TreeMap和TreeSet的一样,都可以通过实现比较器接口来排序的。


<think> 我们被问到:List, Set, Map 是否是接口? 回答:是的,ListSetMap都是Java中的接口。 详细解释: 在Java集合框架中,ListSetMap是三个核心接口,它们定义了一组通用的方法,具体的实现由各个类来完成。 1. List接口:代表有序、可重复的集合。 常见实现类:ArrayList, LinkedList, Vector 2. Set接口:代表无序、不可重复的集合。 常见实现类:HashSet, LinkedHashSet, TreeSet 3. Map接口:代表键值对的映射集合(注意:Map并不继承自Collection接口,它是一个独立的接口)。 常见实现类:HashMap, LinkedHashMap, TreeMap, Hashtable 我们可以通过代码来验证: ```java import java.util.List; import java.util.Set; import java.util.Map; public class CollectionInterfacesCheck { public static void main(String[] args) { // 验证ListSetMap接口 System.out.println("List is interface: " + List.class.isInterface()); System.out.println("Set is interface: " + Set.class.isInterface()); System.out.println("Map is interface: " + Map.class.isInterface()); } } ``` 运行结果: ``` List is interface: true Set is interface: true Map is interface: true ``` 解释: 上述代码使用反射机制检查ListSetMap是否为接口,输出结果均为true,证明它们都是接口。 另外,我们可以查看Java API文档: - List: https://docs.oracle.com/javase/8/docs/api/java/util/List.html - Set: https://docs.oracle.com/javase/8/docs/api/java/util/Set.html - Map: https://docs.oracle.com/javase/8/docs/api/java/util/Map.html 在文档中,可以看到它们都被明确声明为接口(interface)。 总结:ListSetMap都是接口,它们定义了集合的基本操作,具体的实现由具体的类来完成。 §§相关问题§§: 1. Java集合框架中,List接口的主要实现类有哪些? 2. Set接口List接口的主要区别是什么? 3. Map接口与Collection接口有什么关系? 4. 如何选择使用ListSetMap? </think> 是的,**ListSetMap 都是 Java 中的接口**,而不是具体实现类。它们在 Java 集合框架中扮演核心角色,定义了集合的基本行为规范。 ### 详细解释: #### 1. 三者都是接口 ```java // Java 集合框架核心接口定义 public interface List<E> extends Collection<E> { /* ... */ } // 有序、可重复集合 public interface Set<E> extends Collection<E> { /* ... */ } // 无序、不可重复集合 public interface Map<K,V> { /* ... */ } // 键值对映射 ``` #### 2. 接口与实现类的关系 | 接口 | 常见实现类 | 特点 | |------|------------|------| | `List` | `ArrayList`, `LinkedList`, `Vector` | 有序集合,允许重复元素 | | `Set` | `HashSet`, `TreeSet`, `LinkedHashSet` | 唯一元素集合,无重复 | | `Map` | `HashMap`, `TreeMap`, `LinkedHashMap` | 键值对映射,键唯一 | #### 3. 编程实践:使用接口声明变量 ```java import java.util.*; public class CollectionExample { public static void main(String[] args) { // 使用接口类型声明变量 ✅ 良好实践 List<String> names = new ArrayList<>(); names.add("Alice"); names.add("Bob"); Set<Integer> uniqueIds = new HashSet<>(); uniqueIds.add(101); uniqueIds.add(102); Map<String, Integer> scores = new HashMap<>(); scores.put("Alice", 95); // 直接使用实现类声明 ❌ 不够灵活 ArrayList<String> names2 = new ArrayList<>(); } } ``` #### 4. 接口的核心方法 ```java // List 核心方法 void add(int index, E element); E get(int index); // Set 核心方法 boolean add(E e); // 添加元素(自动去重) // Map 核心方法 V put(K key, V value); // 添加键值对 V get(Object key); // 根据键获取值 ``` ### 为什么设计成接口? 1. **解耦设计**:接口定义规范,实现类提供具体方案 ```java // 可灵活切换实现而不影响业务逻辑 List<String> data = new ArrayList<>(); // 改为:List<String> data = new LinkedList<>(); ``` 2. **多态支持**:同一接口不同实现 ```java void processList(List<String> list) { // 可接受任何List实现 } ``` 3. **标准化操作**:所有实现类遵循相同的方法签名 ### 类型体系总结 ```mermaid classDiagram class Collection~E~ { <<interface>> +add() +remove() } class List~E~ { <<interface>> +get() } class Set~E~ { <<interface>> } class Map~K,V~ { <<interface>> +put() +get() } Collection <|-- List Collection <|-- Set List <|.. ArrayList List <|.. LinkedList Set <|.. HashSet Set <|.. TreeSet Map <|.. HashMap Map <|.. TreeMap ``` ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值