Java 从arraylist中删除重复字符串<;对象>;
我的程序正在打开一个文件,然后保存它的字及其与文件开头的字节距离。虽然文件中有太多我不想要的重复单词。我也希望我的名单是按字母顺序排列的。问题是,当我修复订单时,副本会被弄乱,反之亦然。这是我的密码:Java 从arraylist中删除重复字符串<;对象>;,java,list,sorting,arraylist,duplicates,Java,List,Sorting,Arraylist,Duplicates,我的程序正在打开一个文件,然后保存它的字及其与文件开头的字节距离。虽然文件中有太多我不想要的重复单词。我也希望我的名单是按字母顺序排列的。问题是,当我修复订单时,副本会被弄乱,反之亦然。这是我的密码: import java.io.*; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.Ha
import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Set;
class MyMain {
public static void main(String[] args) throws IOException {
ArrayList<DictPage> listOfWords = new ArrayList<DictPage>();
LinkedList<Page> Eurethrio = new LinkedList<Page>();
File file = new File("C:\\Kennedy.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
//This will reference one line at a time...
String line = null;
int line_count=0;
int byte_count;
int total_byte_count=0;
int fromIndex;
int kat = 0;
while( (line = br.readLine())!= null ){
line_count++;
fromIndex=0;
String [] tokens = line.split(",\\s+|\\s*\\\"\\s*|\\s+|\\.\\s*|\\s*\\:\\s*");
String line_rest=line;
for (int i=1; i <= tokens.length; i++) {
byte_count = line_rest.indexOf(tokens[i-1]);
//if ( tokens[i-1].length() != 0)
//System.out.println("\n(line:" + line_count + ", word:" + i + ", start_byte:" + (total_byte_count + fromIndex) + "' word_length:" + tokens[i-1].length() + ") = " + tokens[i-1]);
fromIndex = fromIndex + byte_count + 1 + tokens[i-1].length();
if (fromIndex < line.length())
line_rest = line.substring(fromIndex);
if(!listOfWords.contains(tokens[i-1])){//Na mhn apothikevetai h idia leksh
//listOfWords.add(tokens[i-1]);
listOfWords.add(new DictPage(tokens[i-1],kat));
kat++;
}
Eurethrio.add(new Page("Kennedy",fromIndex));
}
total_byte_count += fromIndex;
Eurethrio.add(new Page("Kennedy", total_byte_count));
}
Set<DictPage> hs = new HashSet<DictPage>();
hs.addAll(listOfWords);
listOfWords.clear();
listOfWords.addAll(hs);
if (listOfWords.size() > 0) {
Collections.sort(listOfWords, new Comparator<DictPage>() {
@Override
public int compare(final DictPage object1, final DictPage object2) {
return object1.getWord().compareTo(object2.getWord());
}
} );
}
//Ektypwsh leksewn...
for (int i = 0; i<listOfWords.size();i++){
System.out.println(""+listOfWords.get(i).getWord()+" "+listOfWords.get(i).getPage());
}
for (int i = 0;i<Eurethrio.size();i++){
System.out.println(""+Eurethrio.get(i).getFile()+" "+Eurethrio.get(i).getBytes());
}
}
}
import java.io.*;
导入java.util.ArrayList;
导入java.util.array;
导入java.util.Collections;
导入java.util.Comparator;
导入java.util.HashSet;
导入java.util.LinkedList;
导入java.util.Set;
类MyMain{
公共静态void main(字符串[]args)引发IOException{
ArrayList ListofWord=新的ArrayList();
LinkedList Eurethrio=新建LinkedList();
File File=新文件(“C:\\Kennedy.txt”);
BufferedReader br=新的BufferedReader(新的InputStreamReader(新文件InputStream(文件)));
//这将一次引用一行。。。
字符串行=null;
int line_count=0;
int字节计数;
int总字节计数=0;
int-fromIndex;
int kat=0;
而((line=br.readLine())!=null){
行数++;
fromIndex=0;
String[]tokens=line.split(“,\\s+\\\s*\\\”\\s*\\\\s+\\\.\\s*\\\\s*\\:\\s*”);
字符串行\u rest=行;
对于(int i=1;i 0){
Collections.sort(listOfWords,newcomparator(){
@凌驾
公共整数比较(最终DictPage object1,最终DictPage object2){
返回object1.getWord().compareTo(object2.getWord());
}
} );
}
//Ektypwsh leksewn。。。
对于(int i=0;i使用此项
public void stripDuplicatesFromFile(String filename) {
try {
BufferedReader reader = new BufferedReader(new FileReader(filename));
Set<String> lines = new HashSet<String>();
String line;
while ((line = reader.readLine()) != null) {
lines.add(line);
}
reader.close();
BufferedWriter writer = new BufferedWriter(new FileWriter(filename));
for (String unique : lines) {
writer.write(unique);
writer.newLine();
}
writer.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void stripDuplicatesFromFile(字符串文件名){
试一试{
BufferedReader reader=新的BufferedReader(新文件读取器(文件名));
Set line=new HashSet();
弦线;
而((line=reader.readLine())!=null){
行。添加(行);
}
reader.close();
BufferedWriter writer=新的BufferedWriter(新的FileWriter(文件名));
用于(字符串唯一:行){
writer.write(独特);
writer.newLine();
}
writer.close();
}catch(filenotfounde异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
}
它将filepath作为输入,找到重复的行并删除它们。但是如果您有大文件,请不要使用此方法。我在一个非常小的.txt文件上使用此方法(不导入日志文件和顺序)。使用此方法
public void stripDuplicatesFromFile(String filename) {
try {
BufferedReader reader = new BufferedReader(new FileReader(filename));
Set<String> lines = new HashSet<String>();
String line;
while ((line = reader.readLine()) != null) {
lines.add(line);
}
reader.close();
BufferedWriter writer = new BufferedWriter(new FileWriter(filename));
for (String unique : lines) {
writer.write(unique);
writer.newLine();
}
writer.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void stripDuplicatesFromFile(字符串文件名){
试一试{
BufferedReader reader=新的BufferedReader(新文件读取器(文件名));
Set line=new HashSet();
弦线;
而((line=reader.readLine())!=null){
行。添加(行);
}
reader.close();
BufferedWriter writer=新的BufferedWriter(新的FileWriter(文件名));
用于(字符串唯一:行){
writer.write(独特);
writer.newLine();
}
writer.close();
}catch(filenotfounde异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
}
它以filepath作为输入,找到重复的行并删除它们。但是如果您有大文件,请不要使用此方法。我在一个非常小的.txt文件上使用此方法(不导入日志文件类型和顺序).使用树集而不是ArrayList,您将自动排序且无重复。使用树集而不是ArrayList,您将自动排序且无重复。首先,您为什么要使用ArrayList
存储单词列表
ArrayList<DictPage> listOfWords = new ArrayList<DictPage>();
或
Set listOfWords=new Treeset();//没有重复项,也没有排序
这将确保您的单词列表不包含任何重复项
Set<DictPage> listOfWords = new Hashset<DictPage>(); //no duplicates but not sorted
如果您想直接对它们进行排序,可以使用TreeSet
,这将使您更容易进行排序。首先,为什么要使用ArrayList
来存储单词列表
ArrayList<DictPage> listOfWords = new ArrayList<DictPage>();
或
Set listOfWords=new Treeset();//没有重复项,也没有排序
这将确保您的单词列表不包含任何重复项
Set<DictPage> listOfWords = new Hashset<DictPage>(); //no duplicates but not sorted
如果你想让它们直接排序,你可以使用TreeSet
,这会让你更容易。可能的重复项可以先对列表排序,然后构建集合(你可以使用LinkedHashSet
来保持顺序)或者使用已经建议过的TreeSet
。可能的重复要么首先对列表排序,然后构建集合(您可以使用LinkedHashSet
来保持顺序),要么使用已经建议过的TreeSet
。这里有几个问题:1)OP似乎不想写回文件2)顺序不会被保留,代码也不会排序3)它没有使用DictPage
,代码也不会对单个单词进行操作4)(更多的是个人观点)仅仅发布代码而不解释什么是不同的,为什么没有帮助那么多