java集合

  /*
        Collection接口
        |---------List接口
            |----------ArrayList(主要实现类)
            |----------LinkedList(对于频繁的插入删除操作)
            |----------Vector(古老的实现类、线程安全但效率低于ArrayList)
        |--------- Set接口   存储无序的  不可重复的元素  Set中常用的方法都是Collection下定义的 无序性!=随机
            |---------HashSet  (主要实现类)
            |---------LinkedHashSet
            |---------TreeSet
         */
@Test
    public void testHashSet(){
        //无序性!=随机
        //不可重复性   得看  equals()方法 hashCode()方法       是否重写
        //进而保证Set元素得不可重复性
        //Set中的元素如何存储呢?   使用了哈希算法
        //当Set中添加对象时首先调用此对象所在类的HashCode()方法  计算此对象的哈希值  此哈希值决定了此对象在Set中的存储位置,若此位置没有对象存储,则直接存到这个位置
        //若此位置已有对象存储再通过 equals()比较这两个对象相同 如果相同后一个不能再添加进来  要求  hashCode()  equals() 方法一致

        Set set = new HashSet();
        set.add(123);
        set.add(465);
        set.add("AA");
        set.add("BB");
        set.add(null);

        Person p1 = new Person(2,"lisi");
        Person p2 = new Person(2,"lisi");
        System.out.println(p1.hashCode());
        System.out.println(p2.hashCode());
        System.out.println(p1.equals(p2));
        set.add(p1);
        set.add(p2);
        System.out.println(set.size());
        System.out.println(set);
    }
/*
 * 数组一旦创建  不可改变     数组存放对象不可增加   使用集合避免弊端
 */
public class TestCollection {
    @Test
    public void testCollection3() {
        Collection coll = new ArrayList();
        // size
        System.out.println(coll.size());
        // add
        coll.add(123);
        coll.add(new String("AA"));
        coll.add(new Date());
        coll.add("BB");
        coll.add(new Student("MM",23));
        
        Collection coll1 = new ArrayList();
        coll1.add(123);
        coll1.add(new String("AA"));

        // 10 removeAll(Collection coll):从当前集合中删除包含在coll中的元素
        coll.retainAll(coll1);
        System.out.println(coll1);
        
        //11  equals(Object obj):  判断所有元素是否完全相同     相同true
        Collection coll2 = new ArrayList();
        coll2.add(123);
        coll2.add(new String("AA"));
        System.out.println(coll1.equals(coll2));
        //12  hashCode():
        System.out.println(coll.hashCode());
        
        //13  toArray()  将集合转换成数组
        Object[] obj = coll.toArray();
        for (Object object : obj) {
            System.out.println(object);
        }
        
        //14  iterator() 返回以正确顺序在列表的元素上进行迭代的迭代器。遍历集合   返回一个 Iterator接口实现类对象 进而实现遍历
        Iterator iterator =  coll.iterator();
        //方式一
        /*System.out.println(iterator.next());
        System.out.println(iterator.next());
        System.out.println(iterator.next());*/
        //方式二
        /*for(int i = 0;i<coll.size();i++) {
            System.out.println(iterator.next());
        }*/
        //方式三使用
        while(iterator.hasNext()) {
            System.out.println(iterator.next());
        }
        
        
        
        
    }

    @Test
    public void testCollection2() {
        Collection coll = new ArrayList();
        //1 size
        System.out.println(coll.size());
        //2 add
        coll.add(123);
        coll.add("AAA");
        System.out.println(coll.size());
        // contains(Object obj)判断集合是否包含指定元素 包含则返回 true
        // 判断依据:根据元素所在在的类的equals()方法进行判断
        // 明确:如果存入集合元素是自定义类对象 要求自定义重写equals()方法
        boolean b1 = coll.contains(123);
        System.out.println(b1);
        Student s = new Student("张三", 33);
        coll.add(s);
        boolean b2 = coll.contains(s);
        System.out.println(b2);
        // 7containsAll(Conlection coll):判断当前几集合中是否包含coll中所有元素
        Collection coll1 = new ArrayList();
        coll1.add(123);
        coll1.add(new String("AAA"));
        boolean b3 = coll.containsAll(coll1);
        coll1.add(456);
        System.out.println("#" + b3);

        // 8、retainAll(Collection coll): 求当前集合与col1的元素共有的元素 返回给当前集合
        coll.retainAll(coll1);
        System.out.println(coll);

        // 9 remove(Object obj): 删除集合中obj 元素 成功返回 true
        boolean b4 = coll.remove("BB");
        System.out.println("#" + b4);

        // 10 removeAll(Collection coll):删除所有
        coll.retainAll(coll);
    }

    @Test
    public void testCollection1() {
        Collection coll = new ArrayList();
        //1 size
        System.out.println(coll.size());
        //2 add
        coll.add(123);
        coll.add("AAA");
        coll.add(new Date());
        System.out.println(coll.size());
        //3 addAll()                     Arrays.asList(1, 2, 3);    数组转换集合
        Collection coll1 = Arrays.asList(1, 2, 3);
        coll.addAll(coll1);
        System.out.println(coll.size());

        // 查看集合元素
        System.out.println(coll);
        //4 isEmpty() 判断集合是否为空
        System.out.println(coll.isEmpty());
        //5 清空集合
        coll.clear();
        System.out.println(coll.size());
    }

@Test
    public void testFor2() {
        String[] str = new String[]{"AA","BB","DD"};
        for(String s:str) {
            s= "M";  //记住s定义的是局部变量  其值不会改变  增强for
            System.out.println(s);
        }
        for(int i = 0;i<str.length;i++) {
            System.out.println(str[i]);
        }
    }
    
    @Test
    public void testFor() {
        Collection coll = new ArrayList();
        // siz  e
        System.out.println(coll.size());
        // add
        coll.add(123);
        coll.add(new String("AA"));
        coll.add(new Date());
        coll.add("BB");
        coll.add(new Student("MM",23));
        
        for (Object i : coll) {
            System.out.println(i);
        }
    }

    //错误写法
    @Test
    public void test2() {
        Collection coll = new ArrayList();
        // size
        System.out.println(coll.size());
        // add
        coll.add(123);
        coll.add(new String("AA"));
        coll.add(new Date());
        coll.add("BB");
        coll.add(new Student("MM",23));
        
        Iterator i = coll.iterator();
        while(i.next()!=null) {
            //java.util.NoSuchElementException
            System.out.println(i.next());
        }
    }
    //正确写法使用迭代器
    @Test
    public void test1() {
        Collection coll = new ArrayList();
        // size
        System.out.println(coll.size());
        // add
        coll.add(123);
        coll.add(new String("AA"));
        coll.add(new Date());
        coll.add("BB");
        coll.add(new Student("MM",23));
        
        Iterator i = coll.iterator();
        while(i.hasNext()) {
            System.out.println(i.next());
        }
    }
原文地址:https://www.cnblogs.com/mzdljgz/p/10877592.html