高分100 求代码 两行数据找相同元素

mrsworf 2012-06-24 09:25:48
我有一个比如叫做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分哟~~~
...全文
249 13 打赏 收藏 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
fasttime 2012-06-25
  • 打赏
  • 举报
回复
来点实际的代码,方便你理解。

int[] it={1,2,2,45};
for(int i:it){
System.out.print(i+" ");
}
System.out.println();
for(int i=0;i<it.length;i++){
System.out.print(it[i]+" ");
}
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 的回复:]

引用 8 楼 的回复:

你的格式不规范导致的。注意空格情况,以下为规范文本:
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]

用……
[/Quote]
看看.keySet()这个方法,想想循环,你就知道了
qybao 2012-06-25
  • 打赏
  • 举报
回复
这个不是有人问过了吗?

import java.io.*;
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();
}
}
mrsworf 2012-06-24
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 的回复:]

你的格式不规范导致的。注意空格情况,以下为规范文本:
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]
[/Quote]
用您的跑出结果了!
for (String key : map_root.keySet())
不太明白什么意思
mrsworf 2012-06-24
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 的回复:]

你的格式不规范导致的。注意空格情况,以下为规范文本:
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]
[/Quote]
啊对谢谢指正 刚才直接就是把我自己给的例子粘贴过去了 忘了改了。。
谢谢
fasttime 2012-06-24
  • 打赏
  • 举报
回复
你的格式不规范导致的。注意空格情况,以下为规范文本:
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]
mrsworf 2012-06-24
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 的回复:]

呵呵。我也来写一个,欢迎大家指评。
Java code

import java.io.*;
import java.util.*;
public class InputTest {
public static void main(String[] args) throws IOException {
File file = new File("D:/input.tx……
[/Quote]
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
at InputTest.main(InputTest.java:13)
mrsworf 2012-06-24
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 的回复:]

呵呵。我也来写一个,欢迎大家指评。
Java code

import java.io.*;
import java.util.*;
public class InputTest {
public static void main(String[] args) throws IOException {
File file = new File("D:/input.tx……
[/Quote]
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
at InputTest.main(InputTest.java:13)
fasttime 2012-06-24
  • 打赏
  • 举报
回复
呵呵。我也来写一个,欢迎大家指评。

import java.io.*;
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);
}
}
mrsworf 2012-06-24
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 的回复:]

引用 2 楼 的回复:

引用 1 楼 的回复:

Java code
public static void main(String[] args) throws Exception {
FileInputStream fis = new FileInputStream("d:\\input.txt");
InputStreamReader isr = new InputStr……
[/Quote]
引了
但是run了以后 没有结果输出
结贴是美德 2012-06-24
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 的回复:]

引用 1 楼 的回复:

Java code
public static void main(String[] args) throws Exception {
FileInputStream fis = new FileInputStream("d:\\input.txt");
InputStreamReader isr = new InputStreamReader(fis);
……
[/Quote]

引包, 哎。。少写一点也不行。。。
mrsworf 2012-06-24
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]

Java code
public static void main(String[] args) throws Exception {
FileInputStream fis = new FileInputStream("d:\\input.txt");
InputStreamReader isr = new InputStreamReader(fis);
……
[/Quote]
我的文本都是规范的 要是想把结果输出到output.txt应该再加那句??
另外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)
brightyq 2012-06-24
  • 打赏
  • 举报
回复
public static void main(String[] args) throws Exception {
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回复帖子就跑到前面去了,不用重复发帖的。

62,620

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧