自己汇总的,不全面,可能重点也不够突出,参考即好
集合部分
1. ArrayList 集合
ArrayList<Integer> list = new ArrayList<Integer>();
list.add(111);
list.add(new Person(“小名”));
2. 迭代器
集合中把这种取元素的方式描述在Iterator接口中。Iterator接口的常用方法如下:
完成集合的迭代(就是集合的遍历)
Collection<String> list = new ArrayList<String>(); //创建集合
Iterator<String> it = list.iterator(); //遍历集合
while(it.hasNext()){
String s = it.next();
System.out.println(s);
}
3. 增强for循环
增强for循环必须有被遍历的目标只能是Collection集合或者是数组
for(元素的数据类型 变量 : Collection集合or数组){ //格式
}
for(String str : list){ //变量Str代表被遍历到的集合元素
System.out.println(str);
}
4.泛型
泛型限定:
l 格式:? extends E
? 代表接收E类型或者E的子类型的元素
l 格式:? super E
? 代表接收E类型或者E的父类型的元素
举例:
public static void printCollection(Collection<? extends Person> list) {
Iterator<? extends Person> it = list.iterator();
while (it.hasNext()) {
System.out.println(it.next());
}
}
5. list接口
6. LinkedList 集合
LinkedList集合数据存储的结构是链表结构。方便元素添加、删除的集合。
list.push("aaa"); 相当于 // addFirst
String s = list.pop(); 相当于 // removeFirst , 其中s返回值为删除的元素
7. Set接口、HashSet、LinkHashSet集合
特点:1.不能包含重复的元素
2.存储自定义类元素时,类中要重写hashCoad和equals方法!
3.LinkHashSet集合中元素有序;
HashSet集合元素无序;
8. Map集合
方法:
Map集合遍历的两种方式:
Map<String, String> map = new HashMap<String, String>();
①:
Set<String> keys = map.keySet();
//2.遍历键的集合,得到每一个键。 采用循环完成
for (String key : keys) {
//3.通过Map集合get方法,得到当前的键对应的值
String value = map.get(key);
System.out.println(key +"-----"+value);
}
②:
//1. 获取Map集合中所有的Entry(键值对)对象
Set<Entry<String, String>> entrySet = map.entrySet();
//2. 遍历个包含多个Entry对象的Set集合, 得到每一个Entry对象
for (Entry<String, String> entry : entrySet) {
//3. 通过Entry对象, 获取entry对象中的键getKey()、值getValue()
String key = entry.getKey();
String value = entry.getValue();
System.out.println(key+"---"+value);
}
9. Collections集合工具类
(1)Collections.shuffle(list); //随即打乱list集合中的元素
(2)Collections.sort( list ); //对list集合中的元素进行升序排列 |
|