Java之路---Day15(Collection类)

2022-10-13,,,

2019-11-01-22:09:09

目录

  1.collection集合的概念

  2.collection集合常用方法

  3.iterator迭代器

  4.增强for

  5.collection常用工具类


collection集合的概念

  ●集合:集合是java中提供的一 种容器,可以用来存储多个数据。

  集合和数组既然都是容器,它们有啥区别呢?

    ●数组的长度是固定的。集合的长度是可变的。

    ●数组中存储的是同-类型的元素.可以存储基本数据类型值。集合存储的都是对象。而且对象的类型可以不一致。在开发中一般当对象多的时候,使用集合进行存储。

collection集合常用方法

 1 package demosummary.collection;
 2 /*
 3    public boolean add(e e) :把给定的对象添加到当前集合中。
 4   public void clear():清空集合中所有的元素。
 5   public boolean remove(e e);把给定的对象在当前集合中删除。
 6   public boolean contains(e e) ;判断当前集合中是否包含给定的对象。
 7   public boolean isempty(): 判断当前集合是否为空。
 8   public int size(): 返回集合中元素的个数。
 9   public object[] toarray(): 把集合中的元素,存储到数组中。
10  */
11 import java.util.arraylist;
12 import java.util.collection;
13 
14 public class collectiontest {
15     public static void main(string[] args) {
16 
17         collection<string> str = new arraylist<>();
18         /*
19             public boolean add(e e) :把给定的对象添加到当前集合中
20          */
21         boolean b = str.add("张三");
22 //        system.out.println(b);//true
23         str.add("李四");
24         str.add("王五");
25         str.add("钱六");
26         str.add("赵七");
27 //        system.out.println(str);//[张三, 李四, 王五, 钱六, 赵七]
28         /*
29             public boolean remove(e e);把给定的对象在当前集合中删除
30          */
31 
32 //        boolean b1 = str.remove("李四");
33 //        system.out.println(b1);//true
34 //        system.out.println(str);//[张三, 王五, 钱六, 赵七]
35 
36         /*
37           public boolean contains(e e) ;判断当前集合中是否包含给定的对象
38          */
39         boolean b2 = str.contains("孙八");
40         boolean b3 = str.contains("赵七");
41         system.out.println(b2);//false
42         system.out.println(b3);//true
43 
44         /*
45           public boolean isempty(): 判断当前集合是否为空。
46          */
47 
48         boolean b4 = str.isempty();
49         system.out.println(b4);//false
50 
51         /*
52           public int size(): 返回集合中元素的个数
53          */
54         int b5 = str.size();
55         system.out.println(b5);//5
56 
57         /*
58           public object[] toarray(): 把集合中的元素,存储到数组中
59          */
60         object[] obj = str.toarray();
61         system.out.println(obj[0]);//张三
62         for (object o : obj) {
63             system.out.println(o);
64         }
65 
66         /*
67           public void clear():清空集合中所有的元素
68          */
69         str.clear();
70         system.out.println(str);//[]
71     }
72 }

iterator迭代器

  iterator接口

    在程序开发中.经常需要遍历集合中的所有元素。针对这种需求, jdk专门提供了一个接口java.util. iterator。iterator 接口也是java集合中的一员,但它与collection、map 接口有所不同,collection接口与map接口主要用于存储元素,而iterator主要用于迭代访问(即遍历) collection中的元素,因此iterator对象也被称为迭代器。

  迭代:

    即collection集合元素的通用获取方式。在取元素之前先要判断集合中有没有元素,如果有,就把这个元素取出来,继续再判断,如果还有就再取出来。一直把集合中的所有元素全部取出。这种取出方式专业术语称为迭代。

  iterator接口的常用方法

    public e next() :返回迭代的下一个元素。
    public boolean hasnext() :如果仍有元素可以迭代,则返回true.

 1 package demosummary.collection;
 2 
 3 import java.util.arraylist;
 4 import java.util.collection;
 5 import java.util.iterator;
 6 
 7 public class iteratortest {
 8     public static void main(string[] args) {
 9         //创建一个集合
10         collection<string> obj = new arraylist<>();
11         //往集合中添加元素
12         obj.add("德玛");
13         obj.add("皇子");
14         obj.add("德邦");
15         obj.add("剑圣");
16         //使用多态方式来创建实现类对象
17         iterator<string> iter = obj.iterator();
18         //使用while循环来迭代集合
19         while (iter.hasnext()){
20             string next = iter.next();//使用迭代器原理
21             system.out.println(next);
22         }
23     }
24 }

增强for  

  增强for循环:底层使用的也是迭代器,使用for循环的格式,简化了迭代器的书写是jdk1.5之后出现的新特性

  collection<e>extends iterable<e>:所有的单列集合都可以使用增强for
  public interface iterable<t>实现这个接口允许对象成为 "foreach"语句的目标。
  增强for循环:用来遍历集合和数组
  
格式:

    for(集合/数组的数据类型变量名:集合名/数组名){
      sout(变量名);

    }

 1 package demosummary.collection;
 2 
 3 import java.util.arraylist;
 4 import java.util.collection;
 5 import java.util.iterator;
 6 
 7 public class iteratortest {
 8     public static void main(string[] args) {
 9         //创建一个集合
10         collection<string> obj = new arraylist<>();
11         //往集合中添加元素
12         obj.add("德玛");
13         obj.add("皇子");
14         obj.add("德邦");
15         obj.add("剑圣");
16         //使用多态方式来创建实现类对象
17         iterator<string> iter = obj.iterator();
18         //增强for
19         for (string str : obj) {
20             system.out.println(str);
21         }
22     }
23 }

collection常用工具类

  java.utils.collections 是集合工具类,用来对集合进行操作。部分方法如下:
    public static <t> boolean addall(collection<t> c,t... elements) :往集合中添加一些元素。
    public static vold shuffle(list<?> 1list) :打乱顺序:打乱集合顺序。

 1 package demosummary.collection;
 2 
 3 import java.util.arraylist;
 4 import java.util.collection;
 5 import java.util.collections;
 6 
 7 /*
 8       public static <t> boolean addall(collection<t> c,t... elements) :往集合中添加一些元素。
 9     public static void shuffle(list<?> 1list) :打乱顺序:打乱集合顺序。
10     public static <t> void sort(list<t> list) :将集合中元素按照默认规则排序。
11     public static <t> void sort(list<t> ist, comparator<? super t> ) :将集合中元素按照指定规则排序。
12  */
13 public class collectiontools {
14     public static void main(string[] args) {
15         arraylist<string> obj = new arraylist<>();
16         //一开始学的添加元素的方法
17 //        obj.add("德玛");
18 //        obj.add("皇子");
19 //        obj.add("德邦");
20 //        obj.add("剑圣");
21 
22         /*
23               public static <t> boolean addall(collection<t> c,t... elements) :往集合中添加一些元素。
24          */
25 
26         //用collection的方法来添加元素
27         collections.addall(obj,"德玛","皇子","德邦","剑圣");
28         system.out.println(obj);//[德玛, 皇子, 德邦, 剑圣]
29 
30         /*
31             public static void shuffle(list<?> 1list) :打乱顺序:打乱集合顺序。
32          */
33         collections.shuffle(obj);
34         system.out.println(obj);//[皇子, 德玛, 德邦, 剑圣]
35 
36     }
37 }

    public static <t> void sort(list<t> list) :将集合中元素按照默认规则排序。
    public static <t> void sort(list<t> ist, comparator<? super t> ) :将集合中元素按照指定规则排序。

 1 package demosummary.collection;
 2 
 3 import java.util.arraylist;
 4 import java.util.collections;
 5 import java.util.comparator;
 6 
 7 public class collectiontools02 {
 8     public static void main(string[] args) {
 9         //创建一个集合对象
10         arraylist<integer> obj = new arraylist<>();
11         //使用collection方法添加元素
12         collections.addall(obj,4,3,2,1);
13         //  public static <t> void sort(list<t> list) :将集合中元素按照默认规则排序。
14         //默认是升序
15         collections.sort(obj);
16         system.out.println(obj);//[1, 2, 3, 4]
17 
18         /*
19             public static <t> void sort(list<t> ist, comparator<? super t> ) :将集合中元素按照指定规则排序。
20          */
21         //创建一个集合对象
22         arraylist<person> obj01 = new arraylist<>();
23         //往集合添加元素
24         obj01.add(new person("德玛",18));
25         obj01.add(new person("皇子",19));
26         obj01.add(new person("德邦",20));
27         obj01.add(new person("剑圣",18));
28         //输出原来集合的排序
29         system.out.println(obj01);
30         //使用collection的方法按照指定规则排序
31         collections.sort(obj01, new comparator<person>() {
32             @override
33             public int compare(person o1, person o2) {
34                 //按照大小来排序
35                 int result = o1.getage()-o2.getage();
36                 if (result==0){
37                     result=o1.getname().charat(0)-o2.getname().charat(0);
38                 }
39                 return result;
40             }
41         });
42         system.out.println(obj01);
43     }
44 }

 

《Java之路---Day15(Collection类).doc》

下载本文的Word格式文档,以方便收藏与打印。