欢迎进入JAVA基础课程
博客地址:https://blog.csdn.net/houjiyu...
本系列文章将主要针对JAVA一些基础知识点进行讲解,为平时归纳所总结,不管是刚接触JAVA开发菜鸟还是业界资深人士,都希望对广大同行带来一些帮助。若有问题请及时留言或加QQ:243042162。
寄语:
再走长征路,回顾过往峥嵘岁月,重温重要历史事件,砥砺前行,用脚步丈量新时代的长征路。工作道路上,我们也要弘扬这种长征精神,坚持不懈,一步一个脚印,脚踏实地,朝着自己的目标前行。
集合
1. 集合框架图
(1)缩略版
(2)详细版
2.集合和数组区别
3.Collection接口
List接口:元素按进入先后有序保存,可重复
(1)LinkedList:底层数据结构是链表,查询慢,增删快,线程不安全,效率高,可以存储重复元素
(2) ArrayList:底层数据结构是数组,查询快,增删慢,线程不安全,效率高,可以存储重复元素
(3) Vector:底层数据结构是数组,查询快,增删慢,线程安全,效率低,可以存储重复元素
Set 接口: 仅接收一次,不可重复,并做内部排序
- HashSet 使用hash表(数组)存储元素
LinkedHashSet 链表维护元素的插入次序 - TreeSet 底层实现为二叉树,元素排好序
- HashSet 使用hash表(数组)存储元素
HashSet和TreeSet区别:
(1)Treeset 中的数据是自动排好序的,不允许放入 null 值。
(2)HashSet 中的数据是无序的,可以放入 null,但只能放入一个 null,两者中的值都不能重复,就如数据库中唯一约束。
(3)HashSet 要求放入的对象必须实现 HashCode()方法,放入的对象,是以 hashcode 码作为标识的,而具有相同内容的 String 对象,hashcode 是一样,所以放入的内容不能重复。但是同一个类的对象可以放入不同的实例
4.Map
- HashMap和HashTable
HashMap 和 Hashtable 都实现了 Map 接口,因此很多特性非常相似。但是,他们有以下不同点:
(1)HashMap 允许键和值是 null,而 Hashtable 不允许键或者值是 null。
(2)Hashtable 是同步的,而 HashMap 不是。因此,HashMap 更适合于单线程环境,而 Hashtable 适合于多线程环境。
- TreeMap
5.集合遍历
(1)list遍历
public class CollectionMain { public static void main(String[] args) { List<String> testList = new ArrayList<>(); testList.add("1"); testList.add("2"); testList.add("3"); System.out.println("使用Iterator迭代....."); Iterator<String> iterator = testList.iterator(); while (iterator.hasNext()){ String value = iterator.next(); System.out.printf("%s ",value); } //在使用ListIterator迭代时,开始也需要正向迭代,然后在倒序迭代 System.out.println("\n\n使用ListIterator迭代....."); System.out.println("正向遍历....."); ListIterator<String> listIterator = testList.listIterator(); while (listIterator.hasNext()){ String value = listIterator.next(); System.out.printf("%s ",value); } System.out.println("\n反向遍历....."); while (listIterator.hasPrevious()){ String value = listIterator.previous(); System.out.printf("%s ",value); } }}
输出结果
使用Iterator迭代.....1 2 3 使用ListIterator迭代.....正向遍历.....1 2 3 反向遍历.....3 2 1
(2)map遍历
public class MapMain { public static void main(String[] args) { Map<Integer, String> map = new HashMap<Integer, String>(); map.put(5, "a"); map.put(2, "b"); map.put(3, "c"); map.put(4, "d"); map.put(null, "e");// 和上面相同 , 会自己筛选 System.out.println(map.size()); //方式一:通过Map.keySet遍历key和value for(Map.Entry<Integer,String> entry:map.entrySet()){ System.out.println("1,key:"+entry.getKey()+",value:"+entry.getValue()); } //方式二:通过Map.entrySet使用iterator遍历key和value Iterator<Map.Entry<Integer,String>> iterator=map.entrySet().iterator(); while (iterator.hasNext()){ Map.Entry<Integer,String> entry=iterator.next(); System.out.println("2,key:"+entry.getKey()+",value:"+entry.getValue()); } //方法三: for(Integer key:map.keySet()){ String v = map.get(key);//得到每个key多对用value的值 System.out.println("3,key:"+key+",value:"+v); } }}
输出结果
51,key:null,value:e1,key:2,value:b1,key:3,value:c1,key:4,value:d1,key:5,value:a2,key:null,value:e2,key:2,value:b2,key:3,value:c2,key:4,value:d2,key:5,value:a3,key:null,value:e3,key:2,value:b3,key:3,value:c3,key:4,value:d3,key:5,value:a