1.1 Collection

Collection.png

1.1常用的方法

  1. public class CollectionMethod {
  2. @SuppressWarnings({"all"})
  3. public static void main(String[] args) {
  4. List list = new ArrayList();
  5. // add:添加单个元素
  6. list.add("jack");
  7. list.add(10);//list.add(new Integer(10))
  8. list.add(true);
  9. System.out.println("list=" + list);
  10. // remove:删除指定元素
  11. //list.remove(0);//删除第一个元素
  12. list.remove(true);//指定删除某个元素
  13. System.out.println("list=" + list);
  14. // contains:查找元素是否存在
  15. System.out.println(list.contains("jack"));//T
  16. // size:获取元素个数
  17. System.out.println(list.size());//2
  18. // isEmpty:判断是否为空
  19. System.out.println(list.isEmpty());//F
  20. // clear:清空
  21. list.clear();
  22. System.out.println("list=" + list);
  23. // addAll:添加多个元素
  24. ArrayList list2 = new ArrayList();
  25. list2.add("红楼梦");
  26. list2.add("三国演义");
  27. list.addAll(list2);
  28. System.out.println("list=" + list);
  29. // containsAll:查找多个元素是否都存在
  30. System.out.println(list.containsAll(list2));//T
  31. // removeAll:删除多个元素
  32. list.add("聊斋");
  33. list.removeAll(list2);
  34. System.out.println("list=" + list);//[聊斋]
  35. // 说明:以ArrayList实现类来演示.
  36. }

1.2 迭代器(Iterator)

  • 类似与C语言中的链表
  1. public class CollectionIterator {
  2. @SuppressWarnings({"all"})
  3. public static void main(String[] args) {
  4. Collection col = new ArrayList();
  5. col.add(new Book("三国演义", "罗贯中", 10.1));
  6. col.add(new Book("小李飞刀", "古龙", 5.1));
  7. col.add(new Book("红楼梦", "曹雪芹", 34.6));
  8. //1. 先得到 col 对应的 迭代器
  9. Iterator iterator = col.iterator();
  10. //2. 使用while循环遍历
  11. // while (iterator.hasNext()) {//判断是否还有数据
  12. // //返回下一个元素,类型是Object
  13. // Object obj = iterator.next();
  14. // System.out.println("obj=" + obj);
  15. // }
  16. //快速生成 while => itit
  17. while (iterator.hasNext()) {
  18. Object obj = iterator.next();
  19. System.out.println("obj=" + obj);
  20. }
  21. //3. 当退出while循环后 , 这时iterator迭代器,指向最后的元素
  22. // iterator.next();//NoSuchElementException
  23. //4. 如果希望再次遍历,需要重置
  24. 迭代器
  25. iterator = col.iterator();
  26. System.out.println("===第二次遍历===");
  27. while (iterator.hasNext()) {
  28. Object obj = iterator.next();
  29. System.out.println("obj=" + obj);
  30. }
  31. }
  32. }
  33. class Book {
  34. private String name;
  35. private String author;
  36. private double price;
  37. public Book(String name, String author, double price) {
  38. this.name = name;
  39. this.author = author;
  40. this.price = price;
  41. }
  42. public String getName() {
  43. return name;
  44. }
  45. public void setName(String name) {
  46. this.name = name;
  47. }
  48. public String getAuthor() {
  49. return author;
  50. }
  51. public void setAuthor(String author) {
  52. this.author = author;
  53. }
  54. public double getPrice() {
  55. return price;
  56. }
  57. public void setPrice(double price) {
  58. this.price = price;
  59. }
  60. @Override
  61. public String toString() {
  62. return "Book{" +
  63. "name='" + name + '\'' +
  64. ", author='" + author + '\'' +
  65. ", price=" + price +
  66. '}';
  67. }

2 List接口

  • List集合类中的元素是有序的,可重复
  • List集合中每个元素都有对应的索引,可用get()得到

List常用方法

  1. public class ListMethod {
  2. @SuppressWarnings({"all"})
  3. public static void main(String[] args) {
  4. List list = new ArrayList();
  5. list.add("张三丰");
  6. list.add("贾宝玉");
  7. // void add(int index, Object ele):在index位置插入ele元素
  8. //在index = 1的位置插入一个对象
  9. list.add(1, "Curry");
  10. System.out.println("list=" + list);
  11. // boolean addAll(int index, Collection eles):从index位置开始将eles中的所有元素添加进来
  12. List list2 = new ArrayList();
  13. list2.add("jack");
  14. list2.add("tom");
  15. list.addAll(1, list2);
  16. System.out.println("list=" + list);
  17. // Object get(int index):获取指定index位置的元素
  18. //说过
  19. // int indexOf(Object obj):返回obj在集合中首次出现的位置
  20. System.out.println(list.indexOf("tom"));//2
  21. // int lastIndexOf(Object obj):返回obj在当前集合中末次出现的位置
  22. list.add("Curry");
  23. System.out.println("list=" + list);
  24. System.out.println(list.lastIndexOf("Curry"));
  25. // Object remove(int index):移除指定index位置的元素,并返回此元素
  26. list.remove(0);
  27. System.out.println("list=" + list);
  28. // Object set(int index, Object ele):设置指定index位置的元素为ele , 相当于是替换.
  29. list.set(1, "玛丽");
  30. System.out.println("list=" + list);
  31. // List subList(int fromIndex, int toIndex):返回从fromIndex到toIndex位置的子集合
  32. // 注意返回的子集合 fromIndex <= subList < toIndex
  33. List returnlist = list.subList(0, 2);
  34. System.out.println("returnlist=" + returnlist);
  35. }
  36. }

3 ArrayList

  • ArrayList: 维护了一个Object[]数组。
  • 扩容机制:
    1.如果使用无参构造器,则数组初始容量为0,第一次添加后容量为10,再次扩容就会变成1.5倍
    2.如果使用指定大小的构造器,则初始为指定大小,扩容就变成1.5倍。

4 Vector

  • Vector底层也是一个Object[]数组,但Vector是线程安全的,效率没有ArrayListg高
  • 扩容机制:
    1.如果使用无参构造器,则数组初始容量为10,再次扩容就会变成2倍
    2.如果使用指定大小的构造器,则初始为指定大小,扩容就变成2倍。

5 LinkedList

  • 底层实现了双向链表和双端队列
  • 线程不安全

6 Set接口

  • 无序,没有索引,元素不能重复
  • 可以存放空值

7 HashSet

  • 实现了Set接口
  • 底层是HashMap(数组+链表)
    1f3c3d6db78a4bc67c10a7b610abb04.jpg

7.1 HashSet添加元素的步骤

  1. 添加一个元素时,会先得到Hash值,然后转成索引值
  2. 在存储数据表中的索引上调用equals方法比较,如果有相同元素,就放弃添加,没有就添加的链表尾部
  3. jdk8中,如果链表元素超过阈值(16),且索引表的大小>=设定的值(64),就会进行树化(红黑树)

8 LinkedHashSet

  • 底层是LinkedHashMap(数组+双向链表)

9 Collections工具类

  1. public class Collections_ {
  2. public static void main(String[] args) {
  3. //创建ArrayList 集合,用于测试.
  4. List list = new ArrayList();
  5. list.add("tom");
  6. list.add("smith");
  7. list.add("king");
  8. list.add("milan");
  9. list.add("tom");
  10. // reverse(List):反转 List 中元素的顺序
  11. Collections.reverse(list);
  12. System.out.println("list=" + list);
  13. // shuffle(List):对 List 集合元素进行随机排序
  14. // for (int i = 0; i < 5; i++) {
  15. // Collections.shuffle(list);
  16. // System.out.println("list=" + list);
  17. // }
  18. // sort(List):根据元素的自然顺序对指定 List 集合元素按升序排序
  19. Collections.sort(list);
  20. System.out.println("自然排序后");
  21. System.out.println("list=" + list);
  22. // sort(List,Comparator):根据指定的 Comparator 产生的顺序对 List 集合元素进行排序
  23. //我们希望按照 字符串的长度大小排序
  24. Collections.sort(list, new Comparator() {
  25. @Override
  26. public int compare(Object o1, Object o2) {
  27. //可以加入校验代码.
  28. return ((String) o2).length() - ((String) o1).length();
  29. }
  30. });
  31. System.out.println("字符串长度大小排序=" + list);
  32. // swap(List,int, int):将指定 list 集合中的 i 处元素和 j 处元素进行交换
  33. //比如
  34. Collections.swap(list, 0, 1);
  35. System.out.println("交换后的情况");
  36. System.out.println("list=" + list);
  37. //Object max(Collection):根据元素的自然顺序,返回给定集合中的最大元素
  38. System.out.println("自然顺序最大元素=" + Collections.max(list));
  39. //Object max(Collection,Comparator):根据 Comparator 指定的顺序,返回给定集合中的最大元素
  40. //比如,我们要返回长度最大的元素
  41. Object maxObject = Collections.max(list, new Comparator() {
  42. @Override
  43. public int compare(Object o1, Object o2) {
  44. return ((String)o1).length() - ((String)o2).length();
  45. }
  46. });
  47. System.out.println("长度最大的元素=" + maxObject);
  48. //Object min(Collection)
  49. //Object min(Collection,Comparator)
  50. //上面的两个方法,参考max即可
  51. //int frequency(Collection,Object):返回指定集合中指定元素的出现次数
  52. System.out.println("tom出现的次数=" + Collections.frequency(list, "tom"));
  53. //void copy(List dest,List src):将src中的内容复制到dest中
  54. ArrayList dest = new ArrayList();
  55. //为了完成一个完整拷贝,我们需要先给dest 赋值,大小和list.size()一样
  56. for(int i = 0; i < list.size(); i++) {
  57. dest.add("");
  58. }
  59. //拷贝
  60. Collections.copy(dest, list);
  61. System.out.println("dest=" + dest);
  62. //boolean replaceAll(List list,Object oldVal,Object newVal):使用新值替换 List 对象的所有旧值
  63. //如果list中,有tom 就替换成 汤姆
  64. Collections.replaceAll(list, "tom", "汤姆");
  65. System.out.println("list替换后=" + list);
  66. }