Collection的contions(All)方法

Collection:


Java集合可分为Collection和Map两种体系

  • Collection接口: 单列集合,定义了存取一组对象的方法的集合。

    • List:元素有序、可重复的集合 ---> “动态数组”

      • ArrayList、LinkedList、Vector

    • Set: 元素无序、不可重复的集合

      • HashSet 、LinkedHashSet 、TreeSet

  • Map接口:双列集合,保存具有映射关系“key-value对”的集合。

    • HashMap、LinkedHashMap、TreeMap、Hashtable、 Properties


什么是枚举类?枚举类的对象声明的修饰符有哪些?

枚举类:类中对象的个数是确定的,有限个。

public static final

什么是元注解?说明Retention和Target元注解的作用

元注解:对现有注解进行解释说明的注解

@Retention :用来说明该注解类的生命周期。它有以下三个参数:

RetentionPolicy.SOURCE : 注解只保留在源文件中

RetentionPolicy.CLASS : 注解保留在class文件中,在加载到JVM虚拟机时丢弃

RetentionPolicy.RUNTIME : 注解保留在程序运行期间,此时可以通过反射获得定义在某个类上的所有注解。

@Target : 用来说明该注解可以被声明在那些元素之前。

ElementType.TYPE:说明该注解只能被声明在一个类前。

ElementType.FIELD:说明该注解只能被声明在一个类的字段前。

ElementType.METHOD:说明该注解只能被声明在一个类的方法前。

ElementType.PARAMETER:说明该注解只能被声明在一个方法参数前。

ElementType.CONSTRUCTOR:说明该注解只能声明在一个类的构造方法前。

ElementType.LOCAL_VARIABLE:说明该注解只能声明在一个局部变量前。

ElementType.ANNOTATION_TYPE:说明该注解只能声明在一个注解类型前。

ElementType.PACKAGE:说明该注解只能声明在一个包名前。

比较throw和throws的异同。

 

 


同步监视器:

俗称锁。 任何一个类的对象都可以充当锁,多个线程公用一把锁。


Collection 常用方法:

package com.cheng.collection;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;

public class Demo01 {
   public static void main(String[] args) {
       Collection coll = new ArrayList();

       //集合添加元素add
       coll.add("aa");
       coll.add("bb");
       coll.add(123);
       coll.add(new Date()); //当前时间
       System.out.println(coll);

       System.out.println(coll.size());//size获取集合中元素的个数 4个

       Collection coll1 =  new ArrayList();
       coll1.add("asd");
       coll1.add("niu");
       coll1.add(456);
       coll.addAll(coll1);//addall 将括号内的集合元素全部添加到coll集合中

       System.out.println(coll1.size());//3
       System.out.println(coll.size());//7
       System.out.println(coll.isEmpty());//isEmpty 输出coll集合是否为空

       coll.clear();
       System.out.println(coll.size());//0
       System.out.println(coll.isEmpty());//true
  }
}

package com.cheng.collection;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;

//集合: contions(Object obj) 判断集合中是否包含对应字符

public class Methord2 {
   public static void main(String[] args) {
       Collection coll = new ArrayList();
       coll.add(123);
       coll.add(456);
       coll.add(new String("牛不牛"));
       coll.add(false);
       coll.add(new Person("Ali",24));


       //contions(Object obj) 判断集合中是否包含对应字符
       boolean boo = new Boolean(coll.contains(123));
       System.out.println(boo);//true
       System.out.println(coll.contains(457));//false
       System.out.println(coll.contains(new String("牛不牛")));//true
       //为true事因为String类中重写了equals方法 比较的是内容
       System.out.println(coll.contains(new Person("Ali",24)));//false
       //自定义的类中没有重写equals方法 所以比较的地址 是false
       //如果想输出true 就要在自定义的Person类中重写toString方法 如Person类中末尾的注释所示


       //coll.continonsall(Collection coll1) 判断coll1中的元素是否都在coll中
       Collection coll1  = Arrays.asList(123,456);
       System.out.println(coll.containsAll(coll1));//true
  }


}

Person类:

package com.cheng.collection;

import java.util.Objects;

public class Person {
   private String name;
   private int age;

   public Person() {
  }

   public Person(String name, int age) {
       this.name = name;
       this.age = age;
  }

   public String getName() {
       return name;
  }

   public void setName(String name) {
       this.name = name;
  }

   public int getAge() {
       return age;
  }

   public void setAge(int age) {
       this.age = age;
  }

   @Override
   public String toString() {
       return "Person{" +
               "name='" + name + '\'' +
               ", age=" + age +
               '}';
  }


   //如下
//   @Override
//   public boolean equals(Object o) {
//       if (this == o) return true;
//       if (o == null || getClass() != o.getClass()) return false;
//       Person person = (Person) o;
//       return age == person.age && Objects.equals(name, person.name);
//   }
//


}

 

posted @ 2021-07-27 15:57  Dudo1  阅读(235)  评论(0)    收藏  举报