Java HashSet 无法按预期与 Arrays.asList 一起工作

Java HashSet does not work as expected with Arrays.asList

我正在尝试用一堆数据初始化一个集合。但是当我检查一些已经用 contains 函数插​​入的项目时,它 returns false.

这段代码有什么问题?

public class Class {
    public static Set< String > set = new HashSet< >();

    static {
        set.addAll(Arrays.asList("First Holidays", "Big Milestones", "Pregnancy Milestones", "Create Your Own", "Milestones in Color", "Silver Balloons", "Pink Balloons", "Unicorn Balloons", "Ribbon Badges", "Circle Badges", "Animal numbers", "Cute animals", "Cute months ", "Floral numbers", "Starry numbers", "Activities", "Sleep", "Emotions", "Emoticons", "Endearments", "Love in Color", "Everyday in Color", "Badges & Banners", "Pop Art", "Fireworks", "Dividers", "Flags", "Hearts", "Love In Color", "Props", "Masks", "Stats", "This & That", "Gender Reveals", "Birth Announcements", "Measurements", "Announcements in Color", "Funny in Color", "Daddy", "Family", "Friends", "Friends & Family in Color", "Christmas", "Christmas Doodles", "Christmas in Color", "New Year", "New Year in Color", "Easter", "Easter in Color", "Halloween", "Halloween in Color", "Thanksgiving", "Thanksgiving in Color", "Mothers Day", "Mothers Day In Color", "Fathers Day", "Valentine’s Day", "Valentine’s Day in Color", "Happy Hanukkah", "Happy Ramadan", "Holidays in Color", "Birthday in Color", "Unicorn", "Party", "Summer", "Fall", "Winter", "School Bursts", "Gender Reveals", "Baby shower", "Pregnancy Milestones", "Bump Fruit Stickers", "Bump Fruit Badges", "Weekly Stickers", "Weekly Ribbon Badges", "Weekly Calendar Stickers", "Ribbon Due Dates", "Cute Due Dates", "Pregnancy in Color", "Gender Reveals", "Baby shower", "Pregnancy Milestones", "Bump Fruit Stickers", "Bump Fruit Badges", "Weekly Stickers", "Weekly Ribbon Badges", "Weekly Calendar Stickers", "Ribbon Due Dates", "Cute Due Dates", "Pregnancy in Color", "Gender Reveals", "Birth Announcements", "Measurements", "Announcements in Color", "Pink Balloons", "Unicorn Balloons", "Ribbon Badges", "Circle Badges", "Cute animals", "Cute months", "Floral numbers", "Starry numbers", "First Holidays", "Big Milestones", "Pregnancy Milestones", "Create Your Own", "Milestones in Color", "Activities", "Sleep", "Emotions", "Emoticons", "Endearments", "Love in Color", "Everyday in Color", "Badges & Banners", "Pop Art", "Fireworks", "Dividers", "Flags", "Hearts", "Love In Color", "Props", "Masks", "Stats", "This & That", "Daddy", "Family", "Friends", "Friends & Family in Color", "Christmas", "Christmas", "Christmas in Color", "New Year", "New Year in Color", "Easter", "Easter in Color", "Halloween", "Halloween in Color", "Thanksgiving", "Thanksgiving in Color", "Mothers Day", "Mothers Day In Color", "Fathers Day", "Valentine’s Day", "Valentine’s Day in Color", "Happy Hanukkah", "Happy Ramadan", "Holidays in Color", "Summer", "Fall", "Winter", "Funny in Color", "Birthday in Color", "Unicorn", "Party", "School Bursts"));
    }

    public static void main(String[] args) {
        boolean check = set.contains("Animal Numbers");
        System.out.println(check); // false
    }
}

也这样试过,还是不行

public class Class {
    public static Set <String> set = new HashSet<>(Arrays.asList("First Holidays", "Big Milestones", "Pregnancy Milestones", "Create Your Own", "Milestones in Color", "Silver Balloons", "Pink Balloons", "Unicorn Balloons", "Ribbon Badges", "Circle Badges", "Animal numbers", "Cute animals", "Cute months ", "Floral numbers", "Starry numbers", "Activities", "Sleep", "Emotions", "Emoticons", "Endearments", "Love in Color", "Everyday in Color", "Badges & Banners", "Pop Art", "Fireworks", "Dividers", "Flags", "Hearts", "Love In Color", "Props", "Masks", "Stats", "This & That", "Gender Reveals", "Birth Announcements", "Measurements", "Announcements in Color", "Funny in Color", "Daddy", "Family", "Friends", "Friends & Family in Color", "Christmas", "Christmas Doodles", "Christmas in Color", "New Year", "New Year in Color", "Easter", "Easter in Color", "Halloween", "Halloween in Color", "Thanksgiving", "Thanksgiving in Color", "Mothers Day", "Mothers Day In Color", "Fathers Day", "Valentine’s Day", "Valentine’s Day in Color", "Happy Hanukkah", "Happy Ramadan", "Holidays in Color", "Birthday in Color", "Unicorn", "Party", "Summer", "Fall", "Winter", "School Bursts", "Gender Reveals", "Baby shower", "Pregnancy Milestones", "Bump Fruit Stickers", "Bump Fruit Badges", "Weekly Stickers", "Weekly Ribbon Badges", "Weekly Calendar Stickers", "Ribbon Due Dates", "Cute Due Dates", "Pregnancy in Color", "Gender Reveals", "Baby shower", "Pregnancy Milestones", "Bump Fruit Stickers", "Bump Fruit Badges", "Weekly Stickers", "Weekly Ribbon Badges", "Weekly Calendar Stickers", "Ribbon Due Dates", "Cute Due Dates", "Pregnancy in Color", "Gender Reveals", "Birth Announcements", "Measurements", "Announcements in Color", "Pink Balloons", "Unicorn Balloons", "Ribbon Badges", "Circle Badges", "Cute animals", "Cute months", "Floral numbers", "Starry numbers", "First Holidays", "Big Milestones", "Pregnancy Milestones", "Create Your Own", "Milestones in Color", "Activities", "Sleep", "Emotions", "Emoticons", "Endearments", "Love in Color", "Everyday in Color", "Badges & Banners", "Pop Art", "Fireworks", "Dividers", "Flags", "Hearts", "Love In Color", "Props", "Masks", "Stats", "This & That", "Daddy", "Family", "Friends", "Friends & Family in Color", "Christmas", "Christmas", "Christmas in Color", "New Year", "New Year in Color", "Easter", "Easter in Color", "Halloween", "Halloween in Color", "Thanksgiving", "Thanksgiving in Color", "Mothers Day", "Mothers Day In Color", "Fathers Day", "Valentine’s Day", "Valentine’s Day in Color", "Happy Hanukkah", "Happy Ramadan", "Holidays in Color", "Summer", "Fall", "Winter", "Funny in Color", "Birthday in Color", "Unicorn", "Party", "School Bursts"));

    public static void main(String[] args) {
        boolean check = set.contains("Animal Numbers");
        System.out.println(check); // false
    }
}

您使用名称“set”初始化但为“proSet”执行 addAll?我看不出有什么不妥。但是,为了更好的阅读选择,它可以是:

public static Set <String> set = new HashSet<>(Arrays.asList(... ,"Animal Numbers", ...));

使用更新后的代码,字符串会更改大小写。

"Circle Badges", "Animal numbers", "Cute animals"

boolean check = set.contains("Animal Numbers");

String.equals区分大小写。

区分大小写 - 集合查找是字符串对象的区分大小写的比较

Animal Numbers 不在集合中。 Animal numbers 在集合中。

import java.util.*;

class Solution {
    public final Set<String> set;

    public Solution() {
        set = new HashSet<>();
        set.addAll(Arrays.asList("Circle Badges", "Animal numbers", "Cute animals"));
    }

    public static void main(String[] args) {
        Solution solution = new Solution();
        System.out.println("Animal Numbers: " + solution.set.contains("Animal Numbers"));
        System.out.println("Animal numbers: " + solution.set.contains("Animal numbers"));
    }
}

总是小写(不确定不同语言的 Locale)

如果需要不区分大小写的比较,一种选择是以小写形式存储字符串并以小写形式检查字符串。

import java.util.*;
import java.util.stream.Collectors;

class Solution {
    public final Set<String> set;

    public Solution() {
        set = Arrays.asList("Animal numbers", "Cute animals", "Cute months ")
            .stream().map(String::toLowerCase).collect(Collectors.toSet());
    }

    public static void main(String[] args) {
        Solution solution = new Solution();
        System.out.println("Animal Numbers: " + solution.set.contains("Animal Numbers".toLowerCase()));
        System.out.println("Animal numbers: " + solution.set.contains("Animal numbers".toLowerCase()));
    }
}

备注

有问题的示例输入在 Cute months 之后包含 space( ),因此在输入和检查期间可能都需要 trim()