JAVA高级---集合

JAVA高级—集合

一、集合

1.对象的容器,可实现数组的功能。

2.集合长度不固定,可以存储为引用类型

二、Collection体系集合

list接口:arraylist,linkedlist

set接口:hashset,treeset

list有序有下标可重复,set无序无下标不可重复

三、Collection接口使用

1.遍历集合

-增强for

-迭代器 iterator

Iterator it=Collection.Iterator();

while(it.hasnext()){

Student s=(Student)it.next();

system.out printlin(s.tostring());

}

2.list接口使用

遍历还有使用ListIterator的方法

Iterator lit=ListIterator.Iterator();

while(lit.hasnext()){

Student s=(Student)lit.next();

system.out printlin(s.tostring());

}

list.remove((object)20) 删除值为20的元素

四、List实现类

1.arraylist 查询快,增删慢

2.Linkedlist 查询慢,增删快

Arraylist

1.add()

2.remove(s1) 删除对象

3.默认元素数量容量为0

LinkedList

1.add()

2.remove(s1) 删除对象

3.默认元素数量容量为0

4.clear();

四、Set实现类

HashSet

HashSet<Person> persons=new HashSet<>();
Person p1=new Person("xiaohonf",20);
Person p2=new Person("lili",21);
Person p3=new Person("niujin",22);
persons.add(p1);
persons.add(p2);
persons.add(p3);
persons.add(new Person("niujin",22));

目标希望add相同的元素的无法成功

重写hascode()和equals()

有快捷方法,genrate

TreeSet

1.无重复的集合

2.comparator接口

五、泛型集合

参数化类型、类型安全的集合,强制类型元素一致

Iterator lit=ListIterator.Iterator();

while(lit.hasnext()){

Student s=lit.next();

system.out printlin(s.tostring());

}

六、Map集合

1.包括hashmap和treemap,包括的都是键值对,而不是值,map集合的键不允许重复,无序,无下标

2.常用方法

clear()

put() 添加元素

containskey()

containsvalue()

entryset() 可以用该方法遍历,返回entry映射键值对(效率高)

Set<Map.Entry<String,String>> entries=map.entrySet();然后使用增强for,entry.getKey(),entry.getValues();

keyset() 返回所有key的set集合,可以用增强for和iterator遍历

Hashmap

和map方法一致

遍历方法:1.增强for

HashMap<Student,String> students=new HashMap<Student,String>();
for(Student key:students.keySet()){
  system.out println(key.tostring()+"===="+students.get(key));
}

2.entryset

HashMap<Student,String> students=new HashMap<Student,String>();
for(Map.Entry<Student,String> entry:students.entrySet()){
  system.out println(entry.getKey()+"===="+entry.getValue());
}

hashtable线程安全,比hashmap快

Properties要求键值都是String,和流类来搭配用

七、Collections工具类

1.collections.sort(list)

2.binarySearch(list,10)

3.reverse()

4.copy()需要扩充size

5.shuffle() 打乱

6.list转为数组 list.toArray()

数组转为集合 Arrays.asList()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值