java学习总结(二)

这篇博客详细介绍了Java中的HashMap、LinkedList、TreeMap、Hashtable等集合类的特性和应用场景,并探讨了Collections工具类的常用方法。文章还涵盖了异常处理的概念、处理方式,包括try-catch-finally和throws关键字的使用,以及File类的操作。最后,作者提到了IO流的FileInputStream和FileOutputStream,并简单提及了方法递归的基本概念和应用。
摘要由CSDN通过智能技术生成

HashMap

 *HashMap集合本身基于哈希表

 * 它可以保证键的唯一性(Map都是针对键有效),对象作为键要保证重写了equals()方法

 * HashMap<Student,String>

例子:

                public static void main(String[] args) {
		HashMap<Student,String> map = new HashMap<Student,String>();
		Student s1 = new Student("西施", 27) ;
		Student s2 = new Student("杨贵妃", 35) ;
		Student s3 = new Student("王昭君", 30) ;
		Student s4 = new Student("貂蝉",28) ;
		Student s5 = new Student("西施", 27) ;
	
		
		//添加元素
		map.put(s1, "元朝") ;
		map.put(s2, "宋朝") ;
		map.put(s3, "唐朝") ;
		map.put(s4, "清朝") ;
		map.put(s5, "金") ;
		
		Set<Student> set = map.keySet();
		for(Student s : set) {
			String value = map.get(s);
			System.out.println(s.getName()+"---"+s.getAge()+"---"+value);
		}
	}
结果:
杨贵妃---35---宋朝
王昭君---30---唐朝
西施---27---金
貂蝉---28---清朝

LinkedList

 *LinkedHashMap<K,V> :是Map接口基于哈希表和链接列表实现的
 *哈希表保证键的唯一性

 *链接列表保证元素有序性(存储和取出一致)

  例子:

                public static void main(String[] args) {
		LinkedHashMap<String, String> map = new LinkedHashMap<String,String>() ;
		
		//添加元素
		map.put("it001", "hello");
		map.put("it002", "mysql");
		map.put("it003", "world");
		map.put("it004", "javaweb");
		map.put("it001", "javaee");
		
		Set<String> set = map.keySet() ;
		for(String key :set) {
			String value = map.get(key) ;
			System.out.println(key+"----"+value);
		}
	}
结果:
it001----javaee
it002----mysql
it003----world
it004----javaweb

TreeMap

 *TreeMap基于红黑树结构的Map接口的实现

   数据按一定的规则进行排序存储,自定义类作为键有两种排序方法:

     1)自然排序(自定义类实现Comparable接口并且重写其中的compareTo方法

     2)比较器排序,Comparator 重写compare方法

实例:

                public static void main(String[] args) {
		
		//创建一个TreeMap集合,使用比较器排序的方式
		//匿名内部类的方式
		TreeMap<Student, String>  tm = new TreeMap<Student,String>(new Comparator<Student>() {

			@Override
			public int compare(Student s1, Student s2) {
				
				//主要条件:年龄从小到大
				int num =s1.getAge() -s2.getAge() ;
				//年龄相同,不一定姓名一样
				int num2 = num ==0 ? s1.getName().compareTo(s2.getName()): num ;
				return num2 ;
			}
			
		} );
		
		//创建学生对象
		Student s1 =  new Student("唐伯虎", 28) ;
		Student s2 =  new Student("杜甫", 35) ;
		Student s3 =  new Student("李白", 40) ;
		Student s4 =  new Student("李清照", 32) ;
		Student s5 =  new Student("唐伯虎", 28) ;
		Student s6 =  new Student("苏轼", 35) ;
		
		//添加到集合中
		tm.put(s1, "宋代") ;
		tm.put(s2, "唐代") ;
		tm.put(s3, "唐代") ;
		tm.put(s4, "宋代") ;
		tm.put(s5, "清代") ;
		tm.put(s6, "清代") ;
		
		
		//遍历
		Set<Student> set = tm.keySet() ;
		for(Student key :set) {
			String value = tm.get(key) ;
			System.out.println(key.getName()+"---"+key.getAge()+"---"+value);
		}
	}
结果:
唐伯虎---28---清代
李清照---32---宋代
杜甫---35---唐代
苏轼---35---清代
李白---40---唐代

Hashtable

 *        HashMap集合和Hashtable的区别?
 *        共同点:都是map接口的实现类,都是基于哈希表的实现类
 *        Has

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值