I was just wondering, what is the best way to add several items to a HashSet at once?
I'm working on a homework assignment where the object is to iterate through a .java file and count the keywords in the file. At the bottom of the assignment description it says ("Hint: Create a Set to hold all Java keywords")
I'm not completely familiar with HashSets, and I didn't know how to add a bulk of words at once, and I certainly didn't want to go through .add("final") .add("true") ..and so on for each keyword.
So, I created an array list with all of those words. I then used a for loop to loop through and add each one to the set.However, this seems redundant. If I've got all the keywords in an array, then I don't see why I would need to add them to a HashSet in order to complete the assignment. But, for sake of learning some more on HashSets, is there a way to do this without the method I used(other than 1 by 1)?
String[] aryKeywords = { "abstract", "asset", "boolean", "break", "byte", "case", "catch", "char", "class", "const", "continue", "default", "do", "double", "else", "enum", "extends", "final", "finally", "float", "for", "goto", "if", "implements", "import", "instanceof", "int", "interface", "long", "开发者_高级运维native", "new", "package", "private", "protected", "public", "return", "short", "static", "strictftp", "super", "switch", "synchronized", "this", "throw", "throws", "transient", "try", "void", "volatile", "while", "false", "null", "true" };
Set<String> jKeywords = new HashSet<String>();
for (int i = 0; i < aryKeywords.length; i++) {
jKeywords.add(aryKeywords[i]);
}
Thanks for any insight!
Jon Skeet's answer is correct. Another (supposedly faster, according to its documentation) approach is:
Set<String> jKeywords = new HashSet<String>();
Collections.addAll(jKeywords, aryKeywords);
Or to specify them inline (to mirror Jon's answer):
Collections.addAll(jKeywords, "abstract", "asset", "boolean", /* ... */);
You could use:
Set<String> jKeywords = new HashSet<String>(Arrays.asList(aryKeywords));
Or if you want to specify them inline:
Set<String> jKeywords = new HashSet<String>(Arrays.asList(
"abstract", "asset", "boolean", /* etc */));
If you're ever in a situation where you've already got a set, you can use addAll
:
jKeywords.addAll(Arrays.asList(extraMembers));
you could do
jKeywords.addAll(Arrays.<String>asList(aryKeywords));
but that adds more garbage for somewhat cleaner code
There's some VARIANTS of the same thing (the double {{ trick, which I don't like, but if you really want I'll menion), but no, Java doesn't really have a perfect answer here. For you. Another variation is the String[] you did, followed by Arrays.asList it into a List and then set.addAll . From an efficiency POV, none are particularly great.
You could use Arrays.asList()
:
Set<String> jKeywords = new HashSet<String>(Arrays.asList(aryKeywords));
Set<String> jKeywords = new HashSet<String>(Arrays.asList(aryKeywords));
I don't think you can bypass representing you items as an array. But, you can add them all at once by using a construct like
new HashSet (Arrays.asList(aryKeywords));
精彩评论