zoukankan      html  css  js  c++  java
  • 集合和Collection接口常用方法

    1 数组弊端:

    1)一旦创建,其长度不可变。2)真是的数组存放的对象的个数是不可知的。

    2 Java集合分为Collection和Map两个集合

    Collection接口:

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

           -----HashSet、LinkedHashSet、TreeSet

           List接口: 元素有序,可重复的集合  ——看作”动态数组“

            ----ArrayList(主要的实现类)、LinkedList、Vector

          注意:添加进List集合中的元素或对象所在的类一定要重写equals方法,因为要确定集合中是否有这个元素。

            Map接口: 具有映射关系“key-value对“的集合

           -----HashMap、LinkedHashSet、TreeMap、HashTable(子类: properties)

    3 Collection接口:包含存储数据的通用方法

    方法使用如下:

    package lianxi1;
    
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.Collection;
    import java.util.Date;
    import java.util.Iterator;
    
    import org.junit.Test;
    
    public class TestConnection {
    @Test   
        public void test1(){
           Collection coll1 = new ArrayList();
           coll1.add(342);
           coll1.add("sfdsa");
           coll1.add(new Date());
           System.out.println(coll1.size());
           System.out.println(coll1);//是由AbstractCollection重写,ArrayList继承过来的
           Collection coll2 = Arrays.asList(34,"men","883");  //Arrays.asList提供固定长度数组转为集合的方法
           coll1.addAll(coll2);
           System.out.println(coll1);
           System.out.println(coll1.contains("883"));
           System.out.println(coll1.containsAll(coll2));
           //coll2.clear();  //——————————————————运行时异常,coll2是Arrays对象,没有clear方法
           coll1.clear();
           System.out.println(coll2.isEmpty());
           System.out.println(coll1.isEmpty());
           System.out.println("coll1="+coll1+"coll2="+coll2);
           System.out.println(coll2.hashCode());
           
       }
    @Test
       public void test2(){
           Collection coll1 = new ArrayList();
           //coll1.add(2,45); --------coll1只能引用Collection的方法,必须强制类型转换,才能用ArrayList独有方法 
           coll1.add("ff");
           coll1.add(33);
           coll1.add("rr");
           coll1.add(45);
           coll1.add(new Date());
           Collection coll2 = new ArrayList();
           coll2.add(55);
           coll2.add(35.432);
           coll2.add("rr");
           Student stu = new Student("3001","wu");
           coll2.add(stu);
           System.out.println(coll1);
           System.out.println(coll2);
          // coll1.retainAll(coll2);
           coll1.removeAll(coll2);
           System.out.println(coll1);
           System.out.println(coll2);
           System.out.println(coll1.equals(coll2));
           //toArray()
           Object[] obj = coll1.toArray();
           for(int i=0;i<obj.length;i++){
               System.out.print(obj[i]+" ");
           }
           System.out.println();
           //Iterator()
           Iterator ite = coll1.iterator();
           while(ite.hasNext()){
               System.out.println(ite.next());
           }
           //remove(o)
           coll2.remove(stu);
           System.out.println(coll2);
       }
    }
  • 相关阅读:
    VS2013诡异问题,虚方法、泛型,通通躺枪
    RabbitMQ与Redis队列对比
    CentOS 6.5中配置RabbitMQ
    RabbitMQ集群配置
    Ubuntu下的RabbitMQ安装与web管理配置
    EasyNetQ自定义异常消息处理
    2021年 B站1024程序节技术对抗赛
    01背包问题 之 动态规划(通俗解释)
    Jmeter Web 性能测试入门 (七):Performance 测试中踩过 Jmeter 的坑
    Jmeter Web 性能测试入门 (五):Jmeter 参数化 Request
  • 原文地址:https://www.cnblogs.com/yjtm53/p/4147218.html
Copyright © 2011-2022 走看看