HashMap的put()方法

java Debug ------------------集合 HashMap

需求: 希望从文件里读数据,每一行都存到HashMap中,key是每一行第一个段,Value是ArrrayList对象,key如果有重复责存入同一个ArrayList中;

budge 修复后:

 
  
if(pName_file.equals(pName_map))
{
/// sop("if ::" + pName_map); list.add(line);
}
else 
{/// sop("else ::" + pName_file);hm.put(pName_map, new ArrayList(list)); // Debug 此处的value必须要new 一个新的对象,不然后面list对象改动map中也会改变
pName_map = pName_file;
list.clear();
list.add(line);
}

 
  
完整代码:

class HashMapTest
{
	
	public static void main(String[] args)
	{
		
		readFromFile();
		
	}

	public static void readFromFile()
	{
		HashMap hm = new HashMap();
		List list = new ArrayList();

		BufferedReader bufr = null;

		String pName_map = null;
		String[] str_arr = null;
		String line = null;
		try
		{
			//打开文件
			File f = f = new File("D:\\桌面\\xxxxxxxxxxx\\yyyyyyyyyy\\zzzzzzzzzzz\\test");
			bufr =new BufferedReader( new InputStreamReader(new FileInputStream(f)));
		
			// 获取 文件第一行的包名 
			if ((line = bufr.readLine()) != null)
			{
				str_arr = line.split(";");
				assert(str_arr.length >= 3);
				pName_map = str_arr[0];		
		//		cname = str[1];		
		//		mname = str[2];		
				
///				sop("FIRST LINE:" + line);
				list.add(line);
			}
			

			//在读取文件信息 到HashMap
			while((line = bufr.readLine()) != null)
			{
				
				str_arr = line.split(";");
				assert(str_arr.length >= 3);
				String pName_file = str_arr[0];
				
///				sop(pName_file);
///				sop(pName_map);
				if(pName_file.equals(pName_map))		// 注意对象的比较 只能是equals   
				{
///					sop("if ::" + pName_map);
					list.add(line);
				}
				else 
				{
///					sop("else ::" + pName_file);
					hm.put(pName_map, new ArrayList(list));        // Debug 此处的value必须要new 一个新的
					pName_map = pName_file;	
					list.clear();
					list.add(line);
					
				}
			}
			hm.put(pName_map, new ArrayList(list));

			output(hm);
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			if (bufr != null)
			{
				try
				{
					bufr.close();
				}
				catch (Exception e)
				{
					e.printStackTrace();
				}
				
			}			
		}
	}

	public static void output(Map map)
	{  
      if (map != null)
		 {  
			 Object key = null;  
			 Object value = null;  
			 //使用迭代器遍历Map的键,根据键取值  
			 Iterator it = map.keySet().iterator();  
			 while (it.hasNext())
			 {  
				key = it.next();  
				value = map.get(key); 
//				sop(value.toString());
				ArrayList v = (ArrayList)value;
//				sop(v.toString());
				System.out.println("key: " + key + "; value: ");  
				
				
				Object[] objArr = v.toArray();
				for (int i = 0; i < objArr.length ; i++ )
				{
					sop(objArr[i]);
				}
				
				/*
				Iterator it1 = v.listIterator();

				while(it1.hasNext())
				{
					sop(it1.next() + "\n");
				}*/
				
			 }  
			/*
			 //或者使用迭代器遍历Map的记录Map.Entry  
			 Map.Entry entry = null;  
			 it = map.entrySet().iterator();  
			 while (it.hasNext())
			 {  
				//一个Map.Entry代表一条记录  
				entry = (Map.Entry)it.next();  
				//通过entry可以获得记录的键和值  
				//System.out.println("key: " + entry.getKey() + "; value: " + entry.getValue());  
			 } */
		 }
	  }

	public static void sop(Object obj)
	{
		System.out.println(obj);
	}
}


 
  
 
 

你可能感兴趣的:(Debug)