JAVA按行读取文件如何判断两行内容是否相同

如题。。比如

aaa 

bbb 

ccc 

aaa 

eee 

fff 

eee


按行读取这个文件,如何判断两行是否相同?因为这里我需要将相同内容的所在行号记录下来,

比如上述就应该处理成这种形式:

(map类型<string,List<Integer>>)


       aaa:(1,4)

      bbb: (2)

      ccc: (3)

      eee: (5,7)

      fff: (6)


慕婉清6462132
浏览 995回答 3
3回答

慕容森

HashMap<String,List<Integer>> hm = new HashMap<String,List<Integer>>();&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; for (String s : text){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (hm.containsKey(s)){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; List<Integer> list = hm.get(s);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; list.add(index);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; hm.put(s, list);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; List<Integer> list = new ArrayList<Integer>();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; list.add(index);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; hm.put(s, list);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }

九州编程

这个Map的设计没有问题,如果不考虑效率,可以一行一行读,然后做相应操作啊。判断两行相等不就是用equals吗?补充:不完全代码:reader = new BufferedReader(new FileReader(file));int line = 1;String lineStr = null; //每一行文本while ((lineStr= reader.readLine()) != null) {&nbsp; &nbsp; //TODO 做相应的操作,楼下已提供&nbsp; &nbsp; line++; // 行号}

喵喵时光机

按照题主的说法,就使用keyExist来判断是否存在就可以了阿,不知道题主是需要有什么其他的要求;
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java