当前位置:网站首页>Collection集合与Map集合
Collection集合与Map集合
2022-07-06 09:25:00 【手可摘鑫晨】
1,Collection集合
Collection<E>:单列集合(接口)
List<E>(接口):
特点:
1,有索引值
2,可以重复
3,有序排列
List<E>接口中的成员方法:
void add(int index,E e);
E remove(int index);
E get(int index);
E set(int index,E e);
List<E> subList(int beginIndex,int endIndex);
int indexOf(Object o);
int lastIndexOf(Object o);
public static void main(String[] args) {
List<String> list = new ArrayList<>();
list.add("aa");
list.add("bb");
list.add("cc");
list.add("dd");
list.add("ee");
list.add(0, "aaa");
System.out.println(list);
String removeElement = list.remove(0);
System.out.println("被删除的元素是:"+removeElement);//被删除的元素是:aaa
System.out.println(list);
String element = list.get(0);
System.out.println(element);//aa
String replaceElement = list.set(0, "aaa");
System.out.println("被替换的元素是:"+replaceElement);//被替换的元素是:aa
System.out.println(list);
List<String> list2 = list.subList(1, 3);//左闭右开
System.out.println(list2);
}实现类:
ArrayList(底层实现是数组)
特点:查询快,增删慢
LinkedList(底层实现是链表)
特点:查询慢,增删快
LinkedList<E>类独有的成员方法:
void addFirst(E e);
void addLast(E e);
String removeFirst();
String removeLast();
String getFirst();
String getLast();
public static void main(String[] args) {
LinkedList<String> list = new LinkedList<>();
list.add("aa");
list.add("bb");
list.add("cc");
list.add("dd");
list.add("ee");
list.addFirst("aaa");
list.addLast("eee");
System.out.println(list);
String first = list.removeFirst();
System.out.println(first);//aaa
String last = list.removeLast();
System.out.println(last);//eee
System.out.println(list);
first = list.getFirst();
System.out.println(first);//aa
last = list.getLast();
System.out.println(last);//ee
}Set(接口):
特点:
1,没有索引值
2,不能重复
实现类:
TreeSet(底层是红黑树,可排序)
注意:TreeSet新增元素的时候,泛型必须是Comparable类型
构造方法:
public TreeSet();
public TreeSet(Comparator<> c);
public class Test06 {
public static void main(String[] args) {
//TreeSet新增元素的时候,泛型必须是Comparable类型
Set<Person> set = new TreeSet<>();
set.add(new Person("张三"));
set.add(new Person("张三疯了"));
set.add(new Person("张三小"));
System.out.println(set);
}
}
class Person implements Comparable<Person>{
private String name;
public Person(String name) {
super();
this.name = name;
}
@Override
public String toString() {
return "Person [name=" + name + "]";
}
//主要要说比较的规则
//升序:当前对象-参数对象
//降序:参数对象-当前对象
@Override
public int compareTo(Person o) {
//升序
int result = this.name.length()-o.name.length();
}
}
public class Test07 {
public static void main(String[] args) {
//创建Comparator类型的对象
Comparator<Teacher> c = new Comparator<Teacher>() {
@Override
public int compare(Teacher t1, Teacher t2) {
// 升序:
// 参数1-参数2
// 降序:
// 参数2-参数1
int result = t2.getName().length()-t1.getName().length();
return result;
}
};
Set<Teacher> set = new TreeSet<>(c);
set.add(new Teacher("张三"));
set.add(new Teacher("张三疯了"));
set.add(new Teacher("张三小"));
System.out.println(set);
}
}
class Teacher{
private String name;
public String getName() {
return name;
}
public Teacher(String name) {
super();
this.name = name;
}
@Override
public String toString() {
return "Teacher [name=" + name + "]";
}
}
HashSet(底层是哈希表,无序)
哈希表:数组+链表+红黑树
注意:如果自定义类型的对象,我们希望添加到HashSet集合中,我们认为成员变量的值相同,就为同一个元素,则需要覆盖重写hashCode方法和equals方法。
LinkedHashSet(底层是链表+哈希表,有序)
2,Collection集合的常用方法
| boolean add(E e); | 新增一个元素 |
| boolean addAll(E e); | 新增多个元素 |
| boolean isEmpty(); | 判断集合是否为空 |
| boolean contains(E e); | 查找元素是否存在 |
| boolean containsAll(E e); | 查找多个元素是否都存在 |
| boolean remove(E e); | 删除该元素 |
| boolean removeAll(E e); | 删除多个元素 |
| int size(); | 集合元素的个数 |
| void clear(); | 清空集合 |
Object[] toArray(); | 将集合转换为数组 |
| Iterator<E> iterator(); | 返回一个Iterator对象,用来遍历集合 |
3,Iterator迭代器
(1)获取迭代器对象
Iterator i = 集合对象.iterator();
(2)判断是否有下一个元素
i.hasNext();
(3)获取元素
i.next();
public static void main(String[] args) {
Collection<String> c = new ArrayList<>();
c.add("张三");
c.add("李四");
c.add("王五");
//获取迭代器对象
Iterator<String> iterator = c.iterator();
while(iterator.hasNext()) {
String element = iterator.next();
System.out.println(element);
}4,增强for循环
介绍:增强for循环的底层就是迭代器。可以遍历集合、数组。
格式:
for(数组的数据类型/集合的泛型 变量名:要遍历的集合/数组){
System.out.println(变量名);
}
5,Collections类
| static <T> boolean addAll(Collection<T> c,T... array); | 添加多个元素到集合中 |
| static void sort(List<> list); | 对集合进行升序排序 |
| static <T extends Comparable<T>> void sort(List<T> list); | 对集合根据Comparable接口中方法进行排序 |
| static <T> void sort(List<T> list,Comparator<T> c); | 对集合根据Comparator接口中方法进行排序 |
| static void shuffle(List<?> list); | 对集合进行乱序排序 |
Collections.reverse(List<T> list); | 对集合进行反序排序 |
6,Map集合
Map<K,V>:双列集合接口
特点:
(1)一个元素是有一个K,一个V两部分组成.
(2)K V可以是任意的引用数据类型。
(3)一个K对应唯一的一个V。K不能重复。
常用实现类:
HashMap implements Map
底层:哈希表
特点:新增顺序和取出顺序不一定一致(无序)
LinkedHashMap extends HashMap
底层:哈希表+链表
特点:新增顺序和取出顺序一致(有序)
TreeMap
底层:红黑树(可排序)
Hashtable:线程安全,效率低
HashMap:线程不安全,效率高
常用方法:
| V put(K k,V v); | 如果K存在,则新的V替换旧的V,返回被替换的V。 如果K不存在,则返回null。 |
| V remove(Object key); | 删除元素,根据key删除整个元素,返回被删除元素的V。 如果Key不存在,则返回null。 |
| V get(Object key); | 根据key获取V,如果key不存在则返回null。 |
| boolean containsKey(Object key); | 如果K存在,返回true。 如果K不存在,返回false。 |
| boolean containsValue(Object value); | 如果V存在,返回true。 如果V不存在,返回false。 |
| boolean isEmpty(); | 判断集合是否为空。 |
| void clear(); | 清空集合 |
| int size(); | 获取集合中元素的个数 |
案例:将双列集合转换为单列集合
public static void main(String[] args) {
Map<String,Double> map = new HashMap<>();
map.put("张三", 90.0);
map.put("张三丰", 99.0);
map.put("张三疯了", 90.0);
//把Map双列集合转换为单列集合
//获取所有的V
Collection<Double> values = map.values();
System.out.println(values);
//获取所有的K
Set<String> set = map.keySet();
System.out.println(set);
for(String key:set) {
//根据key获取V
Double v = map.get(key);
System.out.println(key+"--"+v);
}
}Entry<K,V>接口:
K getKey();
V getValue();
public static void main(String[] args) {
Map<String,Double> map = new HashMap<>();
map.put("张三", 90.0);
map.put("张三丰", 99.0);
map.put("张三疯了", 90.0);
//获取所有的Entry
Set<Entry<String,Double>> entrySet = map.entrySet();
Iterator<Entry<String,Double>> iterator = entrySet.iterator();
while(iterator.hasNext()) {
Entry<String,Double> entry = iterator.next();
String key = entry.getKey();
Double value = entry.getValue();
System.out.println(key+"--"+value);
}
}7,可变参数
定义:
方法名(数据类型... 变量名);
注意事项:
1.一个方法只能有一个可变参数
2.如果有多个参数,可变参数必须是最后一个
public static void main(String[] args) {
int[] array = {10,20,30,40,50};
method(array);
// method(1,2,3,4,5);//1,2,3,4,5
// method();//0
}
public static void method(int... nums) {
System.out.println(nums.length);
//我们就把可变参数作为数组去处理
for (int i = 0; i < nums.length; i++) {
System.out.println(nums[i]);
}
}
边栏推荐
- Nest and merge new videos, and preset new video titles
- [pointer] delete all spaces in the string s
- What are the business processes and differences of the three basic business modes of Vos: direct dial, callback and semi direct dial?
- Emqtt distribution cluster and node bridge construction
- Logstack introduction and deployment -- elasticstack (elk) work notes 019
- “Hello IC World”
- Summary of thread implementation
- Capitalize the title of leetcode simple question
- Function: string storage in reverse order
- Global and Chinese markets of cobalt 2022-2028: Research Report on technology, participants, trends, market size and share
猜你喜欢
![Cadence physical library lef file syntax learning [continuous update]](/img/0b/75a4ac2649508857468d9b37703a27.jpg)
Cadence physical library lef file syntax learning [continuous update]

Logstack introduction and deployment -- elasticstack (elk) work notes 019
自动化测试中敏捷测试怎么做?

The minimum sum of the last four digits of the split digit of leetcode simple problem

Statistics 8th Edition Jia Junping Chapter 4 Summary and after class exercise answers

CSAPP家庭作业答案7 8 9章
![[200 opencv routines] 98 Statistical sorting filter](/img/ba/9097df20f6d43dfce9fc1e374e6597.jpg)
[200 opencv routines] 98 Statistical sorting filter
软件测试方法有哪些?带你看点不一样的东西

The latest query tracks the express logistics and analyzes the method of delivery timeliness

Report on the double computer experiment of scoring system based on 485 bus
随机推荐
Leetcode simple question: check whether two strings are almost equal
如何成为一个好的软件测试员?绝大多数人都不知道的秘密
Global and Chinese markets for complex programmable logic devices 2022-2028: Research Report on technology, participants, trends, market size and share
Fundamentals of digital circuits (I) number system and code system
Pedestrian re identification (Reid) - data set description market-1501
安全测试入门介绍
The latest query tracks the express logistics and analyzes the method of delivery timeliness
Investment should be calm
Video scrolling subtitle addition, easy to make with this technique
Transplant hummingbird e203 core to Da Vinci pro35t [Jichuang xinlai risc-v Cup] (I)
In Oracle, start with connect by prior recursive query is used to query multi-level subordinate employees.
[pointer] use the insertion sorting method to arrange n numbers from small to large
数字电路基础(二)逻辑代数
Report on the double computer experiment of scoring system based on 485 bus
Using flask_ Whooshalchemyplus Jieba realizes global search of flask
Statistics 8th Edition Jia Junping Chapter 1 after class exercises and answers summary
Want to learn how to get started and learn software testing? I'll give you a good chat today
Function: string storage in reverse order
Capitalize the title of leetcode simple question
[oiclass] maximum formula