2020-8-17

Collection  ①List ②Set
Map–>hashMap
Set hashSet
List ->ArrayList LinkedList
ArrayList:连续的可变长度数组可以存任何信息
遍历,随机访问的效率高
LinkList:链表数组,非连续的,非顺序的
更改数组元素效率高
Set:无序的,不可重复的(数据不能相同)

Map:以key –value的形式存储,存储的都是对象

在java5之前存入到容器中的类型都是object增加泛型后,java容器可以记住存放的类型
Comparable comparator用来自动判断set中的元素

Collection接口提供的方法
(1)add(E e): 返回值为bool类型
(2)addAll()
(3)clear();
(4)contains(Object b)
(5)equals()
(6)isEmpty()
(7)iterator() 迭代器:返回迭代器类型
迭代器
hasNext() next() remove()
interator仅用于遍历集合,(实现循环)

一.List
1.list是collection的子接口。
2.用arraylist实例化list接口
//创建一个可变长度的数组
List list1 =new ArrayList();
//添加元素。

	list1.add(1);       //Interger类型
	list1.add("abc");
	list1.add(new demo1());
	//通过指定位置获取元素
	 Object O = list1.get(1);
	 System.out.println(O);
	 Object demo1 =list1.get(2);
	 System.out.println(demo1);
	 
	 //泛型,指定存放类型Integer,String,Person
	 List<Integer> intlist =new ArrayList<Integer>();    //int可变长度数组

	 //在list最前面,放置一个元素
	 intlist.add(0,123);     //可重复
	 //获取某个元素的位置信息
	 int index = intlist.indexOf(123);   //获取123的位置信息并返回int形变量
	 System.out.println(index);

Set集合
Hashset:存储无序的,唯一的对象
遍历方法需要借助interator中的方法进行

在类中判断类中重复重写
public boolean equals(Object obj) {
// obj this 判断
if(obj == this){
return true;
}
if(obj == null){
return false;
}
People person = (People)obj;
if(person.getName().equals(this.getName())&&person.getAge() ==this.getAge())
{return true;}

	return false;
	
	
}
@Override
public int hashCode() {
	// TODO Auto-generated method stub
	return age*12;
}

Set set1 =new HashSet<>();
set1.add(1); //Integer类型
set1.add(“abc”);
set1.add(new demo2());

	set1.add(1);      //再次添加1的时候被set容器识别,此时set容器的长度为3
 	
	set1.add(new demo2()); //此时set容器的长度为4
	
	Set<People> setpeople = new HashSet<People>();
	People people1 = new People("张三",20);
	People people2 = new People("张三",20);
	People people3 = new People("张三",22);
	//需要在peopleset容器中自动识别重复的人  (信息相同)
	setpeople.add(people1);
	setpeople.add(people2);
	setpeople.add(people3);
	
	//需要重写实体类的equals方法和hashcode方法
	System.out.println(setpeople.size());
	
	//删除元素  先获取再删除(不用迭代器)
	
	setpeople.remove(people2);
	System.out.println(setpeople.size());

迭代器:专门是实现集合的遍历

List容器用迭代器实现
①for(int i =0;i <=peopleList.size();i++){
People textforpeople = peopleList.get(i);
}
for(;iterator.hasNext()😉{
People p =iterator.next();
}
② while(iterator.hasNext()){
People p =iterator.next();
}
③ //迭代器的使用
//增强for循环,需要提前指定容器类型
for (People people:peopleList){
System.out.println(people.getName());
System.out.println(people.getAge());
}
Set容器用迭代器实现因为set是无序的所以不能使用get方法获取下标进行迭代
①获取迭代器 iterator iter = peopleset.iterator();
//for while
For(people tmpp:personset){
System.out.print(tmpp.getName()+temp.getAge())
}

操作集合的工具类collections
因为是静态类,使用时直接Collections.方法名 进行调用
Reverse:反转元素的位置
Shuffle: 随机排序打乱原来元素顺序
*如果list中的元素是对象,如何进行排序??
//sort排序:取首字母进行排序
//交换 swap collections.swap(list,1,2) //对list的1,2进行交换
*如果list中的元素是对象,如何进行排序??
排序时,不支持类排序的方式

Arraylist和list之间的转换
把数组转化为arraylist
①空容器(ArrayList),遍历数组并赋值
把arraylist转化为数组
List.toarray();

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值