循环保存分组记录信息

今天做了一个程序,用来将同一分组的信息按组进行保存的功能,写的有什么问题希望大家帮我检查检查.

public class GroupRecord {
	private Map<String,StringBuffer> record = new HashMap<String,StringBuffer>();
	private LinkedList<String> groupNames = new LinkedList<String>();
	/**
	 * create new group
	 * @param groupName
	 */
	public boolean creatGroup(String groupName) {
		if(record.get(groupName)!= null)return false;
		record.put(groupName, new StringBuffer("\n*************** This is Group "+groupName+":\n"));
		groupNames.addFirst(groupName);
		return true;
	}
	/**
	 * add a record to "groupName" group 
	 * @param groupName 
	 * @param str the add record
	 * @throws NotFoundGroupException 
	 */
	public boolean addRecord(String groupName,String str) throws NotFoundGroupException {
		if(record.get(groupName)== null) {
			throw new NotFoundGroupException(groupName);
		}
		record.get(groupName).append(str+"\n");
		return true;
	}
	/**
	 * remove a group
	 * @param groupName
	 * @return
	 */
	public StringBuffer removeGroup(String groupName) throws NotFoundGroupException {
		if(record.get(groupName)== null) {
			throw new NotFoundGroupException(groupName);
		}
		return record.remove(groupName);
	}
	/**
	 * return all group
	 * @return
	 */
	public Collection<StringBuffer> getAllGroup() {
		return record.values();
	}
	/**
	 * This method in to the designated grouping add 
	 * records to judge grouping exists, if there is no first create then add 注:此方法运行比较慢
	 * @param groupName
	 * @param str the add record
	 * @return
	 */
	public boolean addRecordAutoCreatGroup(String groupName,String str) {
		if(record.get(groupName)== null) {
			creatGroup(groupName);
		}
		try {
			addRecord(groupName,str);
		} catch (NotFoundGroupException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}
	/**
	 * get all the group name from GroupRecord
	 * @return 
	 */
	public LinkedList<String> getAllGroupName() {
		return groupNames;
	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值