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);

最新文章

  1. ECMASCript2015 提案 stage-3的对象展开运算符
  2. sql语法:inner join on, left join on, right join on详细使用方法
  3. 什么?你还不会写JQuery 插件
  4. Leetcode Permutations
  5. SQL复杂查询和视图(2)
  6. android 模拟器
  7. Jquery 1.8.2 click function - 动态
  8. C# 串口通信总结
  9. python 详解re模块
  10. C#运行时鼠标移动控件 - 调用Windows API(ReleaseCapture)
  11. c语言实现atoi和itoa函数。
  12. 【Xilinx-LVDS读写功能实现】-00-开始
  13. tbl.js div实现的表格控件,完全免费,不依赖jquery
  14. iOS获取WIFI的IP、子网掩码,以及域名转IP
  15. Android 百分比布局库(percent-support-lib) 解析与扩展
  16. 转自CSDN-详述 Java 中的别名现象
  17. English trip -- Phonics 6 元音字母 u + Unit 5 B课 review
  18. 连接Mysql错误 error 1042 can&#39;t get hostname for your address
  19. YII 自带验证码实现
  20. Struts2运行原理

热门文章

  1. DOM节点的修改
  2. 发送SMS短信(JSON) 转载
  3. BroadcastReceiver和EventBus区别是什么
  4. CyclicBarrier类合唱演绎
  5. postman+newman(2)
  6. PreparedStatement解决sql注入问题
  7. android menu的问题
  8. form 编译命令
  9. 移动前端头部标签(HTML5 meta)
  10. Python Django之路由系统