从文件中获取同学生日最多的一个

public class Birth
{
static HashMap<String, String> map = new HashMap<String, String>();

public static void main(String[] args)
{
File file = new File("D:/birthday.txt");
BufferedReader reader = null;
StringBuffer sb = new StringBuffer();
String str = "";
try
{
reader = new BufferedReader(new FileReader(file));
try
{
while ((str = reader.readLine()) != null)
{
String[] strs = str.split(" ");
map.put(strs[0], "");
}
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
catch (FileNotFoundException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
try
{
reader.close();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}

String max = searchSameBirthMax(map, getBirthday(map));
getNames(map,max);
}

public static int getCount(String s ,Map<String, String> birthMap)
{
int count =0;
for(String b:birthMap.values())
{
if(s.equals(b))
{
count++;
}
}
return count;
}
public static String searchSameBirthMax(Map<String, String> birthMap,Set<String> set)
{
Map<String, Integer> si = new HashMap<String, Integer>();

for(String b : set)
{
int count = getCount(b,birthMap);
si.put(b, count);
}
List<Integer> list = new ArrayList<Integer>();
for(Integer i : si.values())
{
list.add(i);
}
Collections.sort(list);

String max = list.get(list.size()-1).toString();
//System.out.println("======"+max);
String maxbirth ="";

Set<Entry<String,Integer>> entrySet = si.entrySet();
for (Map.Entry<String, Integer> entry : si.entrySet())
{
String birth = entry.getKey();
String count = entry.getValue().toString();
//System.out.println("======"+count);
if(count.equals(max))
{
maxbirth = birth;
}
}

return maxbirth;
}


public static void getNames(Map<String, String> birthMap,String key)
{
Set<Entry<String,String>> entrySet = birthMap.entrySet();
for (Entry<String, String> entry : entrySet)
{
String name = entry.getKey();
String count = entry.getValue().toString();

if(count.equals(key))
{
System.out.println(name);
}
}
}


public static Set<String> getBirthday(Map<String, String> birthMap)
{
Set<String> set = new HashSet<String>();
for (String str:birthMap.values())
{
set.add(str);
}

return set;
}

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值