Java基础之集合工具类

集合工具类

这次的集合工具类算是对类集框架的一种扩充,有Stack类,Queue接口,Properties类,Collection类四种,他们各自的特色鲜明。

Stack类

Stack类是对栈的操作,继承于Vector类,不同的是他有自己的入栈,出栈操作方法。栈的元素全部出栈后再使用出栈将抛出空异常。

  • push()
  • pop()

注意

  • 栈的特征是先进后出,Stack类中的元素也是如此。
  • Iterator同样可以对Stack类迭代,但是不遵循先进后出的特点
  • 因为是Vector的子类,同样当入栈的数据的数据类型是自定义类时,需要重写equals方法才能正确进行remove删除操作和contains查询操作
Queue类

Queue接口定义的是一种单端队列的标准,直接继承于Collection接口,与List,Set 是同级别的接口。主要靠LinkedList子类和PriorityQueue子类实例化。当元素为空时输出null。

LinkedList子类和PriorityQueue子类 的区别:

  • PriorityQueue会对数据排序,而LinkedList不会添加的顺序就是输出的顺序

————————————————————————————————
Queue的主要方法:

  • offer()
  • poll()

注意:

  • Queue 接口的元素符合队列特征,先进先出。
  • 因为ProrityQueue子类通过ComparTo方法进行数据排序,所以当数据的数据类型是自定义类时要覆写ComparTo方法。
  • Queue 的对象需要重写equals方法才能正确进行remove删除操作和contains查询操作。

代码演示:

import java.util.PriorityQueue;
import java.util.Queue;

public class Demo4 {
	public static void main(String[] args) {
		Queue<Emp> queue = new PriorityQueue<Emp>();
		queue.offer(new Emp("入云龙",21));
		queue.offer(new Emp("智多星",11));
		queue.offer(new Emp("托塔天王",23));
		queue.remove(new Emp("托塔天王",23));
		System.out.println(queue.poll());
		System.out.println(queue.poll());
		System.out.println(queue.poll());
		System.out.println("**************");
	}
}

class Emp implements Comparable<Emp>{
	private String name;
	private int age;
	
	public Emp(String name,int age) {
		this.age = age;
		this.name = name;
	}
	public String toString() {
		return "姓名:"+this.name+",年龄:"+this.age;
	}
	
	public boolean equals(Object obj) {
		if(this == obj) {
			return true;
		}
		if("".equals(obj)) {
			return false;
		}
		if(!(obj instanceof Emp)) {
			return false;
		}
		Emp emp = (Emp)obj;
		return this.name.equals(emp.name)&& this.age==emp.age;
	}
	@Override
	public int compareTo(Emp emp) {
		// TODO Auto-generated method stub
		if(this.age>emp.age) {
			return 1;
		}
		if(this.age<emp.age) {
			return -1;
		}
		if(this.age == emp.age) {
			this.name.compareTo(emp.name);
		}
		return 0;
	}
}
Properties类

Properties 类继承于hashtable,和Map集合比较像可以存储二元偶数据,不同的是Properties类不使用泛型只能存储String类型数据。并且properties类提供了资源的输出和输入操作方法。

Properties的主要方法:

  • setProperty()
  • getProperty()
  • store()//资源的输出
  • load()//资源的输入,根据key查找

代码演示:

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.PriorityQueue;
import java.util.Properties;

public class Demo4 {
	public static void main(String[] args) throws FileNotFoundException, IOException {
		Properties ue = new Properties();
		ue.setProperty("智多星", "吴用");
		ue.setProperty("入云龙", "公孙胜");
		ue.setProperty("拼命三郎", "阮小二");
		ue.setProperty("托塔天王", "晁盖");
		ue.store(new FileOutputStream("D:\\a\\a.txt"),"english");
		ue.load(new FileInputStream("D:\\a\\a.txt"));
		System.out.println(ue.getProperty("智多星"));
		System.out.println(ue.getProperty("入云龙"));

		System.out.println("**************");
	}
}

Collections

Collections 完全和Collection类集没有关系,Collections是一个集合工具类,它可以操作Map类集和Collection类集,提供了很多便捷方法,例如二分查找。

Collections的主要方法:

  • addAll()
  • sort()
  • binarySearch()

注意:Collections的使用很有特色,先创建一个集合的对象,再使用Collections的方法对他操作。

代码演示:

public class Demo4 {
	public static void main(String[] args)  {
		List<String> ue = new ArrayList<String>();
		Collections.addAll(ue, "入云龙","智多星","托塔天王","平明三郎");
		Collections.sort(ue);
		System.out.println(Collections.binarySearch(ue, "入云龙"));
		System.out.println("**************");
	}
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值