この質問は何百万回も聞かれていることを知っています。私は百万回の解決策を見てきましたが、私にとってはうまくいきませんでした. ファイルに書き込みたいハッシュセットがありますが、ハッシュセットの各要素を別々の行に入れたいです。これが私のコードです:
Collection<String> similar4 = new HashSet<String>(file268List);
Collection <String> different4 = new HashSet<String>();
different4.addAll(file268List);
different4.addAll(sqlFileList);
similar4.retainAll(sqlFileList);
different4.removeAll(similar4);
Iterator hashSetIterator = different.iterator();
while(hashSetIterator.hasNext()){
System.out.println(hashSetIterator.next());
}
ObjectOutputStream writer = new ObjectOutputStream(new FileOutputStream("HashSet.txt"));
while(hashSetIterator.hasNext()){
Object o = hashSetIterator.next();
writer.writeObject(o);
}