TreeSet和TreeMap的输出

Posted xyqing525

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了TreeSet和TreeMap的输出相关的知识,希望对你有一定的参考价值。

如果加入TreeSet和TreeMap的元素没有实现comprable中的compareTo()方法,那么会报错“treeset cannot be cast to java.lang.Comparable”.

因此需要对元素类型实现comparable借口,并实现compareTo()方法,即告诉TreeSet和TreeMap你要怎么排列,它才能进行排序。

基础类:

package niukewang;

import java.util.Objects;

public class setClass implements Comparable<setClass>{

    String a;
    String b;
    public setClass(String a, String b)
    {
        this.a=a;
        this.b=b;    
    }
    
    public int hashCode() {
        return a.hashCode();
    }
    
    public boolean equals(Object obj)
    {
        if(obj==null) return false;
        
        if(getClass() != obj.getClass()) return false;
        
        final setClass s=(setClass)obj;
        
        return Objects.equals(this.a, s.a);
    }

    @Override
    public int compareTo(setClass o) {
        int num=this.b.compareTo(o.b);
        return num;
    }
}

测试类:

package niukewang;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.Vector;


public class test1 {

    public static void main(String args[]) 
    { 
        setClass s1=new setClass("http://www.yjbys.com/", "same0");
        setClass s2=new setClass("http://www.yjbys.com1/", "same0");
        setClass s3=new setClass("http://www.yjbys.com2/", "same2");
        setClass s4=new setClass("http://www.yjbys.com2/", "same3");
        
        //hasSet
        System.out.println("hasSet......");
        Set<setClass> set=new HashSet<setClass>();
        set.add(s1);
        set.add(s2);
        set.add(s3);
        set.add(s4);
        Iterator it= set.iterator();
        while(it.hasNext())
        {
            setClass ob=(setClass)it.next();
            System.out.println(ob.a);
        }
        
        //TreeSet
        System.out.println("\nTreeSet........");
        Set<setClass> tree=new TreeSet<setClass>();
        tree.add(s1);
        tree.add(s2);
        tree.add(s3);
        tree.add(s4);
        Iterator treeit=tree.iterator();
        while(treeit.hasNext())
        {
            setClass ob=(setClass) treeit.next();
            System.out.println(ob.a);
        }
        
        //TreeMap
        System.out.println("\nTreeMap.........");
        Map<setClass,String> treemap=new TreeMap<setClass,String>();
        treemap.put(s1, "TreeMap one");
        treemap.put(s2, "TreeMap two");
        treemap.put(s3, "TreeMap three");
        treemap.put(s4, "TreeMap four");
        for(Map.Entry<setClass, String> entry: treemap.entrySet())
        {
            System.out.println("The treemap key is "+entry.getKey().a+" The value is "+ entry.getValue());
        }
        
        //HasMap
        System.out.println("\nHashMap......");
        Map<setClass,String> hashmap=new HashMap<setClass, String>();
        hashmap.put(s1, "HashMap one");
        hashmap.put(s2, "HashMap two");
        hashmap.put(s3, "HashMap three");
        hashmap.put(s4, "HashMap four");
        for(Map.Entry<setClass, String> entry : hashmap.entrySet())
        {
            System.out.println("The key is "+entry.getKey().a+" The value is "+entry.getValue());
        }
        
        //HasTable
        System.out.println("\nHashTable......");
        Map<setClass,String> hashtable=new Hashtable<setClass, String>();
        hashtable.put(s1, "HashTable one");
        hashtable.put(s2, "HashTable two");
        hashtable.put(s3, "HashTable three");
        hashtable.put(s4, "HashTable four");
        for(Map.Entry<setClass, String> entry: hashtable.entrySet())
        {
            System.out.println("The HashTable key is "+entry.getKey().a+" The HashTable value is "+entry.getValue());
        }
        
        //LinkedList
        System.out.println("\nLinkedList.....");
        List list=new LinkedList();
        list.add(s1);
        list.add(s2);
        list.add(s3);
        list.add(s4);
        Iterator listit=list.iterator();
        while(listit.hasNext())
        {
            setClass ob=(setClass)listit.next();
            System.out.println(ob.a);
        }

        //ArrayList
        System.out.println("\nArrayList.....");
        List array=new ArrayList();
        array.add(s1);
        array.add(s2);
        array.add(s3);
        array.add(s4);
        Iterator arrayit=array.iterator();
        while(arrayit.hasNext())
        {
            setClass ob=(setClass)arrayit.next();
            System.out.println(ob.a);
        }
        
        //vector
        System.out.println("\nvector.....");
        Vector v=new Vector();
        v.add(s1);
        v.add(s2);
        v.add(s3);
        v.add(s4);
        Iterator vit=v.iterator();
        while(vit.hasNext())
        {
            setClass  ob=(setClass)vit.next();
            System.out.println(ob.a);
        }
            
    }
}

 

输出结果:

hasSet......
http://www.yjbys.com1/
http://www.yjbys.com/
http://www.yjbys.com2/

TreeSet........
http://www.yjbys.com/
http://www.yjbys.com2/
http://www.yjbys.com2/

TreeMap.........
The treemap key is http://www.yjbys.com/ The value is TreeMap two
The treemap key is http://www.yjbys.com2/ The value is TreeMap three
The treemap key is http://www.yjbys.com2/ The value is TreeMap four

HashMap......
The key is http://www.yjbys.com1/ The value is HashMap two
The key is http://www.yjbys.com/ The value is HashMap one
The key is http://www.yjbys.com2/ The value is HashMap four

HashTable......
The HashTable key is http://www.yjbys.com1/ The HashTable value is HashTable two
The HashTable key is http://www.yjbys.com/ The HashTable value is HashTable one
The HashTable key is http://www.yjbys.com2/ The HashTable value is HashTable four

LinkedList.....
http://www.yjbys.com/
http://www.yjbys.com1/
http://www.yjbys.com2/
http://www.yjbys.com2/

ArrayList.....
http://www.yjbys.com/
http://www.yjbys.com1/
http://www.yjbys.com2/
http://www.yjbys.com2/

vector.....
http://www.yjbys.com/
http://www.yjbys.com1/
http://www.yjbys.com2/
http://www.yjbys.com2/

TreeSet并不像hashSet那样,用hashCode和equals取决于元素是否相等。
而是取决于compareTo()方法中指定的排序的方式;

  setClass s1=new setClass("http://www.yjbys.com/", "same0");
  setClass s2=new setClass("http://www.yjbys.com1/", "same0");
  setClass s3=new setClass("http://www.yjbys.com2/", "same2");
  setClass s4=new setClass("http://www.yjbys.com2/", "same3");

如果用hashCode和equals取决于元素是否相等,那么s3和s4是相等的;

而此时候,是根据compareTo()方法中的第二个字符串b来判断是否相等,则s1与s2相等,s3和s4不相等,所以才有这样的输出:

TreeSet........
http://www.yjbys.com/
http://www.yjbys.com2/
http://www.yjbys.com2/

以上是关于TreeSet和TreeMap的输出的主要内容,如果未能解决你的问题,请参考以下文章

java中treemap和treeset实现(红黑树)

Java 数据结构TreeMap和TreeSet的介绍

Java 数据结构TreeMap和TreeSet的介绍

Java 数据结构TreeMap和TreeSet的介绍

在TreeSet和TreeMap中使用hashCode()和equals()

Java集合框架 Map接口实现类--TreeMap的使用 & TreeMap和TreeSet的关系