Java Sorted Map Example
2015-06-07 21:14
351 查看
InthisexampleweshallshowyouhowtomakeuseofJavaSortedMap.A
Tip1
Thismethodcomparesthisobjectwithoobject.Returnedintvaluehasthefollowingmeanings.
Also,wecanuseourownJavaComparableandComparatorExampletosortObjectsbyByronKiourtzoglou.
Tip2
All
Avoid(noarguments)constructor,whichcreatesanempty
Aconstructorwithasingleargumentoftype
Aconstructorwithasingleargumentoftype
Aconstructorwithasingleargumentoftype
Rangeview—performsarbitraryrangeoperationsonthe
Endpoints—returnsthefirstorthelastkeyinthe
Comparatoraccess—returnsthe
viewsourceprint?
isaSortedMap
thatsortitsentriesinascendingorderaccordingtothekeys’naturalordering,oraccordingtoaMap
providedatthetimeoftheComparator
creation.AllkeysinsertedintoaSortedMap
mustimplementtheSortedMap
interface(orbeacceptedbythespecifiedComparable
).Furthermore,allsuchelementsmustbemutuallycomparable(i.e,MutuallyComparablesimplymeansthattwoobjectsaccepteachotherastheargumenttotheirComparator
method),IfyoutrytosortkeyswhichdonotimplementcompareTo
ornothasaspecificComparable
,aComparator
willbethrown.ClassCastException
Tip1
java.lang.Comparable:intcompareTo(Objecto):
Thismethodcomparesthisobjectwithoobject.Returnedintvaluehasthefollowingmeanings.
positive–thisobjectisgreaterthano
zero–thisobjectequalstoo
negative–thisobjectislessthano
Also,wecanuseourown
.IfyouneedtoknowmoreabouttheComparator
andComparable
,TakealookonComparator
Tip2
All
implementationclassesshouldprovidefour“standard”constructorsasthefollowing:SortedMap
Avoid(noarguments)constructor,whichcreatesanempty
sortedaccordingtothenaturalorderingofitskeys.SortedMap
1 | SortedMapsortedMap= new TreeMap(); |
,whichcreatesanemptyComparator
sortedaccordingtothespecifiedSortedMap
.Comparator
1 | Comparatorcomparator= new MyComparator(); |
2 | SortedMapsortedMap= new TreeMap(comparator); |
,whichcreatesanewMap
withthesamekey-valuemappingsasitsargument,sortedaccordingtothekeys’naturalordering.Map
1 | Mapmap= new HashMap(); |
2 | SortedMapsortedMap= new TreeMap(map); |
,whichcreatesanewSortedMap
withthesamekey-valuemappingsandthesameorderingastheinputSortedMap
.SortedMap
1 | SortedMapsortedMap= new TreeMap(); |
2 | SortedMapnewSortedMap= new TreeMap(sortedMap); |
1.SortedMapOperations:
TheinterfaceprovidesoperationsfornormalMapoperationsandforthefollowing:SortedMap
Rangeview—performsarbitraryrangeoperationsonthe
SortedMap
:ReturnsaviewoftheportionofthissubMap( KfromKey, KtoKey)
whosekeysrangefromfromKey,inclusive,totoKey,exclusive.Map
:ReturnsaviewoftheportionofthisheadMap( KtoKey)
whosekeysarestrictlylessthantoKey.Map
:ReturnsaviewoftheportionofthistailMap( KfromKey)
whosekeysaregreaterthanorequaltofromKey.Map
Endpoints—returnsthefirstorthelastkeyinthe
SortedMap
:Returnsthefirst(lowest)keycurrentlyinthisfirstKey()
.Map
:Returnsthelast(highest)keycurrentlyinthislastKey()
.Map
Comparatoraccess—returnsthe
,ifany,usedtosortthemapComparator
:Returnsthecomparator()
usedtoorderthekeysinthisComparator
,ornullifthisMap
usesthenaturalorderingofitskeys.Map
2.Example:
2.1.SortMapExample.java
01 | package com.jcg.util.map; |
02 |
03 | import java.util.Comparator; |
04 | import java.util.HashMap; |
05 | import java.util.Map; |
06 | import java.util.TreeMap; |
07 |
08 | /** |
09 | *@authorashraf |
10 | * |
11 | */ |
12 | public class SortMapExample{ |
13 |
14 | /** |
15 | *Themainmethod. |
16 | * |
17 | *@paramargsthearguments |
18 | */ |
19 | public static void main(String[]args){ |
20 | //creatingunsortedmapofemployeeidasakeyandemployeenameasavalue |
21 | MapunsortMap= new HashMap(); |
22 | unsortMap.put( 10 , "Ashraf" ); |
23 | unsortMap.put( 5 , "Sara" ); |
24 | unsortMap.put( 6 , "Mohamed" ); |
25 | unsortMap.put( 20 , "Esraa" ); |
26 | unsortMap.put( 1 , "Bahaa" ); |
27 | unsortMap.put( 7 , "Dalia" ); |
28 | unsortMap.put( 8 , "Amira" ); |
29 | unsortMap.put( 99 , "Ahmed" ); |
30 | unsortMap.put( 50 , "Sama" ); |
31 | unsortMap.put( 2 , "Nada" ); |
32 | unsortMap.put( 9 , "Osama" ); |
33 |
34 | System.out.println( "UnsortMap......" ); |
35 | printMap(unsortMap); |
36 |
37 | //UsingthedefaultnaturalorderingofsortedmapIntegerkeywhichimplementComparableinterface |
38 | System.out.println( "\nSortedMapinascendingorder......" ); |
39 | MapascSortedMap= new TreeMap(); |
40 | ascSortedMap.putAll(unsortMap); |
41 | printMap(ascSortedMap); |
42 |
43 | //Forcingthedescendingorderbycreatingourowncomparatorthenpassingittothesortedmapatcreationtime |
44 | System.out.println( "\nSortedMapindescendingorder......" ); |
45 | MapdesSortedMap= new TreeMap( |
46 | new Comparator(){ |
47 |
48 | @Override |
49 | public int compare(Integero1,Integero2){ |
50 | return o2.compareTo(o1); |
51 | } |
52 |
53 | }); |
54 | desSortedMap.putAll(unsortMap); |
55 | printMap(desSortedMap); |
56 |
57 | } |
58 |
59 | /** |
60 | *Printsthemap. |
61 | * |
62 | *@parammapthemap |
63 | */ |
64 | public static void printMap(Mapmap){ |
65 | for (Map.Entryentry:map.entrySet()){ |
66 | System.out.println( "Key:" +entry.getKey()+ "Value:" |
67 | +entry.getValue()); |
68 | } |
69 | } |
70 |
71 | } |
2.2.Explanation:
Let’ssupposethatwewanttosortawhichcontainsagroupofemployeesaccordingtotheiridswhereweusetheemployeeidasakeyandtheemployeenameasavalue.AfterwecreateaMap
usingthisSortedMap
andthekeyofthisMap
isanMap
typewhichimplementstheInteger
interface,thekeysareorderedintheirnaturalordering.Also,wecanapplythedescendingorderbycreatingourownComparable
thenpassingittotheComparator
atcreationtime.SortedMap
2.3.Output:
01 | UnsortMap...... |
02 | Key:50Value:Sama |
03 | Key:1Value:Bahaa |
04 | Key:2Value:Nada |
05 | Key:99Value:Ahmed |
06 | Key:20Value:Esraa |
07 | Key:5Value:Sara |
08 | Key:6Value:Mohamed |
09 | Key:7Value:Dalia |
10 | Key:8Value:Amira |
11 | Key:9Value:Osama |
12 | Key:10Value:Ashraf |
13 |
14 | SortedMapinascendingorder...... |
15 | Key:1Value:Bahaa |
16 | Key:2Value:Nada |
17 | Key:5Value:Sara |
18 | Key:6Value:Mohamed |
19 | Key:7Value:Dalia |
20 | Key:8Value:Amira |
21 | Key:9Value:Osama |
22 | Key:10Value:Ashraf |
23 | Key:20Value:Esraa |
24 | Key:50Value:Sama |
25 | Key:99Value:Ahmed |
26 |
27 | SortedMapindescendingorder...... |
28 | Key:99Value:Ahmed |
29 | Key:50Value:Sama |
30 | Key:20Value:Esraa |
31 | Key:10Value:Ashraf |
32 | Key:9Value:Osama |
33 | Key:8Value:Amira |
34 | Key:7Value:Dalia |
35 | Key:6Value:Mohamed |
36 | Key:5Value:Sara |
37 | Key:2Value:Nada |
38 | Key:1Value:Bahaa |
相关文章推荐
- 2014由于在myeclipse5.5.1许可证
- 学习java第6天 模仿XP画板(10%)
- Java - The SortedMap Interface
- 006_03Java解析XML之DOM方式与SAX方式
- 在ubuntu中如何安装sun-java6-jdk
- java用正则表达式判断字符串中是否仅包含英文字母、数字和汉字
- spring实战学习笔记一2015年6月7号
- Java中的泛型方法
- java程序打包jar文件自带图片
- 老农解决猫狗鱼的问题(黑马基础习题)
- Eclipse下使用SVN插件从服务器获取工程
- java中使用正则表达式
- JAVA类的序列化
- java中的IO流(2)----读取文本数据
- javaweb 一个登陆界面
- java中的IO流(1)----读取二进制文件
- Java for LeetCode 207 Course Schedule【Medium】
- javaSWing程序命令行和IDE运行没有问题,打包成jar后直接双击运行部分功能不行
- 《Java从入门到精通》第五章学习笔记
- 226_尚学堂_高淇_java300集最全视频教程_【GOF23设计模式】_单例模式_应用场景_饿汉式_懒汉式