Java集合的初识

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档


前言

Java中,采用数组存储不固定个数的元素的话,当元素个数超过数组长度后,就不得不创建新的数组来存储元素,因此我们就急需寻找另一种容器来代替数组存储不定量的元素,也就是集合。


一、集合是什么?

集合是一种可变长度的容器,当长度超出原本容量时候,集合会自动进行扩容操作而无需我们进行手动的扩容,无需担心容量的问题,从而让我们更专心于业务。

二、数组与集合的区别

1. 存储数据的不同

数组既可以存储基本数据类型,又可以存储引用数据类型,基本数据类型存储的是值,引用数据类型存储的是地址。

集合只能存储引用数据类型,存储的是对象的引用,即地址,若是要存放基本数据类型会发生自动装箱变成包装类

2. 长度不同

数组的长度是固定的,不能自动增长(所以元素个数固定的时候,推荐使用数组)

集合的长度是可变的,可以根据元素的增加而增长(如果元素的个数不固定,推荐使用集合)

3. 保存数据的结构不同

数组 – 采用数组这个基本的结构保存数据

集合 – 集合中存在多种不同的保存数据的结构,有数组,链表,红黑树,队列等等

三、集合的分类

Collection集合

1. 定义

单列集合,直接存储对象的引用,用于存储单个元素

Collection是一个接口,他的下面存在多个子接口以及实现类(如List,Set,Queue),统称为Collection体系

2. 分类

List集合
List集合中的元素是有序的,可重复的

Queue队列
除优先级外,保持先进先出的原则

Set集合
Set集合中的元素是无序的,并且不能存放重复的元素

3. 体系图

在这里插入图片描述

Map集合

1. 定义

双列集合,以键值对的形式存在,将键和值捆绑到一起进行存放(Key,Value)

Map集合(映射)中不能含有重复的键,若出现重复的键,新的值会覆盖旧的值。

每个键最多只能映射到一个值。

2. Map接口与Collection接口的不同

Map是双列的,而Collection是单列的。

Map集合的数据结构只针对键,和值无关,Collection集合的数据结构只针对元素有效。

四、集合的简单使用

单个集合里面的简单操作

// 创建一个名为collection的集合
Collection collection = new ArrayList();

// 向集合中添加数据
// 添加数据会返回boolean类型数据,表示是否添加成功
bolean add = collection.add("苹果"); // true
collection.add("火龙果");

// 打印集合
System.out.println(collection);

// 删除集合中的指定元素
// 删除数据会返回boolean类型的数据,表示是否删除成功
// 当指定元素不在集合中,则会删除失败
boolean remove = collection.remove("小苹果"); // false

// 获取集合中的元素个数
int size = collection.size();
System.out.println(size);

// 查看集合中是否包含指定的元素
boolean contains = collection.contains("苹果");
System.out.println(contains);

// 查看集合是否为空
boolean empty = collection.isEmpty();
System.out.println(empty);

// 清空集合
collection.clear();

集合间的简单操作

// 创建两个集合
Collection c1 = new ArrayList();
Collection c2 = new ArrayList();

// c1填充数据
c1.add(1);
c1.add(2);
c1.add(3);
c1.add(4);

// c2填充数据
c2.add(2);
c2.add(3);
c2.add(5);

// 将c2中的所有元素添加到c1后面
c1.addAll(c2);  
System.out.println(c1);	// [1, 2, 3, 4, 2, 3, 5]

// 通过c1创建新的集合c3
Collection c3 = new ArrayList(c1);
System.out.println(c3); // [1, 2, 3, 4, 2, 3, 5]

// 将c2中的元素,从c1中删除
// 删除的元素既在c1中,也在c2中
c1.removeAll(c2);
System.out.println(c1); // [1, 4]

// 在c3中保留c2的元素
// 删除的元素是只在c3中,不在c2中的
c3.retainAll(c2);
System.out.println(c3); // [2, 3, 2, 3, 5]

// 判断c3中是否包含c2中的所有元素,缺一不可
boolean b = c3.containsAll(c2);
System.out.println(b); // true

集合获取元素(迭代器)

// 创建集合
Collection c = new ArrayList();

// 向集合添加数据
c.add("1");
c.add("2");
c.add("3");
c.add("4");

// 数组获取元素借助下标完成
// Collection没有下标,借助工具Iterator一个个获取
// 创建迭代器对象
Iterator iterator = c.iterator();

// 依次获取数据
while (iterator.hasNext()) {
	Object next = iterator.next();
	if ("2",equals(next)) {
	// 删除2这个元素
	// 注意,此处不能采用c.remove(next)这个方法,会产生并发异常
	// 因为 迭代器中会存储一个变量记录迭代器中修改次数
	// 集合中也会存在一个变量,记录集合中修改次数
	// 每次进行数据获取的时候,会校验两个变量,不一致则会报错,并发修改异常
		iterator.remove();
	}
	System.out.println(next);
}

// 迭代器简写形式 -- 增强for(forEach循环)
for (Object o : c) {
	System.out.println(o);
}

// forEach方法
c.forEach((Object o) -> {
	System.out.println(o);
});

// 简写
c.forEach(o -> System.out.println(o));

// 再简写
c.forEach(System:out::println);
Multi-objective evolutionary federated learning (MEFL) is a machine learning approach that combines the principles of multi-objective optimization and federated learning. Multi-objective optimization is a technique that aims to optimize multiple objectives simultaneously, while federated learning is a decentralized machine learning approach that allows multiple devices to train a model collaboratively without sharing their data. MEFL is designed to overcome the limitations of traditional federated learning approaches, which often suffer from issues related to privacy, communication, and scalability. By using multi-objective optimization, MEFL can optimize the performance of the federated learning algorithm while also addressing these issues. MEFL works by dividing the optimization problem into multiple objectives, such as minimizing the loss function, reducing communication costs, and preserving privacy. A genetic algorithm is then used to optimize these objectives simultaneously, producing a set of Pareto-optimal solutions that represent the trade-offs between the different objectives. These Pareto-optimal solutions can then be used to select the best model for deployment, depending on the specific requirements of the application. MEFL has been shown to be effective in a wide range of applications, including image classification, natural language processing, and speech recognition. Overall, MEFL represents a promising approach to federated learning that can improve the privacy, communication, and scalability of the algorithm while also optimizing its performance.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值