Java 集合类框架


1
package test; 2 import java.util.ArrayList; 3 import java.util.Collection; 4 import java.util.Date; 5 import java.util.Iterator; 6 7 import org.junit.Test; 8 public class TestCollection { 9 @Test 10 public void test1(){ 11 Collection<Object> coll = new ArrayList<>(); 12 //1,向定义的集合中添加元素:coll.add(); 13 coll.add("aa"); 14 coll.add(123); 15 coll.add(new Date()); 16 System.out.println(coll.toString()); 17 18 //2:向一个集合中加入另一个集合的全部元素:coll1.addAll(coll); 19 Collection<Object> coll1 = new ArrayList<>(); 20 coll1.add(520); 21 coll1.add("zhaoning"); 22 coll1.addAll(coll); 23 System.out.println(coll1.toString()); 24 25 //3:计算一个集合内有几个元素: coll1.size(); 26 // Collection coll2 = new ArrayList(); 27 // coll2.contains("zhaoning"); 28 int lenght = coll1.size(); 29 System.out.println(lenght); 30 31 //4:清空集合内部的元素:coll.clear(); 32 coll.clear(); 33 System.out.println(coll.toString()); 34 } 35 36 @Test 37 public void test2(){ 38 //5:判断集合是否为空:c1.isEmpty();判断集合c1是否为空,返回值为布尔值,如果为空返回true,不为空则返回false; 39 Collection<Object> c1 = new ArrayList<>(); 40 boolean i = c1.isEmpty(); 41 System.out.println(i); 42 c1.add(520); 43 i = c1.isEmpty(); 44 System.out.println(i); 45 46 //6:移除集合中某个元素:c1.remove(520);删除c1中 520 元素; 47 c1.add("zhaoning"); 48 c1.remove(520); 49 System.out.println(c1.toString()); 50 51 //7:移除集合中某个元素:c1.removeAll(c2);删除c1中 包含c2集合的全部 元素; 52 Collection<Object> c2 = new ArrayList<>(); 53 c2.add("zhaoning"); 54 c1.removeAll(c2); 55 //8:判断集合是否包含某个元素:c2.contains(520);判断集合是否包含元素520,返回值为布尔值,如果包含返回true,否则返回false; 56 c2.clear(); 57 c2.add("zhaoning"); 58 c2.add(520); 59 c2.add(1314); 60 System.out.println(c2.contains(520)); 61 62 //9:判断一个集合是否包含另一个集合所有元素:c2.containsAll(c1): 判断集合c2是否包含集合c1的全部元素,返回值为布尔值, 63 // 如果包含返回true,否则返回false; 64 boolean b = c2.containsAll(c1); 65 System.out.println(b); 66 } 67 68 @Test 69 public void test3(){ 70 //10:讲一个集合元素数组的形式呈现:c3. 71 Collection<Object> c3 = new ArrayList<>(); 72 c3.add("zyn"); 73 c3.add("girl"); 74 Date d1 = new Date(); 75 c3.add(d1); 76 c3.add("girl"); 77 c3.add("zyn"); 78 // Object[] obj = c3.toArray(); 79 // for(int i = 0; i < obj.length; i++){ 80 // System.out.println(obj[i]); 81 // } 82 83 //11:迭代器:iterator 84 Iterator it = c3.iterator(); 85 while(it.hasNext()){ 86 System.out.println(it.next()); 87 } 88 89 System.out.println(); 90 91 //12:比较两个集合的元素是否相同:c4.equals(c3) :返回值为布尔值,相同为true,不同为false; 92 Collection<Object> c4 = new ArrayList<>(); 93 c4.add("zyn"); 94 c4.add("girl"); 95 Date d2 = new Date(); 96 c4.add(d2); 97 boolean b1 = c4.equals(c3); 98 it = c4.iterator(); 99 while(it.hasNext()){ 100 System.out.println(it.next()); 101 } 102 System.out.println(b1);//b1 = false的原因:new 的时间不一样,存在毫秒差距; 103 System.out.println(d2.getTime() - d1.getTime()); 104 } 105 106 @Test 107 public void test4(){ 108 //13:计算一个集合的hash值:c5.hashCode(); 返回值是一个int型值 109 Collection<Object> c5 = new ArrayList<>(); 110 System.out.println(c5.hashCode()); 111 } 112 113 @Test 114 public void test5(){ 115 //来验证两个集合的元素完全相同,但是顺序不同,用equals()比较是否相等: 116 Collection<Object> cc = new ArrayList<>(); 117 Collection<Object> cd = new ArrayList<>(); 118 //cc.add("zhaoning"); 119 cc.add("zyn"); 120 cc.add("and"); 121 //cc.add("zyn"); 122 cc.add("zhaoning"); 123 cd.add("zyn"); 124 cd.add("and"); 125 cd.add("zhaoning"); 126 System.out.println(cc.equals(cd)); 127 128 /* 129 * 总结:两个集合元素相同,但是元素顺序不同,用equals()比较是不相同的; 130 */ 131 } 132 }

对于TreeSet 和TreeMap来说,要想规定向集合中添加同一种元素并且自定义根据元素的某一个属性进行排序,就要继承Comparator类,重写compare()方法;或者是实现Comparable接口,重写compareTo()方法;

package collection;

import java.util.*;

import org.junit.*;
public class TestTreeSet {
    @Test
    public void test1(){
        Set set = new TreeSet();
        set.add(new Person("zhaoning",19));
        set.add(new Person("zyn",20));
        set.add(new Person("Aa",21));
        
        Iterator it = set.iterator();
        while(it.hasNext()){
            System.out.println(it.next());
        }
    }
    
    @Test
    public void test2(){
        Comparator<?> com = new Comparator<Object>(){//实现定制排序重写compare()方法:

            @Override
            public int compare(Object o1, Object o2) {
                if(o1 instanceof Customer && o2 instanceof Customer){
                    Customer c1 = (Customer)o1;
                    Customer c2 = (Customer)o2;
                    return c1.getId().compareTo(c2.getId());
                }
                return 0;
            }
        };
        Set set = new TreeSet(com);
        set.add(new Customer("zhaoning",00001));
        set.add(new Customer("zyn",00002));
        set.add(new Customer("Aa",00003));
        
        Iterator it = set.iterator();
        while(it .hasNext()){
            System.out.println(it.next());
        }
    }
}




package collection;

public class Person implements Comparable<Object>{//实现Comparable接口:
    private Integer age;
    private String name;
    
    public Person(){
        super();
    }
    public Person(String name,Integer age){
        this.name = name;
        this.age = age;
    }
    
    public String getName(){
        return this.name;
    }
    
    public Integer getAge(){
        return this.age;
    }
    
    @Override
    public int compareTo(Object o) {//重写compareTo()方法来自定义利用哪个元素进行排序:
        if(o instanceof Person){
            Person p = (Person)o;
            return this.age.compareTo(p.age);
        }
        return 0;
    }
    
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((age == null) ? 0 : age.hashCode());
        result = prime * result + ((name == null) ? 0 : name.hashCode());
        return result;
    }
    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Person other = (Person) obj;
        if (age == null) {
            if (other.age != null)
                return false;
        } else if (!age.equals(other.age))
            return false;
        if (name == null) {
            if (other.name != null)
                return false;
        } else if (!name.equals(other.name))
            return false;
        return true;
    }
    
    public String toString(){
        return "Person : [ name:" + this.name + ", age:" + this.age + "]";
    }
}

接口Collection: Set 和 List 两个子接口:

      List接口下实现的类有: ArrayList,Vector(老版本),LinkedList;(有序的)

      Set接口下实现的类有:HashSet 和 TreeSet;(无序的)

接口Map: 实现的类有HashMap,LinkedHashMap,TreeMap;

    

原文地址:https://www.cnblogs.com/zhaoningzyn/p/6868807.html