天天看点

关于HashMap的一些东东

最近在做一个项目,要求是不能用数据库,数据是从一个文本文件中读取。

于是我就采用HashMap来实现。

首先是从文件中读取出全部数据放入ArrayList中,然后把ArrayList放入HashMap中,然后给这个HashMap创建2个索引,用于展示数据的时候查询方便。

public Map loadGameSchedule(String fileName) throws Exception
	{
		Map result = new HashMap();
		BufferedReader reader = new BufferedReader(new FileReader(fileName));
		//初始化数组
		ArrayList beginDateList = new ArrayList(1000);
		ArrayList beginTimeList = new ArrayList(1000);
		ArrayList endTimeList = new ArrayList(1000);
		ArrayList hostTeamList = new ArrayList(1000);
		ArrayList visitingTeamList = new ArrayList(1000);
		ArrayList remarkList = new ArrayList(1000);
		
		//读取文件,直到文件结束。(返回null表示文件结束)
		String line = null;
		line = reader.readLine();
		while(line != null)
		{
			System.out.println(line);
			String [] param = line.split("\\|");
			
			String beginDate = param[0];
			String beginTime = param[1];
			String endTime = param[2];
			String hostTeam = param[3];
			String visitingTeam = param[4];
			String remar ="";
			
			if(param.length==5)
			{
				try
				{
					remar = param[5]==null?"":param[5];
				}
				catch(Exception e)
				{
					
				}
			}
			
			beginDateList.add(beginDate);
			beginTimeList.add(beginTime);
			endTimeList.add(endTime);
			hostTeamList.add(hostTeam);
			visitingTeamList.add(visitingTeam);
			remarkList.add(remar);
			
			line = reader.readLine();
		}
		
		//填充MAP对象
		result.put("date", beginDateList);
		result.put("beginTime", beginTimeList);
		result.put("endTime", endTimeList);
		result.put("hostTeam", hostTeamList);
		result.put("visitingTeam", visitingTeamList);
		result.put("remark", remarkList);
		reader.close();
		return result ;
	}
           

 创建索引:

public void createScheduleIndex(Map dataSet,Map dataIdxMap,Map teamIdxMap)
	{
		//取出比赛日期
		ArrayList beginDateList = (ArrayList)dataSet.get("date");
		//取出主队
		ArrayList hostTeamList = (ArrayList)dataSet.get("hostTeam");
		//取出客队
		ArrayList visitingTeamList = (ArrayList)dataSet.get("visitingTeam");
		
		ArrayList dataIdxList = new ArrayList(1000);
		int totalSize = beginDateList.size();
		
		for (int i = 0; i < totalSize; i++)
		{
			String beginDate = (String)beginDateList.get(i);
			String hostTeam = (String)hostTeamList.get(i);
			String visitingTeam = (String)visitingTeamList.get(i);
			
			
			
			ArrayList tempList = (ArrayList)dataIdxMap.get(beginDate);
			if(null == tempList)
			{
				tempList =  new ArrayList();
			}
			
			tempList.add(i);
			
			dataIdxMap.put(beginDate, tempList);
			
			
			
			int beginYear = Integer.parseInt(beginDate.substring(0,2));
			int beginMonth = Integer.parseInt(beginDate.substring(2,4));
			
			String sYear = "";
			String sMonth = "";
			//满足格式要求
			if(beginYear<10)
			{
				sYear = "0" + beginYear;
			}
			else
			{
				sYear = Integer.toString(beginYear);
			}
			if(beginMonth<10)
			{
				sMonth =  "0" + beginMonth;;
			}
			else
			{
				sMonth = Integer.toString(beginMonth);
			}
			
			String hostTeamAndBeginTime = hostTeam+sYear+sMonth;
			String visitingTeamAndBeginTime = visitingTeam+sYear+sMonth;
			
			ArrayList tempList1 = (ArrayList)teamIdxMap.get(hostTeamAndBeginTime);
			//printMap(teamIdxMap);
			//System.out.println(hostTeamAndBeginTime+"=hostTeamAndBeginTime="+tempList1);
			
			if(null == tempList1)
			{
				tempList1 =  new ArrayList();
			}
			tempList1.add(i);
			teamIdxMap.put(hostTeamAndBeginTime, tempList1);
			
			
			ArrayList tempList2 = (ArrayList)teamIdxMap.get(visitingTeamAndBeginTime);
			if(null == tempList2)
			{
				tempList2 =  new ArrayList();
			}
			tempList2.add(i);
			teamIdxMap.put(visitingTeamAndBeginTime, tempList2);
			
		}
	}
           

打印函数:

public static void printMap(Map map)
	{
		/*Iterator it = map.keySet().iterator();
		while (it.hasNext())
		{
			String key;
			key = (String) it.next();
			System.out.println(key + ":" + map.get(key));
		}
		*/
		Set<Map.Entry<String, Integer>> set = map.entrySet();

		Iterator iter = set.iterator();
		while (iter.hasNext())
		{
			Map.Entry<String, Integer> entry = (Map.Entry<String, Integer>) iter
					.next();
			System.out.print(entry.getKey());
			System.out.println(":" + entry.getValue());
		}
	}
           

 测试:

//Calendar now = Calendar.getInstance();
		/*	now.before(arg0)
		//now.set(Calendar.HOUR_OF_DAY, arg1)
		System.out.println(now.get(Calendar.HOUR_OF_DAY)+":"+now.get(Calendar.MINUTE));
		System.out.println("11:30".split(":")[0]);
		now.set(Calendar.YEAR, arg1)
		now.set(Calendar.MONTH, arg1)
		now.set(Calendar.DAY_OF_MONTH, arg1)
		now.set(Calendar.HOUR_OF_DAY, arg1)
		now.set(Calendar.MINUTE, arg1)
		now.set(Calendar.SECOND, arg1)*/
		
		/*now.set(Calendar.MONTH, 11);
		now.set(Calendar.DAY_OF_MONTH, now.get(Calendar.DAY_OF_MONTH)+1);
		int year = now.get(Calendar.YEAR);
		int month = now.get(Calendar.MONTH)+1;
		int day = now.get(Calendar.DAY_OF_MONTH);
		String monthAndDay = year +""+month +""+day;
		System.out.println(monthAndDay);
		*/
		
		/*Calendar now = Calendar.getInstance();
		int month = now.get(Calendar.MONTH)+1;
		int day = now.get(Calendar.DAY_OF_MONTH);
		String monthAndDay = month +"月"+day;
		System.out.println(monthAndDay);*/
		
		
		Nba nba = new Nba();
		
		
		Map dataSet = nba.loadGameSchedule("src/nab.txt");
		
		Map dataIdxMap = new HashMap();
		Map teamIdxMap = new HashMap();
		/*Map nameIdxMap = new HashMap();*/
		nba.createScheduleIndex(dataSet, dataIdxMap,teamIdxMap);
		
		
		
		//nba.createScheduleIndex(dataSet,dataIdxMap,teamIdxMap);
		
		printMap(dataSet);
		System.out.println("******************************************");
		printMap(dataIdxMap);
		System.out.println("******************************************");
		printMap(teamIdxMap);
		
		Calendar today = Calendar.getInstance();
		//today.set(arg0, arg1)
		
		
		//System.out.println(s1.split("|")[1]);