---
title: "循环保存分组记录信息"
author: "yongjiucui"
date: "2010-12-10T18:45:40Z"
tags: []
categories: ["编程语言"]
slug: "untitled"
draft: false
lastmod: "2010-12-10T18:45:40Z"
id: 838968
url: "https://cuiyj3.iteye.com/blog/838968"
---

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

```
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;
	}
}
```