JAVA : Set addAll() vs adding using loop

后端 未结 3 701
轮回少年
轮回少年 2021-01-13 04:24

I was trying to add Integers from 2 sets into single Set via for loop and also using addAll() method provided by Collections. For test purpose, I have populated 2 Sets with

3条回答
  •  无人共我
    2021-01-13 04:57

    Concerning your question about how it is handled internally, just check the available source code, e.g. Ctrl-click in eclipse and find the implementation, which in your case is in AbstractCollection:

        public boolean addAll(Collection c) {
          boolean modified = false;
          for (E e : c)
            if (add(e))
                modified = true;
          return modified;
        }
    

    (from JDK 1.7)

提交回复
热议问题