java-集合(6)-TreeSet,HashSet与TreeSet比较,HashMap,TreeMap只有在排序的功能时使用

来源:互联网 发布:淘宝担保交易怎么弄 编辑:程序博客网 时间:2024/06/06 02:32

本节主要介绍了TreeSet 的使用,要实现Comparable接口,还介绍了HashMap打印所有Key打印所有values,打印所有键值对Map.Entry,在根据键值对分别获得key和values。


1:TreeSet是依靠TreeMap来实现的。
2: TreeSet是一个有序集合,TreeSet中元素将按照升序排列,缺省是按照自然顺序进行排列,意味着TreeSet中元素要实现Comparable接口。
3: 我们可以在构造TreeSet对象时,传递实现了Comparator接口的比较器对象。
HashSet和TreeSet的比较
HashSet是基于Hash算法实现的,其性能通常都优于TreeSet。我们通常都应该使用HashSet,在我们需要排序的功能时,我们才使用TreeSet

这里写图片描述
例子:

package treeSet;import java.util.Comparator;import java.util.Iterator;import java.util.TreeSet;class TreeSetTest {    public static void main(String[] args){        TreeSet ts = new TreeSet(new Student.StudentComparator());        /*ts.add("xiaoxi");        ts.add("xiaozhu");        ts.add("xiaohong");*/        Student s1 = new Student(1,"xiaohong");        Student s2 = new Student(2,"xiaoxi");        Student s3 = new Student(2,"xiaomiao");        ts.add(s1);        ts.add(s2);        ts.add(s3);        //无get()方法,只能用迭代器取出元素        Iterator<Object> it = ts.iterator();        while(it.hasNext()){            System.out.println(it.next());        }    }}class Student implements Comparable<Object> {    int num;    String name;    //实现比较器(Comparator)接口。声明为static 好处是不用产生一个外部类对象再产生一个内部类对象而是直接用外部类名称来产生一个内部类对象    static class StudentComparator implements Comparator<Student> {        @Override        public int compare(Student o1, Student o2) {            int result = o1.num > o2.num ? 1 : (o1.num == o2.num ? 0 : -1);            if(result == 0){                //当我们要比较的学号有一样的,那么就比较名字                result = o1.name.compareTo(o2.name);            }            return result;        }    }    Student(int num, String name) {        this.name = name;        this.num = num;    }    @Override    public int compareTo(Object arg0) {        Student s = (Student) arg0;        // 如果当前数比你要比较的数大返回1,小,返回负数        return num > s.num ? 1 : (num == s.num ? 0 : -1);    }    //格式化输出格式    public String toString() {        return "num=" + num + ", name=" + name;    }}

结果:
num=1, name=xiaohong
num=2, name=xiaomiao
num=2, name=xiaoxi


HashMap
是继承Map
HashMap is an implementation of Map. All optional operations are supported.
这里写图片描述
Map接口与Connection接口不同,没有add 方法用put,获取用get
简单实例:

package MapTest;import java.util.HashMap;class MapTest {    public static void main(String[] args){        HashMap hm = new HashMap<>();        hm.put("1", "xiaoxi");        hm.put("2", "xiaozhu");        hm.put("3", "xiaohong");        System.out.println(hm.get("1"));        System.out.println(hm.get("2"));        System.out.println(hm.get("3"));    }}

结果
xiaoxi
xiaozhu
xiaohong

HashMap方法的使用
keySet()
Returns a set of the keys contained in this map.返回keys
values()
Returns a collection of the values contained in this map.
entrySet()
Returns a set containing all of the mappings in this map.
直接打印键值对
Returns a set containing all of the mappings in this map. Each mapping is an instance of Map.Entry. As the set is backed by this map, changes in one will be reflected in the other.返回值是Map.Entry对象实例,这里可以通过getKey()方法单独得到key,通过getValue()单独得到values.


示例

package MapTest;import java.util.Collection;import java.util.HashMap;import java.util.Iterator;import java.util.Set;class MapTest {    public static void printElements(Collection<?> c) {        Iterator<?> it = c.iterator();        while (it.hasNext()) {            System.out.println(it.next());        }    }    public static void main(String[] args){        HashMap hm = new HashMap<>();        hm.put("1", "xiaoxi");        hm.put("2", "xiaozhu");        hm.put("3", "xiaohong");        //将所有Key打印出来        Set keys = hm.keySet();        System.out.println("this is key :");        printElements(keys);        //将所有values打印出来        Collection values = hm.values();        System.out.println("this is values :");        printElements(values);    }}

结果

this is key :321this is values :xiaohongxiaozhuxiaoxi

entrySet()使用示例

        //打印键值对        Set entry_set = hm.entrySet();        System.out.println(entry_set);

结果:
[3=xiaohong, 2=xiaozhu, 1=xiaoxi]

package MapTest;import java.util.Collection;import java.util.HashMap;import java.util.Iterator;import java.util.Map;import java.util.Map.Entry;import java.util.Set;class MapTest {    public static void printElements(Collection<?> c) {        Iterator<?> it = c.iterator();        while (it.hasNext()) {            System.out.println(it.next());        }    }    public static void main(String[] args){        HashMap hm = new HashMap<>();        hm.put("1", "xiaoxi");        hm.put("2", "xiaozhu");        hm.put("3", "xiaohong");        //将所有Key打印出来        Set keys = hm.keySet();        System.out.println("this is key :");        printElements(keys);        //将所有values打印出来        Collection values = hm.values();        System.out.println("this is values :");        printElements(values);        //打印键值对        Set entry_set = hm.entrySet();        System.out.println(entry_set);        //根据得到的键值对分别getKey(),getValues();        Iterator it = entry_set.iterator();        while(it.hasNext()){            Map.Entry me = (Entry) it.next();            System.out.println(me.getKey() + ":");            System.out.println(me.getValue());        }    }}

结果:

3:xiaohong2:xiaozhu1:xiaoxi
0 0
原创粉丝点击