java集合框架——详解List、Set、Map

1.List、Set、Map区别及其主要使用场合
(1)java集合框架主要有3中接口List、Set、Map。ArraList是有序的元素集合,和数组很类似,我们可以通过索引来访问每一个元素,List中的元素是有重复的。对List中的元素进行排序有2种方法:一是元素类实现Comparable接口,重写compareTo(),二是自定义Comparator接口的实现类,重写compare()。对于上面两个步骤,再调用collections.sort()方法。
(2)Set中的元素是无序不允许重复的,Set元素的一个重要点在于如何判断两个元素是否是重复的,这就需要实现元素类的HashCode()和equals()方法,只有HashCode()相同,且equals()方法返回为真的两个元素算作是重复的,不会再添加到Set的集合中。Set的实现类有HashSet和TreeSet,HashSet的排序方法和上面介绍的ArrayList的一样,而TreeSet是默认根据元素类的compareTo方法进行排序的,不需要再进行排序,但是如果元素类没有重写compareTo方法,编译时会报错。
(3)Map是一组键值对的集合,key是唯一的,不允许有一样的key存在,如果添加的元素有同样的key值,后面添加的元素会自动覆盖前面的元素。Map因为有以上特征,最常见的应用是用于次数统计。
(4)应用场景:允许有重复使用ArrayList,不允许重复使用Set,统计结果使用Map。

2.代码展示
(1)元素类Person

    **package com.yu.collection;
        public class Person implements Comparable<Person> {
        private String name;
        private int age;
        private String city;
    public Person(String name, int age, String city) {
    super();
    this.name = name;
    this.age = age;
    this.city = city;
    }
    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;
    }
    public String getCity() {
        return city;
    }
    public void setCity(String city) {
        this.city = city;
    }


    @Override
    public int compareTo(Person o) {
        // TODO Auto-generated method stub
        if(age>o.age){
            return 1;
        }
        else if(age<o.age){
            return -1;
        }
        else{
            return 0;
        }
    }


    @Override
    public String toString() {
        // TODO Auto-generated method stub
        return name+",  "+age+",  "+city;
    }


    @Override
    public int hashCode() {
        // TODO Auto-generated method stub
        return name.hashCode();
    }`这里写代码片`
    @Override
    public boolean equals(Object obj) {
        // TODO Auto-generated method stub
    Person person =(Person) obj;
    return name.equals(person.getName());
    }
    }

    (2)实现Comparator接口的比较类ComparatorPerson
    package com.yu.collection;

    import java.util.Comparator;

    public class ComparatorPerson implements Comparator<Person> {

        @Override
        public int compare(Person o1, Person o2) {
            // TODO Auto-generated method stub
            return o1.getName().compareTo(o2.getName());
        }

    }


    (3)具体的List、Set、Map测试代码
    package com.yu.collection;

    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.HashMap;
    import java.util.HashSet;
    import java.util.Iterator;
    import java.util.List;
    import java.util.Map;
    import java.util.Set;
    import java.util.TreeSet;

    public class CollectionTest {

        /**
         * @param args
         */
        public static void main(String[] args) {
            // TODO Auto-generated method stub
        //ListTest();
            //SetTest();
        //treeSetTest();
            mapTest();

        }

        public static void ListTest(){
            List<Person> persons = new ArrayList<Person>();
            persons.add(new Person("anna", 20, "北京"));
            persons.add(new Person("dvid", 10, "南京"));
            persons.add(new Person("cat", 58, "西安"));
            persons.add(new Person("beg", 78, "昆明"));
            persons.add(new Person("fresh", 18, "苏州"));

            //Collections.sort(persons);

            Collections.sort(persons, new ComparatorPerson());

            Iterator< Person> iterator = persons.iterator();

            while(iterator.hasNext()){
                System.out.println(iterator.next());
            }
        }


        public static void SetTest(){
            Set<Person> persons = new HashSet<Person>();
            persons.add(new Person("anna", 20, "北京"));
            persons.add(new Person("dvid", 10, "南京"));
            persons.add(new Person("cat", 58, "西安"));
            persons.add(new Person("dvid", 13, "南京"));
            persons.add(new Person("cat", 5, "西安"));
            persons.add(new Person("dvid", 1, "南京"));
            persons.add(new Person("cat", 8, "西安"));

            Iterator< Person> iterator = persons.iterator();
            while(iterator.hasNext()){
                System.out.println(iterator.next());
            }   
        }

        public static void treeSetTest(){
            Set<Person> persons = new TreeSet<Person>();
            persons.add(new Person("anna", 20, "北京"));
            persons.add(new Person("dvid", 10, "南京"));
            persons.add(new Person("cat", 58, "西安"));
            persons.add(new Person("dvid", 10, "南京"));
            persons.add(new Person("cat", 5, "西安"));
            persons.add(new Person("dvid", 10, "南京"));
            persons.add(new Person("cat", 8, "西安"));
            Iterator< Person> iterator = persons.iterator();
            while(iterator.hasNext()){
                System.out.println(iterator.next());
            }   

        }

        public static void  mapTest() {
            Map<String, Person> maps = new HashMap<String,Person>();
            maps.put("001", new Person("xiaohong",23,"北京"));
            maps.put("002", new Person("xiaohong1",231,"北京1"));
            maps.put("003", new Person("xiaohong2",232,"北京2"));
            maps.put("001", new Person("xiaohong3",233,"北京3"));
            maps.put("002", new Person("xiaohong4",234,"北京4"));

            for(Map.Entry<String, Person> entry : maps.entrySet()) {
                System.out.println(entry.getKey()+"------"+entry.getValue());
            }
        }
        }**
阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页