我有一个比如叫做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]下次LZ回复帖子就跑到前面去了,不用重复发帖的。
另外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 typeat find.main(find.java:12)
但是run了以后 没有结果输出
import java.util.*;
public class InputTest {
public static void main(String[] args) throws IOException {
File file = new File("D:/input.txt");
Scanner in = new Scanner(file);
Map<String, String> map_root = new HashMap<String, String>();
Map<String, String> map_rs = new HashMap<String, String>();
String[] str;String str1;
while (in.hasNextLine()) {
str = in.nextLine().split(" ");
if (map_root.containsKey(str[0])) map_root.put(str[0], map_root.get(str[0]) + " " + str[1]);
else map_root.put(str[0], str[1]);
}
for (String key : map_root.keySet()) {
str1 = map_root.get(key);
String[] str2,str5,str6;String str3,str4;
if (str1.indexOf(" ") != -1) {
str2 = str1.split(" ");
str3 = str2[0].substring(str2[0].indexOf("[") + 1, str2[0].lastIndexOf("]"));
str4 = str2[1].substring(str2[1].indexOf("[") + 1, str2[1].lastIndexOf("]"));
str5 = str3.split(",");
str6 = str4.split(",");
for (int j = 0; j < str5.length; j++) {
if ("" == str5[j]) continue;
for (int p = 0; p < str6.length; p++) {
if ("" == str6[p]) continue;
if (str5[j].equals(str6[p])) {
if (map_rs.containsKey(key)) map_rs.put(key, map_rs.get(key) + "," + str5[j]);
else map_rs.put(key, str5[j]);
}
}
}
}
}
StringBuffer buf=new StringBuffer();
for(String key:map_rs.keySet()){
buf.append(key+" "+"["+map_rs.get(key)+"]"+"\r\n");
}
FileOutputStream os=new FileOutputStream(new File("d:/output.txt"));
os.write(buf.toString().getBytes(), 0, buf.toString().getBytes().length);
}
}
at InputTest.main(InputTest.java:13)
at InputTest.main(InputTest.java:13)
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]
for (String key : map_root.keySet())
不太明白什么意思
for(int i:it){
System.out.print(i+" ");
}
System.out.println();
for(int i=0;i<it.length;i++){
System.out.print(it[i]+" ");
}
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();
}
}