0
点赞
收藏
分享

微信扫一扫

Java集合框架及背后的数据结构

大柚子top 2022-05-05 阅读 47

本节目标


        

1.介绍


2.意义


2.1 Java 集合框架的优点及作用

         使用成熟的集合框架,有助于我们便捷、快速的写出高效、稳定的代码

         学习背后的数据结构知识,有助于我们理解各个集合的优缺点及使用场景

2.2 笔试及面试题

3.接口 interfaces

1. Set : 元素不能重复,背后隐含着 查找 / 搜索 的语义

1. SortedSet : 一组有序的不能重复的元素

2. List : 线性结构

3. Queue : 队列

4. Deque : 双端队列

2. Map : 键值对 Key - Value - Pair ,背后隐含着 查找 / 搜索 的语义

1. SortedMap : 一组有序的键值对

3.1基本关系说明

3.2 Collection 接口说明

3.3 Collection 常用方法说明

修饰符和类型方法和说明
booleanadd(E e)

确保此集合包含指定的元素(可选操作)。

booleanaddAll(Collection<? extends E> c)

将指定集合中的所有元素添加到此集合(可选操作)。

voidclear()

从此集合中删除所有元素(可选操作)。

booleancontains(Object o)

如果此集合包含指定的元素,则返回 true。

booleancontainsAll(Collection<?> c)

如果此集合包含指定集合中的所有元素,则返回 true。

booleanequals(Object o)

将指定的对象与此集合进行比较以实现相等性。

inthashCode()

返回此集合的哈希代码值。

booleanisEmpty()

如果此集合不包含任何元素,则返回 true。

Iterator<E>iterator()

返回此集合中元素的迭代器。

default Stream<E>parallelStream()

返回与此集合可能并行的集合作为其源。Stream

booleanremove(Object o)

从此集合中删除指定元素的单个实例(如果存在)(可选操作)。

booleanremoveAll(Collection<?> c)

删除此集合的所有元素,这些元素也包含在指定的集合中(可选操作)。

default booleanremoveIf(Predicate<? super E> filter)

删除此集合中满足给定谓词的所有元素。

booleanretainAll(Collection<?> c)

仅保留此集合中包含在指定集合中的元素(可选操作)。

intsize()

返回此集合中元素的数目。

default Spliterator<E>spliterator()

在此集合中的元素上创建拆分器。

default Stream<E>stream()

返回以此集合作为其源的顺序。Stream

Object[]toArray()

返回一个数组,其中包含此集合中的所有元素。

<T> T[]toArray(T[] a)

返回一个数组,其中包含此集合中的所有元素;返回数组的运行时类型是指定数组的运行时类型。

3.4 Colllection 示例

/**
* Created with IntelliJ IDEA.
* Description:
* User: 星有野
* Date: 2022-05-05
* Time: 12:44
*/

import java.util.Collection;
import java.util.ArrayList;
import java.util.Arrays;

public class TestDemo {
public static void main(String[] args) {
Collection<String> List = new ArrayList<>();
System.out.println(List.size());
System.out.println(List.isEmpty());
List.add("我");
List.add("爱");
List.add("皮皮");
System.out.println(List.size());
System.out.println(List.isEmpty());
Object[] array = List.toArray();
System.out.println(Arrays.toString(array));
for (String s : List) {
System.out.println(s);
}
List.remove("爱");
for (String s : List) {
System.out.println(s);
}
List.clear();
System.out.println(List.size());
System.out.println(List.isEmpty());

}



}



 

3.5 Map 接口说明

Map (Java Platform SE 8 ) (oracle.com)

3.6 Map 常用方法说明

修饰符和类型方法和说明
voidclear()

从此映射中删除所有映射(可选操作)。

default Vcompute(K key, BiFunction<? super K,? super V,? extends V> remappingFunction)

尝试计算指定键及其当前映射值的映射(如果没有当前映射)。null

default VcomputeIfAbsent(K key, Function<? super K,? extends V> mappingFunction)

如果指定的键尚未与某个值关联(或映射到 ),则尝试使用给定的映射函数计算其值并将其输入到此映射中,除非 。nullnull

default VcomputeIfPresent(K key, BiFunction<? super K,? super V,? extends V> remappingFunction)

如果指定键的值存在且不为 null,则尝试在给定键及其当前映射值的情况下计算新的映射。

booleancontainsKey(Object key)

如果此映射包含指定键的映射,则返回 true。

booleancontainsValue(Object value)

如果此映射将一个或多个键映射到指定值,则返回 true。

Set<Map.Entry<K,V>>entrySet()

返回此映射中包含的映射的 Set 视图。

booleanequals(Object o)

将指定的对象与此映射进行比较以获得相等性。

default voidforEach(BiConsumer<? super K,? super V> action)

对此映射中的每个条目执行给定的操作,直到所有条目都已处理完毕或该操作引发异常。

Vget(Object key)

返回指定键映射到的值,或者如果此映射不包含键的映射。null

default VgetOrDefault(Object key, V defaultValue)

返回指定键映射到的值,或者如果此映射不包含键的映射。defaultValue

inthashCode()

返回此映射的哈希代码值。

booleanisEmpty()

如果此映射不包含键值映射,则返回 true。

Set<K>keySet()

返回此映射中包含的键的 Set 视图。

default Vmerge(K key, V value, BiFunction<? super V,? super V,? extends V> remappingFunction)

如果指定的键尚未与某个值关联或与 null 相关联,请将其与给定的非空值相关联。

Vput(K key, V value)

将指定的值与此映射中的指定键相关联(可选操作)。

voidputAll(Map<? extends K,? extends V> m)

将所有映射从指定映射复制到此映射(可选操作)。

default VputIfAbsent(K key, V value)

如果指定的键尚未与某个值关联(或映射到),则将其与给定值相关联并返回 ,else 返回当前值。nullnull

Vremove(Object key)

从此映射中删除键的映射(如果存在)(可选操作)。

default booleanremove(Object key, Object value)

仅当指定键当前映射到指定值时,才删除该项的条目。

default Vreplace(K key, V value)

仅当指定键当前映射到某个值时,才替换该项的条目。

default booleanreplace(K key, V oldValue, V newValue)

仅当当前映射到指定值时,才替换指定键的条目。

default voidreplaceAll(BiFunction<? super K,? super V,? extends V> function)

将每个条目的值替换为在该条目上调用给定函数的结果,直到所有条目都已处理完毕或函数引发异常。

intsize()

返回此映射中键值映射的数目。

Collection<V>values()

返回此映射中包含的值的“集合”视图。

3.7 Map 示例



public class TestDemo {

public static void main(String[] args) {
TreeMap<String,String> map2 = new TreeMap<>();
map2.put("索尼","佳能");
map2.put("女神","皮皮");
System.out.println(map2);

HashMap<String,String> map = new HashMap<>();
map.put("索尼","佳能");
map.put("女神","皮皮");
System.out.println(map);
}

public static void main4(String[] args) {
Map<String,String> map = new HashMap<>();
map.put("索尼","佳能");
map.put("女神","皮皮");
System.out.println(map);
System.out.println("====================");

Set<Map.Entry<String, String>> entrySet = map.entrySet();
for( Map.Entry<String, String> entry : entrySet) {
System.out.println("key: "+entry.getKey()+" value:"+entry.getValue());
}

}
public static void main3(String[] args) {

Map<String,String> map = new HashMap<>();
map.put("女神","皮皮");
map.put("索尼","佳能");

String ret = map.getOrDefault("索尼","佳能");
System.out.println(ret);
boolean flg = map.containsKey("女神2");
System.out.println(flg);

Map<String,String> map2 = new TreeMap<>();

}
public static void main2(String[] args) {
Collection<String> collection = new ArrayList<>();
collection.add("hello");
collection.add("hello2");
//System.out.println(collection);
Object[] objects = collection.toArray();
System.out.println(Arrays.toString(objects));
/*collection.clear();
System.out.println("===================");
System.out.println(collection);
System.out.println(collection.isEmpty());*/

}

public static void main1(String[] args) {
Collection<String> collection = new ArrayList<>();
collection.add("hello");
collection.add("hello2");
//collection.add(1);
//尖括号当中 放的类型 一定要是 类类型 不能是简单的基本类型
Collection<Integer> collection2 = new ArrayList<>();
collection2.add(1);
collection2.add(2);
collection2.add(13);

}
}


 

4.实现 classes


interface顺序表链表红黑树哈希表
SetTreeSetHashSet
ListArrayListLinkedList
QueueLinkedListpriorityQueue
DequeLinkedList
MapTreeMapHashMap

5.知识点框架


1. 集合框架的使用

1. Collection

2. List

3. ArrayList

4. LinkedList

5. Stack

6. Queue

7. PriorityQueue

8. Deque

9. Set

10. HashSet

11. TreeSet

12. Map

13. HashMap

14. TreeMap

15. Collections

6.内容总结


举报

相关推荐

数据结构-集合

0 条评论