treeMap and treeSet
TreeSet:如果要对对象进行排序,对象类要实现Comparable接口!
TreeMap:如果要对对象进行排序,对象类要实现Comparable接口!
下面是我自己写的小程序主要传输对象
public class News implements Comparable<News>{ //news为传递的对象类
String title;
String content;
public News(String title, String content) {//构造带参函数
super();
this.title = title;
this.content = content;
}
public News() {无参函数
super();
}
@Override
public int compareTo(News o) {
// TODO Auto-generated method stub
@SuppressWarnings("unused")
News news = (News) o;
if(this.title.compareTo(o.title)>0){
return 1;
}else if(this.title.compareTo(o.title)<0){
return -1;
}else{
return 0;
}
}
}
//treeMap传递主函数里的
TreeMap<String, News> treeMap=new TreeMap<String,News>();
treeMap.put("3", new News("a","CJJDI"));
treeMap.put("1",new News("d","CJnsdkI"));
treeMap.put("b", new News("1","chuidil"));
treeMap.put("a", new News("4","cnsoai"));
Iterator ti1=hashMap.entrySet().iterator();
while(ti1.hasNext()){ //调用treemap的两个方法hasnext next
@SuppressWarnings("unchecked")
Entry<String, News> e1= (Entry<String, News>) ti1.next();
System.out.println("key:"+e1.getKey()+"value:"+e1.getValue().title+""+e1.getValue().content);
、
//hashset的传递对象主函数里的
HashSet<News> hashSet2=new HashSet<News>();
News news5=new News("G","cnidjdi");
News news6=new News("B","cnidjdi");
News news7=new News("P","cnidjdi");
hashSet2.add(news5);
hashSet2.add(news6);
hashSet2.add(news7);
Iterator<News> it2=hashSet2.iterator();
while(it2.hasNext()){
news6=it2.next();
System.out.print(news6.title);
System.out.println(news6.content);
最新文章
- ECMASCript2015 提案 stage-3的对象展开运算符
- sql语法:inner join on, left join on, right join on详细使用方法
- 什么?你还不会写JQuery 插件
- Leetcode Permutations
- SQL复杂查询和视图(2)
- android 模拟器
- Jquery 1.8.2 click function - 动态
- C# 串口通信总结
- python 详解re模块
- C#运行时鼠标移动控件 - 调用Windows API(ReleaseCapture)
- c语言实现atoi和itoa函数。
- 【Xilinx-LVDS读写功能实现】-00-开始
- tbl.js div实现的表格控件,完全免费,不依赖jquery
- iOS获取WIFI的IP、子网掩码,以及域名转IP
- Android 百分比布局库(percent-support-lib) 解析与扩展
- 转自CSDN-详述 Java 中的别名现象
- English trip -- Phonics 6 元音字母 u + Unit 5 B课 review
- 连接Mysql错误 error 1042 can&#39;t get hostname for your address
- YII 自带验证码实现
- Struts2运行原理