容器

ArrayList

/**
 * 不同类的对象不能进行比较语法上通不过
 */

import java.util.*;

public class TestList {
    public static void main(String[] args){
        List l = new ArrayList();
        //l.add(33);
        l.add(new Student8(199,"莫"));
        l.add(new Student8(199,"莫"));
        l.add(new Student8(200,"启"));
        l.add(new Student8(133,"开发"));
        l.add(new Student8(190,"来看"));
        Collections.sort(l);//调用该方法时,会自动调用compareTo方法,不同类的对象不能进行比较
        System.out.println(l);
    }
}
class Student8 implements Comparable{//因为自定义的类,没有标准的比较方法,所以放入List容器时,需要重写比较方法
    int id;
    String name;
    Student8(int id,String name){
        this.id = id;
        this.name = name;
    }
    @Override
    public String toString(){
        return "["+id+" "+name+"]";
    }
    @Override
    public int compareTo(Object o) {//比较两个对象的大小
        Student8 s = (Student8)o;
        if(this.id==s.id){
            return 1;
        }else if(this.id>s.id){
            return 0;
        }else{
            return -1;
        }

    }
}

HashSet

/**
 * /重写后不同类的对象,内容相等不能放进HashSet容器,语法上通不过
 */

import java.util.*;
public class TestHash {
    public static void main(String[] args){
        Set s = new HashSet();//重写后不同类的对象,内容相等不能放进HashSet容器,语法上通不过
        s.add(new A(2,"MO"));
        s.add(new A(2,"MO"));
        s.add(new A(2,"M"));
        s.add(new AA(2,"M"));
        s.add(new AA(1,"M"));
        s.add(new AA(1,"M"));
        System.out.println(s);
    }
}
class A{
    int id;
    String name;
    A(int id,String name){
        this.id = id;
        this.name = name;
    }
    public String toString(){
        return id+" "+name;
    }
    public int hashCode(){
        return new String(name+id).hashCode();
    }
    public boolean equals(Object obj){
        A a = (A)obj;
        if(this.id==a.id&&this.name==a.name){
            return true;
        }else{
            return false;
        }
    }
}
class AA{
    int id;
    String name;
    AA(int id,String name){
        this.id = id;
        this.name = name;
    }
    public String toString(){
        return id+" "+name;
    }
    public int hashCode(){
        return new String(name+id).hashCode();
    }
    public boolean equals(Object obj){
        AA a = (AA)obj;
        if(this.id==a.id&&this.name==a.name){
            return true;
        }else{
            return false;
        }
    }
}

 

TestIterator_1

 

 测试LinkdList,ArrayList,HashSet,TreeSet

import java.util.*;
public class TestIterator {
    public static void main(String[] args){
        Set s = new HashSet();
        s.add("1002");//自动装箱(自动转换成对象)
        s.add(1003);
        s.add(new P(3,"o"));
        s.add(new P(3,"o"));
        s.add(new P(4,"n"));

        System.out.println(s);//Set是一个集合类型的容器,用于存储对象,无序且不允许重复的
        System.out.println("-------------");
        System.out.println("Iterator 遍历后:");
        showCollection(s);
        System.out.println("-------------");
        List l = new ArrayList();//ArrayList是一个数组形式的容器,用于存储对象,有序且允许重复
        l.add(new P(99,"rr"));
        l.add(new P(4,"ww"));
        l.add(new P(4,"ww"));
        l.add(new P(8,"ii"));
        Collections.sort(l);
        System.out.println(l);
        System.out.println("-------------");
        System.out.println("Iterator 遍历后:");
        showCollection(l);
        List ll = new LinkedList();//LinkedList是一个链表形式的容器,用于存储对象,有序且允许重复
//        ll.add(34);
//        ll.add("7676");//不是同一个类的对象不能比较
        ll.add(new P(9,"ll"));
        ll.add(new P(5,"ee"));
        ll.add(new P(7,"ee"));
        ll.add(new P(3,"ee"));
        Collections.sort(ll);
        System.out.println(ll);
        System.out.println("-------------");
        System.out.println("Iterator 遍历后:");
        showCollection(ll);
        Set ss = new TreeSet();//是一个有序的集合,它的作用是提供有序的Set集合,TreeSet中的数据是自动排好序的。向TreeSet容器中添加的对象必须得是实现Comparable接口的类对象 ,否则运行程序会报错
        //向TreeSet容器中添加的对象可以不重写hashCode方法和equasl方法.
        ss.add(new P(8,"mf"));
        ss.add(new P(0,"f"));
        ss.add(new P(9,"ff"));
        ss.add(new P(3,"fkff"));
        ss.add(new P(3,"fkff"));
        System.out.println("-------------");
        //Collections.sort(ll);TreeSet是由二叉树实现的,是自动排序好的
        System.out.println(ss);
        System.out.println("Iterator 遍历后:");
        showCollection(ss);
    }
    //可以遍历所有Collection接口的实现类,Iterator 迭代器
    public static void showCollection(Collection c){
        Iterator i = c.iterator();
        while(i.hasNext()){
            System.out.println(i.next());
        }
    }
}
class P implements Comparable{
    int x;
    String y;
    P(int x,String y){
        this.x = x;
        this.y = y;
    }
    public String toString(){//重写Object父类的方法,以字符串的形式输出
        return x+""+y;
    }
    public int hashCode(){
        //return new String(x+y).hashCode();正确的,因为字符串本身是一个对象
        //return new Integer(x+y).hashCode(); 错误的,整数不是一个对象,不可以调用hashCode
        return x*y.hashCode();
    }
    public boolean equals(Object obj){
        P p = (P)obj;
        if(this.x==p.x&&this.y==y){
            return true;
        }else{
            return false;
        }
    }
    public int compareTo(Object o){
     P pp = (P)o;
     return this.x-pp.x;
    }
}

Map

首先你要知道什么是map,map就是用于存储键值对(<key,value>)的集合类,也可以说是一组键值对的映射(数学概念)。注意,我这里说的只是map的概念,是为了通俗易懂,面试时候方便记忆,但是你自己一定要明白,在java中map是一个接口,是和collection接口同一等级的集合根接口。

import java.util.*;
public class TestHashMap {
    public static void main(String[] args){
        Map m = new HashMap();//集合的容器
        m.put(23,new Person(1001,18,"mo"));//Key键是唯一的,不可重复,一个键只能对应一个对象
        //一个对象可以对应多个Key就像一个射手只能射一个把
        m.put(24,new Person(1001,18,"mo"));
        m.put(25,new Person(1003,13,"mp"));
        m.put(25,new Person(1004,15,"ml"));
        m.put(27,new Person1(1004,15,"ml"));
        m.put(28,new Person1(1009,15,"ml"));
        m.put(29,new Person1(1009,19,"ml"));
        m.put(30,new Person1(1009,19,"ml"));
        System.out.println(m);
        showCollection(m);
//        Person pp = new Person(1001,18,"mo");
//        Person p = new Person(1001,18,"mo");
//        System.out.println(pp.equals(p));

    }
    public static void showCollection(Map c){
        Set s = c.keySet();//返回所以Map中key组成的Set集合
        Iterator it = s.iterator();
        while(it.hasNext()){
            int K = (Integer)it.next();   // (Integer) 不能省
            System.out.println(K);
        }
    }
}
class Person{
    int id,age;
    String name;
    Person(int id,int age,String name){
        this.id = id;
        this.name = name;
        this.age = age;
    }
    public String toString(){
        return id+name+age;
    }
    public int hashCode(){
        return id*age*name.hashCode();
    }
    public boolean equals(Object obj){
        Person pp = (Person)obj;
        return this.name.equals(pp.name)&&this.age==pp.age&&this.id==pp.id;
    }
}
class Person1{
    int id,age;
    String name;
    Person1(int id,int age,String name){
        this.id = id;
        this.name = name;
        this.age = age;
    }
    public String toString(){
        return id+name+age;
    }
    public int hashCode(){
        return id*age*name.hashCode();
    }
    public boolean equals(Object obj){
        Person1 p = (Person1)obj;
        return this.age==p.age&&this.id==p.id&&this.name.equals(p.name);
    }
}

 运行结果

 

posted @ 2022-02-22 18:07  iiuu也一样  阅读(104)  评论(0编辑  收藏  举报