TreeMap集合特点、排序原理
TreeMap特點(類似于TreeSet):
1.無序,不允許重復(無序指元素順序與添加順序不一致)
2.TreeMap集合默認會對鍵進行排序,所以鍵必須實現自然排序和定制排序中的一種
3..底層使用的數據結構是二叉樹
兩種排序的用法(參照TreeSet集合):
1.TreeSet集合排序方式一:自然排序Comparable
http://blog.csdn.net/baidu_37107022/article/details/70207564
2.TreeSet集合排序方式二:定制排序Comparator
http://blog.csdn.net/baidu_37107022/article/details/70207633
練習:
1,創建公司Company類,擁有屬性:no(公司編號)、name(公司名稱)、num(公司人數)、founder(創始人)、info(公司簡介),要求屬性進行封裝即:屬性私有并提供公有方法。
(a)請根據下列信息創建5個公司對象,各屬性值來自下面的信息
”1001,百度,2000,李彥宏,全球最大的中文搜索引擎、致力于讓網民更便捷地獲取信息,找到所求。”
“1010,騰訊,10000,馬化騰,深圳市騰訊計算機系統有限公司成立于1998年11月,由馬化騰、張志東、許晨曄、陳一丹、曾李青五位創始人共同創立。”
“1020,阿里巴巴,20000,馬云,阿里巴巴網絡技術有限公司(簡稱:阿里巴巴集團)”
“1050,京東,8000,劉強東,京東(JD.com)是中國最大的自營式電商企業,2015年第一季度在中國自營式B2C電商市場的占有率為56.3%。”
“1030,小米,5000,雷軍,小米公司成立于2010年4月,是一家專注于智能產品自主研發的移動互聯網公司。”
(b)請將上述的5個對象添加到TreeMap
package TreeMap;import java.text.CollationKey; import java.text.Collator;/*** 創建公司Company類,擁有屬性:no(公司編號)、* name(公司名稱)、num(公司人數)、founder(創始人)、* info(公司簡介),要求屬性進行封裝即:屬性私有并提供公有方法。* @author Administrator**/ public class Company implements Comparable<Company>{private int no;private String name;private int num;private String founder;private String info;public int getNo() {return no;}public void setNo(int no) {this.no = no;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getNum() {return num;}public void setNum(int num) {this.num = num;}public String getFounder() {return founder;}public void setFounder(String founder) {this.founder = founder;}public String getInfo() {return info;}public void setInfo(String info) {this.info = info;}@Overridepublic String toString() {return "Company [no=" + no + ", name=" + name + ", num=" + num + ", founder=" + founder + ", info=" + info+ "]";}public Company(int no, String name, int num, String founder, String info) {super();this.no = no;this.name = name;this.num = num;this.founder = founder;this.info = info;}public Company() {super();}//要求按照以下規則依次排序:公司編號、公司名稱、創始人、公司人數,按升序排列@Overridepublic int compareTo(Company o) {//公司編號int a=this.no-o.no;if(a!=0){return a;}else{//公司名稱CollationKey key=Collator.getInstance().getCollationKey(this.name);CollationKey key2=Collator.getInstance().getCollationKey(o.name);int b=key.compareTo(key2);if(b!=0){return b;}else{//創始人CollationKey key3=Collator.getInstance().getCollationKey(this.founder);CollationKey key4=Collator.getInstance().getCollationKey(o.founder);int c=key3.compareTo(key4);if(c!=0){return c;}else {//公司人數return this.num-o.num;}}}}}測試代碼:
package TreeMap;import java.util.Set; import java.util.TreeMap;public class Test {public static void main(String[] args) {TreeMap<Company, String> map=new TreeMap<>();map.put(new Company(1001, "百度", 2000, "李彥宏", "全球最大的中文搜索引擎、致力于讓網民更便捷地獲取信息,找到所求"), "有錢的公司,任性!!");map.put(new Company(1010, "騰訊", 10000, "馬化騰", "深圳市騰訊計算機系統有限公司成立于1998年11月,由馬化騰、張志東、許晨曄、陳一丹、曾李青五位創始人共同創立"), "有錢的公司,任性!!");map.put(new Company(1020, "阿里巴巴", 20000, "馬云", "阿里巴巴網絡技術有限公司(簡稱:阿里巴巴集團)"),"有錢的公司,任性!!");map.put(new Company(1050, "京東", 8000, "劉強東", "京東(JD.com)是中國最大的自營式電商企業,2015年第一季度在中國自營式B2C電商市場的占有率為56.3%"),"有錢的公司,任性!!");map.put(new Company(1030, "小米", 5000, "雷軍", "小米公司成立于2010年4月,是一家專注于智能產品自主研發的移動互聯網公司"), "有錢的公司,任性!!");Set<Company> set=map.keySet();for (Company company : set) {System.out.println(company+","+map.get(company));}} }總結
以上是生活随笔為你收集整理的TreeMap集合特点、排序原理的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: HashMap集合
- 下一篇: Java常用集合体系以及相互区别