求助求助!
我有一个txt 文件 文件里每一行只有两个数字
12 14
12 15
13 16
13 29
13 30
等等要求找出每一行第一个比如是12 他之后的所有第二个数字的集合
比如输出结果为
12:14,15,16
13:16,29,30请问怎么存到arraylist里面
另外算法要求是在线性时间里完成
txt文件非常之大
我有一个txt 文件 文件里每一行只有两个数字
12 14
12 15
13 16
13 29
13 30
等等要求找出每一行第一个比如是12 他之后的所有第二个数字的集合
比如输出结果为
12:14,15,16
13:16,29,30请问怎么存到arraylist里面
另外算法要求是在线性时间里完成
txt文件非常之大
解决方案 »
- 关于JAVA Socket编程,我想实现 可以实现自建聊天室功能,当聊天室的人全部走了是聊天室服务器自动关闭。聊天室在网页上显示,用APPLET来写。 我以前没写过 请问下难度怎么样?或者解决方案?
- 可否通过全局变量来在两个线程之间通讯并且触发线程继续运行?
- 请问高手这个用java语言怎么实现?
- System.exit( n)究竟是什么意思?
- 请帮忙查错.在线等
- 我在学习java本地方法调用c,可是在linux不知道如何用cc编译?
- 那位知道xml怎样运行吗
- 怎么回事儿,急!
- 菜鸟求助
- 怎么用java调用C函数或者dll?
- 总感觉自己编程上缺点什么希望各位指点一下小弟
- i=i+1和i++以及i+=1的区别?
用Map<Integer,ArrayList<Integer>的形式存放
ArrayList<Integer> list = map.get(12);
if(list == null)
{
list = new ArrayList<Integer>();
}
list.add(14);//
try {
FileReader fr = new FileReader("C:\\Users\\chenxh\\Desktop\\测试数据.txt");
BufferedReader bf = new BufferedReader(fr);
String str = null;
Map map = new HashMap<String, ArrayList>();
List list = null;
while((str=bf.readLine()) != null){
String[] strs = str.split(" ");
Object obj = map.get(strs[0]);
if(obj == null)
list = new ArrayList();
else
list = (List)obj;
list.add(strs[1]);
map.put(strs[0], list);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
这类读写应该很快的,只不过文件越大可能消耗内存更大些,你可以试试。如果不能实现,需要优化怎么实现,同求解
要在hadoop上跑。。
还没跑你的程序 认真学习先
public static void analysisFile(String srcPath, String dstPath)
throws IOException {
BufferedReader in = null;
try {
in = new BufferedReader(new FileReader(srcPath));
PrintWriter out = null;
try {
out = new PrintWriter(dstPath);
analysisStream(in, out);
} finally {
if (out != null) {
out.close();
out = null;
}
}
} finally {
if (in != null) {
in.close();
in = null;
}
}
} /**
* 数据都有序排列,未作第2列的重复数据验证
*/
private static void analysisStream(BufferedReader in, PrintWriter out)
throws IOException {
String read = null;
String temp = null;
while ((read = in.readLine()) != null) {
if (read.trim().length() == 0) {
continue;
} int index = read.indexOf(' '); // 文件较大,效率上不采用split
if (index == -1) {
throw new IllegalArgumentException("Error Data:" + read);
} String num1 = read.substring(0, index).trim();
String num2 = read.substring(index + 1, read.length()).trim();
if (num1.equals(temp)) {
out.print(',' + num2);
} else {
if (temp != null) {
out.println();
}
out.print(num1 + ':' + num2);
}
out.flush();
temp = num1;
}
}可以保证任何大小的数据皆能运行...
12 13
12 14
12 16
13 12
13 16现在想让他输出为:
12:13,14,15;13
13,:12,16;12
15:null;12,16
16:null;13
就是分号之后的数字是当前这个key 跟着哪一个数字这个怎么解决呀??