我有一个比如叫做input.txt 文本 格式如下
12 Follower[13,14,16,]
12 Following[14,29,39,]
13 Following[12,]
14 Follower[12,48,98,]
14 Following [12,48,90,]
15 Following[16]
16 Follower[15]这个文本是按照第一列的数字(之后成为用户) 升序排列的
我想找的是 比如用户12 他既有follower 也有following 我想找方括号里面相同的元素 并输出存为output.txt 例如:12 [14]
14 [12,48]应该怎么做??
我的数据的第一列(用户id)是排序好了的 数值递增的
而第二列 则有的用户只有follower(如16)有的只有following(如13,15) 而有的是都有 譬如12,14请给我具体的code 跪求!谢谢100分哟~~~
12 Follower[13,14,16,]
12 Following[14,29,39,]
13 Following[12,]
14 Follower[12,48,98,]
14 Following [12,48,90,]
15 Following[16]
16 Follower[15]这个文本是按照第一列的数字(之后成为用户) 升序排列的
我想找的是 比如用户12 他既有follower 也有following 我想找方括号里面相同的元素 并输出存为output.txt 例如:12 [14]
14 [12,48]应该怎么做??
我的数据的第一列(用户id)是排序好了的 数值递增的
而第二列 则有的用户只有follower(如16)有的只有following(如13,15) 而有的是都有 譬如12,14请给我具体的code 跪求!谢谢100分哟~~~
FileInputStream fis = new FileInputStream("d:\\input.txt");
InputStreamReader isr = new InputStreamReader(fis);
BufferedReader br = new BufferedReader(isr);
String astr = br.readLine();
String bstr = null;
Pattern p = Pattern.compile("\\d+");
List lista = new ArrayList();
List listb = new ArrayList();
while ((bstr = br.readLine()) != null) {
String[] aarray = astr.split(" ");
String[] barray = bstr.split(" ");
if (aarray[0].equals(barray[0]) && astr.contains("Follower")
&& bstr.contains("Following")) {
Matcher am = p.matcher(aarray[1]);
while(am.find()){
lista.add(am.group());
}
Matcher bm = p.matcher(barray[1]);
while(bm.find()){
listb.add(bm.group());
}
lista.retainAll(listb); if(lista.size() > 0){
System.out.print(barray[0] +" ");
System.out.print("[");
for(int i = 0;i < lista.size();i++){
System.out.print(lista.get(i));
if(i != lista.size() - 1){
System.out.print(",");
}
}
System.out.println("]");
}
lista.clear();
listb.clear();
}
astr = bstr;
}
br.close();
}写了段代码,但要求你文档里每行规范点,不要有多余的空格。
14 Following [12,48,90,] 多了个空格。文本写成这样:
12 Follower[13,14,16,]
12 Following[14,29,39,]
13 Following[12,]
14 Follower[12,48,98,]
14 Following[12,48,90,]
15 Following[16]
16 Follower[15]
另外eclipse报错了Exception in thread "main" java.lang.Error: Unresolved compilation problems:
Pattern cannot be resolved to a type
Pattern cannot be resolved
List cannot be resolved to a type
List cannot be resolved to a type
Matcher cannot be resolved to a type
Matcher cannot be resolved to a type at find.main(find.java:12)
把包引进去啊。import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
有一点 刚才忘记掉了。。
但是run了以后没有结果呢
package com.zf.test;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;public class Test5 { public static void parser() throws Exception{
Map<String , List<String>> map = new LinkedHashMap<String, List<String>>();
Map<String, String[]> result = new LinkedHashMap<String, String[] >();
File file = new File("E:\\360data\\重要数据\\桌面\\input.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
String line = null;
Pattern p1 = Pattern.compile("^(\\d+)*+");
Pattern p2 = Pattern.compile("\\[.+?\\]");
Pattern p3 = Pattern.compile("\\d+");
while((line = br.readLine()) != null){
Matcher m1 = p1.matcher(line);
Matcher m2 = p2.matcher(line);
String tmp = null;
if(m1.find()){
tmp = m1.group(1);
if(!map.containsKey(tmp))
map.put(tmp, new LinkedList<String>() );
}
if(m2.find()){
String str = m2.group();
Matcher m3 = p3.matcher(str);
while(m3.find()){
map.get(tmp).add(m3.group());
}
}
}
//去掉单个元素
Set<String> keySet = map.keySet();
for (String key : keySet) {
List<String> val = map.get(key);
for (int i = val.size() - 1; i >= 0 ; i--) {
String s = val.remove(i);
if(val.contains(s)){
val.add(s);
}
}
}
//去掉重复元素 ,得到结果集
Set<String> ks = map.keySet();
for (String key : ks) {
List<String> val = map.get(key);
HashSet<String> set = new HashSet<String>(val);
String arr[] = set.toArray(new String[set.size()]);
Arrays.sort(arr); //排序
result.put(key, arr);
}
//写入文件
File outFile = new File("E:\\360data\\重要数据\\桌面\\output.txt");
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile)));
Set<Entry<String, String[]>> set = result.entrySet();
for (Entry<String, String[]> entry : set) {
if(entry.getValue().length > 0){
bw.write(entry.getKey() + " " + Arrays.toString(entry.getValue()) + "\r\n");
}
}
bw.flush() ; bw.close();
} public static void main(String[] args) throws Exception {
parser();
System.out.println("OK");
}
}
Exception in thread "main" java.lang.Error: Unresolved compilation problem: at Test5.main(Test5.java:82)
Exception in thread "main" java.lang.Error: Unresolved compilation problem: 这是编译器没有通过就强制运行的意思。你编译器哪一行报错?
import java.util.*;
import java.util.regex.*;
public class Test {
public static void main(String[] args) throws Throwable {
Map<String, Map<String,List<String>>> map = new LinkedHashMap<String, Map<String, List<String>>>();
Scanner sc = new Scanner(new FileInputStream("input.txt"));
String regex = "(\\d+)\\s+(\\w+)\\s*\\[\\s*((\\d+[,]?\\s*)+)\\s*\\]";
Pattern p = Pattern.compile(regex);
//读文件
while (sc.hasNextLine()) {
String s = sc.nextLine();
if (!s.matches(regex)) continue;
Matcher m = p.matcher(s);
m.find();
String key1 = m.group(1);
String key2 = m.group(2);
String value = m.group(3);
if (!map.containsKey(key1)) {
map.put(key1, new HashMap<String, List<String>>());
}
Map<String, List<String>> sub = map.get(key1);
if (!sub.containsKey(key2)) {
sub.put(key2, new ArrayList<String>());
}
sub.get(key2).addAll(Arrays.asList(value.split(",")));
}
sc.close(); //写文件
PrintStream ps = new PrintStream("output.txt");
for (Map.Entry<String, Map<String, List<String>>> e1 : map.entrySet()) {
Map<String, List<String>> sub = e1.getValue();
if (sub.size() > 1) { //存在Follower和Following的情况才做处理
List<String> list = null;
for (Map.Entry<String, List<String>> e2 : sub.entrySet()) {
if (list == null) list = e2.getValue();
else list.retainAll(e2.getValue());
}
ps.printf("%s [", e1.getKey());
for (String n : list) {
ps.printf("%s,", n);
}
ps.println("]");
}
}
ps.close();
}
}
刚测试了下,针对楼主给的数据,5楼的代码是OK的,从新建个类,把CLASS里面的东西复制,包导入就可以了。
如果有数据校验需求,或者有其他错误数据可能性,要加东西