Nathing
mingwjj
全部博文(111)
j2se(1)
hibernate(0)
2016年(2)
2015年(1)
2014年(31)
2012年(2)
2011年(9)
2010年(36)
2009年(30)
kgdjszx
备案出行
hzlzy
zhaoyh
zyd10137
wddwr730
2_gougou
shenhp
yingyife
分类: Java
2010-10-29 23:02:41
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.Comparator; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.TreeSet; /** * 打印出重复的姓名和重复的次数,并按重复次数排序 * @author Administrator * */ public class GetNameTest { /** * @param args * @throws IOException */ public static void main(String[] args) { Map<String, Integer> results = new HashMap<String, Integer>(); InputStream is = GetNameTest.class.getResourceAsStream("info.txt"); BufferedReader br = new BufferedReader(new InputStreamReader(is)); String line = ""; String name = ""; Integer value = 0; //1.把从文件读取的数据存在Map中,以name为key,name出现的次数为value try { while ((line = br.readLine()) != null) { String lines[] = line.split(","); if (lines.length == 3) { name = lines[1]; value = results.get(name); if (value == null) { value = 0; } results.put(name, value + 1); } } } catch (IOException e) { e.printStackTrace(); } System.out.println(results); //按一定的比较策略将map中value大于1的数据构建成user对象放入TreeSet中 TreeSet<User> treeSet = new TreeSet<User>( new Comparator<Object>() { @Override public int compare(Object o1, Object o2) { User u1 = (User)o1; User u2 = (User)o2; System.out.println("Comparator strategy"); if (u1.getValue()>u2.getValue()) return 1; else if (u1.getValue()<u2.getValue()) return -1; else
//不能返回0,否则,那些个数相同的其他姓名就打印不出来 return u1.getName().compareTo(u2.getName()); } } ); Iterator<String> iter = results.keySet().iterator(); while (iter.hasNext()) { name = (String)iter.next(); if (results.get(name) > 1) { treeSet.add(new User(name,results.get(name))); } } Iterator<User> iterUser = treeSet.iterator(); while (iterUser.hasNext()) { User u = (User)iterUser.next(); System.out.println(u.getName() +":" + u.getValue()); } } } class User { private String name; private Integer value; public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getValue() { return value; } public void setValue(Integer value) { this.value = value; } public User(String name, Integer value) { super(); this.name = name; this.value = value; } /** * 此方法没有执行. */ @Override public boolean equals(Object obj) { boolean flag = false; User user = (User)obj; if (this.value == user.value && this.name.compareTo(user.name) == 0) flag = true; System.out.println("User equals method!"); return flag; } }
//
return u1.getName().compareTo(u2.getName()); } } ); Iterator<String> iter = results.keySet().iterator(); while (iter.hasNext()) { name = (String)iter.next(); if (results.get(name) > 1) { treeSet.add(new User(name,results.get(name))); } } Iterator<User> iterUser = treeSet.iterator(); while (iterUser.hasNext()) { User u = (User)iterUser.next(); System.out.println(u.getName() +":" + u.getValue()); } } } class User { private String name; private Integer value; public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getValue() { return value; } public void setValue(Integer value) { this.value = value; } public User(String name, Integer value) { super(); this.name = name; this.value = value; } /** * 此方法没有执行. */ @Override public boolean equals(Object obj) { boolean flag = false; User user = (User)obj; if (this.value == user.value && this.name.compareTo(user.name) == 0) flag = true; System.out.println("User equals method!"); return flag; } }
上一篇:字符类型统计
下一篇:Java序列化算法透析
登录 注册