当前位置:网站首页>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]);
}
}
边栏推荐
- Currently, mysql5.6 is used. Which version would you like to upgrade to?
- Global and Chinese market for antiviral coatings 2022-2028: Research Report on technology, participants, trends, market size and share
- UCORE lab8 file system experiment report
- 自动化测试中敏捷测试怎么做?
- China's county life record: go upstairs to the Internet, go downstairs' code the Great Wall '
- Global and Chinese markets of electronic grade hexafluorobutadiene (C4F6) 2022-2028: Research Report on technology, participants, trends, market size and share
- Statistics 8th Edition Jia Junping Chapter 4 Summary and after class exercise answers
- Functions: Finding Roots of equations
- Global and Chinese market of barrier thin film flexible electronics 2022-2028: Research Report on technology, participants, trends, market size and share
- [pointer] the array is stored in reverse order and output
猜你喜欢

数字电路基础(二)逻辑代数

CSAPP Shell Lab 实验报告

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

Es full text index

Description of Vos storage space, bandwidth occupation and PPS requirements

几款开源自动化测试框架优缺点对比你知道吗?
软件测试面试要问的性能测试术语你知道吗?

Daily code 300 lines learning notes day 9

What level do 18K test engineers want? Take a look at the interview experience of a 26 year old test engineer

安全测试入门介绍
随机推荐
Pointer -- eliminate all numbers in the string
Fundamentals of digital circuit (IV) data distributor, data selector and numerical comparator
1. Payment system
The common methods of servlet context, session and request objects and the scope of storing data in servlet.
What are the business processes and differences of the three basic business modes of Vos: direct dial, callback and semi direct dial?
[pointer] find the value of the largest element in the two-dimensional array
ByteDance ten years of experience, old bird, took more than half a year to sort out the software test interview questions
[HCIA continuous update] working principle of static route and default route
Sorting odd and even subscripts respectively for leetcode simple problem
C language learning summary (I) (under update)
Expanded polystyrene (EPS) global and Chinese markets 2022-2028: technology, participants, trends, market size and share Research Report
Cc36 different subsequences
Logstack introduction and deployment -- elasticstack (elk) work notes 019
Which version of MySQL does php7 work best with?
Build your own application based on Google's open source tensorflow object detection API video object recognition system (II)
Cadence physical library lef file syntax learning [continuous update]
Oracle foundation and system table
Functions: Finding Roots of equations
[pointer] the array is stored in reverse order and output
Global and Chinese markets of electronic grade hexafluorobutadiene (C4F6) 2022-2028: Research Report on technology, participants, trends, market size and share