Performance and Memory allocation comparison between List and Set

后端 未结 6 1272
执笔经年
执笔经年 2020-12-02 14:00

I want to know the comparison between List and Set in terms of performance,memory allocation and usability.

If i don\'t have any requirement of keeping the uniquenes

6条回答
  •  忘掉有多难
    2020-12-02 14:59

    Use HashSet if you need to use .contains(T) frequently.

    Example:

    private static final HashSet KEYWORDS = Stream.of(new String[]{"if", "do", "for", "try", "while", "break", "return"}).collect(Collectors.toCollection(HashSet::new));
    
    public boolean isKeyword(String str) {
         return KEYWORDS.contains(str);
    }
    

提交回复
热议问题