Java Road --- Day15 (Collection category)

2019-11-01-22:09:09

table of Contents

  1. Collection concept collection

  2. Collection set of common methods

  3. Iterator iterator

  4. Enhanced for

  5. The Collection common tools


The concept of the collection Collection

  ● set: a collection container provided in java, can be used to store a plurality of data.

  Since collections and arrays are containers, they are so what difference does it make?

    ● length of the array is fixed. The collection length is variable.

    ● stored in the array is the same - the type of elements can store basic data type value. Collection of objects are stored. And the type of the object can be inconsistent. When the object is generally more time, use the collection is stored in development.

Collection set of common methods

. 1  Package demosummary.collection;
 2  / * 
. 3     public Boolean the Add (E E): the given object to the current collection.
. 4    public void Clear (): Clear all the elements in the collection.
. 5    public Boolean Remove (EE); delete the given object in the current collection.
. 6    public Boolean the contains (E E); determining whether the current collection contains the given object.
. 7    public Boolean isEmpty (): this determines whether the set is empty.
. 8    public int size (): returns the number of elements in a set.
. 9    public Object [] toArray (): the set of elements stored in the array.
10   * / 
. 11  Import of java.util.ArrayList;
 12 is  Import java.util.Collection;
 13 is  
14  public  class CollectionTest {
15      public  static  void main (String [] args) {
 16  
. 17          Collection <String> STR = new new the ArrayList <> ();
 18 is          / * 
. 19              public Boolean the Add (E E): the given object to the current collection
 20           * / 
21 is          Boolean B = str.add ( "John Doe" );
 22 is  //         System.out.println (B); // to true 
23 is          str.add ( "John Doe" );
 24          str.add ( "Wang Wu " );
 25          str.add (" money six " );
 26          str.add (" Zhao seven " );
27 //        System.out.println (str); // [John Doe, Wang Wu, Qian six or seven Zhao] 
28          / * 
29              public boolean the Remove (EE); the given object is deleted in the current collection
 30           * / 
31 is  
32  //         Boolean str.remove B1 = ( "John Doe");
 33 is  //         System.out.println (B1); // to true
 34 is  //         System.out.println (STR); // [Zhang , Wang Wu, Qian six, seven Zhao] 
35  
36          / * 
37 [            public Boolean the contains (E E); Analyzing current collection contains the given object
 38 is           * / 
39          Boolean B2 = str.contains ( "Sun eight" );
 40          booleanb3 = str.contains ( "Zhao seven" );
 41 is          System.out.println (B2); // to false 
42 is          System.out.println (B3); // to true 
43 is  
44 is          / * 
45            public Boolean isEmpty (): Analyzing The current collection is empty.
46 is           * / 
47  
48          Boolean B4 = str.isEmpty ();
 49          System.out.println (B4); // to false 
50  
51 is          / * 
52 is            public int size (): returns the number of elements in the set
 53 is           * / 
54 is          int = B5 str.size ();
 55          System.out.println (B5);// . 5 
56 is  
57 is          / * 
58            public Object [] toArray (): the set of elements stored in the array
 59           * / 
60          Object [] obj = str.toArray ();
 61 is          System.out.println (obj [ 0]); // seating 
62 is          for (Object O: obj) {
 63 is              System.out.println (O);
 64          }
 65  
66          / * 
67            public void Clear (): Clear all the elements in the set of
 68           * / 
69          str.clear ();
 70          System.out.println (STR); // [] 
71 is      }
 72 }

Iterator iterator

  Iterator interface

    在程序开发中.经常需要遍历集合中的所有元素。针对这种需求, 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 }

 

Guess you like

Origin www.cnblogs.com/hpcz190911/p/11780327.html