I have a couple of questions about how the add function of a Java Collection handles strings. For example, in the below code snippet, I am copying a List of strings to a HashSet. What is the worst-case total time complexity in this case? Is it O(M x N) or O(N), where M is the max length of any string in the list, and N is the total number of strings in the list.
public HashSet<String> createDict(List<String> wordList) {
HashSet<String> wordDict = new HashSet<>();
for(String word : wordList) {
wordDict.add(word);
}
return wordDict;
}
Will the time complexity be exactly the same if I use the below code instead of the loop?
HashSet<String> wordDict = new HashSet<>(wordList);