Test whether there is a comparator during initialization of TreeSet and treemap
•
Java
import java.util.*;
public class testCode {
public static void main(String[] args){/** * (1)TreeSet里面:没有接口Comparator中的compare<a href="https://www.jb51.cc/tag/fangfa/" target="_blank" class="keywords">方法</a>,只<a href="https://www.jb51.cc/tag/diaoyong/" target="_blank" class="keywords">调用</a>了接口Comparator中的equals<a href="https://www.jb51.cc/tag/fangfa/" target="_blank" class="keywords">方法</a> * (2)继承了抽象类AbstractSet<E>:equals<a href="https://www.jb51.cc/tag/fangfa/" target="_blank" class="keywords">方法</a>被覆写.故TreeSet<a href="https://www.jb51.cc/tag/diaoyong/" target="_blank" class="keywords">调用</a>equals<a href="https://www.jb51.cc/tag/fangfa/" target="_blank" class="keywords">方法</a>时,是<a href="https://www.jb51.cc/tag/diaoyong/" target="_blank" class="keywords">调用</a>的这个equals<a href="https://www.jb51.cc/tag/fangfa/" target="_blank" class="keywords">方法</a>. */ //例一:TreeSet在初始化时,指定比较器,从而指定元素的排列顺序 TreeSet<Integer> set=new TreeSet<Integer>(new Comparator<Integer>() { @Override public int compare(Integer o1,Integer o2) { return o2-o1;//降序 } }); set.add(1);set.add(4);set.add(3); //例二:TreeSet在初始化时,不指定比较器,故元素的排列顺序为<a href="https://www.jb51.cc/tag/mo/" target="_blank" class="keywords">默</a>认的升序 TreeSet<Integer> set1=new TreeSet<Integer>(); set1.add(1);set1.add(4);set1.add(3); //分别<a href="https://www.jb51.cc/tag/shuchu/" target="_blank" class="keywords">输出</a>set,set1 Sy<a href="https://www.jb51.cc/tag/stem/" target="_blank" class="keywords">stem</a>.out.println("set: "); Iterator<Integer> iterator1=set.iterator(); while (iterator1.hasNext()){ Sy<a href="https://www.jb51.cc/tag/stem/" target="_blank" class="keywords">stem</a>.out.print(iterator1.next()+" "); } Sy<a href="https://www.jb51.cc/tag/stem/" target="_blank" class="keywords">stem</a>.out.println(); Sy<a href="https://www.jb51.cc/tag/stem/" target="_blank" class="keywords">stem</a>.out.println("---------------------"); Sy<a href="https://www.jb51.cc/tag/stem/" target="_blank" class="keywords">stem</a>.out.println("set1: "); Iterator<Integer> iterator2=set1.iterator(); while (iterator2.hasNext()){ Sy<a href="https://www.jb51.cc/tag/stem/" target="_blank" class="keywords">stem</a>.out.print(iterator2.next()+" "); } Sy<a href="https://www.jb51.cc/tag/stem/" target="_blank" class="keywords">stem</a>.out.println(); Sy<a href="https://www.jb51.cc/tag/stem/" target="_blank" class="keywords">stem</a>.out.println("---------------------"); /** * (1)TreeMap里面:没有接口Comparator中的compare<a href="https://www.jb51.cc/tag/fangfa/" target="_blank" class="keywords">方法</a>,只<a href="https://www.jb51.cc/tag/diaoyong/" target="_blank" class="keywords">调用</a>了接口Comparator中的equals<a href="https://www.jb51.cc/tag/fangfa/" target="_blank" class="keywords">方法</a> * (2)继承了抽象类AbstractMap<K,V>:equals<a href="https://www.jb51.cc/tag/fangfa/" target="_blank" class="keywords">方法</a>被覆写.故TreeMap<a href="https://www.jb51.cc/tag/diaoyong/" target="_blank" class="keywords">调用</a>equals<a href="https://www.jb51.cc/tag/fangfa/" target="_blank" class="keywords">方法</a>时,是<a href="https://www.jb51.cc/tag/diaoyong/" target="_blank" class="keywords">调用</a>的这个equals<a href="https://www.jb51.cc/tag/fangfa/" target="_blank" class="keywords">方法</a>. */ //例一:TreeMap初始化时,未指定比较器,<a href="https://www.jb51.cc/tag/mo/" target="_blank" class="keywords">默</a>认按照key升序 TreeMap<Integer,Integer> map=new TreeMap<Integer,Integer>(); map.put(1,1);map.put(2,3);map.put(-1,3);map.put(4,3); //例二:TreeMap初始化时,指定了比较器,按照key降序 TreeMap<Integer,Integer> map1=new TreeMap<Integer,Integer>(new Comparator<Integer>() { @Override public int compare(Integer o1,Integer o2) { return o2-o1;//降序 } }); map1.put(1,1);map1.put(2,3);map1.put(-1,3);map1.put(4,3); //<a href="https://www.jb51.cc/tag/shuchu/" target="_blank" class="keywords">输出</a>map,map1 Iterator<Map.Entry<Integer,Integer>> i1=map.entrySet().iterator(); Iterator<Map.Entry<Integer,Integer>> i2=map1.entrySet().iterator(); Sy<a href="https://www.jb51.cc/tag/stem/" target="_blank" class="keywords">stem</a>.out.println("map:"); while (i1.hasNext()){ Map.Entry<Integer,Integer> entry=i1.next(); Sy<a href="https://www.jb51.cc/tag/stem/" target="_blank" class="keywords">stem</a>.out.print("key:"+entry.getKey()+","); Sy<a href="https://www.jb51.cc/tag/stem/" target="_blank" class="keywords">stem</a>.out.println("value: "+entry.getValue()); } Sy<a href="https://www.jb51.cc/tag/stem/" target="_blank" class="keywords">stem</a>.out.println("---------------------"); Sy<a href="https://www.jb51.cc/tag/stem/" target="_blank" class="keywords">stem</a>.out.println("map1: "); while (i2.hasNext()){ Map.Entry<Integer,Integer> entry=i2.next(); Sy<a href="https://www.jb51.cc/tag/stem/" target="_blank" class="keywords">stem</a>.out.print("key: "+entry.getKey()+","); Sy<a href="https://www.jb51.cc/tag/stem/" target="_blank" class="keywords">stem</a>.out.println("value: "+entry.getValue()); } Sy<a href="https://www.jb51.cc/tag/stem/" target="_blank" class="keywords">stem</a>.out.println("---------------------"); }
}
Process finished with exit code 0
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码