image.png
    image.png

    1. public class CollectionTest {
    2. @Test
    3. public void test1(){
    4. Collection coll = new ArrayList();
    5. //add(Object e)将元素e添加到集合coll中
    6. coll.add("AA");
    7. coll.add("BB");
    8. coll.add("123");//自动装箱
    9. coll.add(new Date());
    10. //size():获取添加的元素的个数
    11. System.out.println(coll.size());//4
    12. //addAll(Collection coll1):将coll1集合中的元素添加到当前的集合中
    13. Collection coll1 = new ArrayList();
    14. coll1.add(456);
    15. coll1.add("CC");
    16. coll.addAll(coll1);
    17. System.out.println(coll.size());//6
    18. System.out.println(coll);
    19. //clear():清空集合元素
    20. coll1.clear();
    21. //isEmpty():判断当前集合是否为空
    22. System.out.println(coll1.isEmpty());//false
    23. }
    24. }
    1. public class CollectionTest {
    2. @Test
    3. public void test1() {
    4. Collection coll = new ArrayList();
    5. coll.add(123);
    6. coll.add(456);
    7. coll.add(new String("Tom"));
    8. coll.add(false);
    9. coll.add(new Person("jerry", 20));
    10. //1.contains(Object obj):判断当前集合中是否包含obj
    11. //我们在判断时会调用obj对象所在类的equals();
    12. boolean contains = coll.contains(123);
    13. System.out.println(contains);
    14. System.out.println(coll.contains(new String("Tom")));
    15. // System.out.println(coll.contains(p));
    16. System.out.println(coll.contains(new Person("jerry", 20)));
    17. //2.containsAll(Collection call1):判断形参coll1中的所有元素是否都存在于当前集合中
    18. Collection coll1 = Arrays.asList(123, 456);
    19. System.out.println(coll.containsAll(coll1));
    20. }
    21. @Test
    22. public void test2() {
    23. //3.remove(Object obj):从当前集合中移除obj元素
    24. Collection coll = new ArrayList();
    25. coll.add(123);
    26. coll.add(456);
    27. coll.add(new Person("jerry", 20));
    28. coll.add(new String("Tom"));
    29. coll.add(false);
    30. coll.remove(123);
    31. System.out.println(coll);
    32. coll.remove(new Person("jerry", 20));
    33. System.out.println(coll);
    34. //4.removeAll(Collection coll1):从当前集合中移除coll1中所有的元素
    35. Collection coll1 = Arrays.asList(123, 456);
    36. coll.removeAll(coll1);
    37. System.out.println("............" + coll);
    38. }
    39. @Test
    40. public void test3() {
    41. Collection coll = new ArrayList();
    42. coll.add(123);
    43. coll.add(456);
    44. coll.add(new Person("jerry", 20));
    45. coll.add(new String("Tom"));
    46. coll.add(false);
    47. //5.retainAll(Collection coll1):获取当前集合和coll1集合的交集,并返回给当前集合
    48. // Collection coll1 = Arrays.asList(123,456,789);
    49. // coll.retainAll(coll1);
    50. // System.out.println(coll);
    51. //6.equals(Object obj):要想返回true,需要当前集合和形参集合的元素都相同
    52. Collection coll1 = new ArrayList();
    53. coll1.add(123);
    54. coll1.add(456);
    55. coll1.add(new Person("jerry", 20));
    56. coll1.add(new String("Tom"));
    57. coll1.add(false);
    58. System.out.println(coll.equals(coll1));
    59. }
    60. @Test
    61. public void test4() {
    62. Collection coll = new ArrayList();
    63. coll.add(123);
    64. coll.add(456);
    65. coll.add(new Person("jerry", 20));
    66. coll.add(new String("Tom"));
    67. coll.add(false);
    68. //7.hashCode():返回当前对象的哈希值
    69. System.out.println(coll.hashCode());
    70. //8.集合---->数组:toArray()
    71. Object[] arr = coll.toArray();
    72. for (int i = 0; i < arr.length; i++) {
    73. System.out.println(arr[i]);
    74. }
    75. //9.iterator():返回Iterator接口的实例,用于遍历集合元素,放在IteratorTest.java中
    76. }
    77. }