java-哈希集包含重复项?

时间:2018-10-10 02:50:59

标签: java

我正在尝试从PDF中提取所有文本并将其存储在HashSet中。据我所知,HashSet不包含重复项,因此在提取重复项时它将忽略重复项。但是,当我打印出哈希结果时,我注意到其中有重复的空白。

我想将哈希值插入MySQL的表中,但是它具有主键约束,因此给我带来了麻烦。 有什么方法可以删除哈希中的所有重复项吗?

我提取文本的代码:

public static void main(String[] args) throws Exception {
      String path ="D:/PDF/searchable.pdf";
        HashSet<String> uniqueWords = new HashSet<>();
        try (PDDocument document = PDDocument.load(new File(path))) {

            if (!document.isEncrypted()) {

                PDFTextStripper tStripper = new PDFTextStripper();
                String pdfFileInText = tStripper.getText(document);
                String lines[] = pdfFileInText.split("\\r?\\n");
                for (String line : lines) {
                    String[] words = line.split(" ");

                    for (String word : words) {
                        uniqueWords.add(word);

                    }

                }
              System.out.println(uniqueWords);

            }
        } catch (IOException e){
            System.err.println("Exception while trying to read pdf document - " + e);
        }
        Object[] words =  uniqueWords.toArray();
        System.out.println(words[1].toString());

        MysqlAccess connection=new MysqlAccess();

        for(int i = 1 ; i <= words.length - 1 ; i++ ) {

            connection.readDataBase(path, words[i].toString());

        }

        System.out.println("Completed");

    }

}

这是我的哈希值:

[, highlight, of, Even, copy, file,, or, ., ,, 1, reader,, different, D, F, ll, link, ea, This, ed, document, V, P, ability, regardless, g, d, text., e, b, a, n, o, web, l, footnote., should, Most, IDRH, selection, text-searchable, positioning, u, s, what, r, PDF., happens, er, y, x, to, body, single, ca, te, together, ti, th, would, when, be, Text-Searchable, document,, text, isn't, such, kinds, sh, co, ld, font,, example, ch, this, attempt, have, t,, Notice,, contained, from, re, text.1, page,, style, page., able, if, is, You, standard, PDF, your, as, readers, you, the, in, main, an, iz]

如果它们是唯一的,为什么当我尝试插入主键列时为什么抛出" Duplicate entry for key PRIMARY"

任何建议将不胜感激。

1 个答案:

答案 0 :(得分:2)

HashSet不允许输入任何重复项。

这是HashSet类的add(E e)方法描述:

public boolean add(E e)

将指定的元素添加到该集中(如果尚不存在)。更正式地说,如果此元素集中不包含任何元素e2,则将指定元素e添加到该元素集合中,从而使(e == null?e2 == null:e.equals(e2))。如果此集合已经包含该元素,则调用将使该集合保持不变并返回false。

在您的情况下,当您在pdfFileInText上调用split方法时,将得到具有单个空格的字符串和具有多个空格的字符串的字符串数组,从而导致HashSet数据结构同时具有单个空格和多个空格。但是,当在数据库中插入某个字符串时,该字符串会被修剪,从而导致重复的条目。

要对此进行详细说明,请查看以下代码段:

public class TestHashSetUniqueness {
public static void main(String[] args) {
    HashSet<String> hashSet = new HashSet<String>();
    String oneSpace = " ";
    String twoSpaces = "  ";

    hashSet.add(oneSpace);
    hashSet.add(twoSpaces);

    // Here HashSet size is 2 as it is treating string objects oneSpace
    // and twoSpaces as two different strings.
    System.out.println("HashSet size without trim() : "+hashSet.size());

    hashSet.clear();
    hashSet.add(oneSpace.trim());
    hashSet.add(twoSpaces.trim());

    // As we are trimming(removing the excess spaces) spaces in the strings
    // causing our HashSet to have only one element there by avoiding duplicates
    System.out.println("HashSet size with trim() : "+hashSet.size());
}

}

因此,在将字符串添加到HashSet时,请在字符串上调用trim()来解决问题。

我希望这能回答您的问题。