该帖已经被评为新手帖
|
|
---|---|
作者 | 正文 |
发表时间:2009-08-13
最后修改:2009-08-13
Map<Integer, String> map = new TreeMap<Integer, String>(); while((temp = bfReader.readLine()) != null) { Integer t = getInteger(temp); if(map.containsKey(t)) { String value= map.get(t); temp += "\r\n" + value; } map.put(t, temp) ; } 上面是我的想法,可以解决TreeMap的key冲突问题 getInteger(temp);方法中我用的正则匹配。 |
|
返回顶楼 | |
发表时间:2009-08-13
最后修改:2009-08-13
public class App { public static void main(String[] args) { try { sort("src/main/resources/intel.txt", "target/sun.txt"); } catch (IOException e) { e.printStackTrace(); } } private static void sort(String source, String target) throws IOException { Scanner scanner = new Scanner(new File(source)); List<App.Item> items = new ArrayList<App.Item>(); while (scanner.hasNextLine()) { String[] tokens = scanner.nextLine().split(" "); items.add(new App.Item(tokens[0], Integer.parseInt(tokens[1]))); } scanner.close(); Collections.sort(items, new Comparator<App.Item>() { @Override public int compare(Item o1, Item o2) { return o1.getNumber().compareTo(o2.getNumber()); } }); PrintWriter writer = new PrintWriter(new BufferedWriter(new FileWriter( target))); for (App.Item item : items) { writer.println(item.getName() + " " + item.getNumber()); } writer.close(); } static class Item { private String name; private Integer number; public Item(String name, Integer number) { this.name = name; this.number = number; } public String getName() { return name; } public Integer getNumber() { return number; } } } |
|
返回顶楼 | |
发表时间:2009-08-14
我在东软某部门笔试真碰到了楼主说的这道题
|
|
返回顶楼 | |