How to use addall () method for collections?

I need to use it to combine two ordered lists of objects.

+6
java
source share
3 answers

From the API:

addAll(Collection<? extends E> c) : adds all the elements in the specified collection to this collection (optional operation).

Here's an example using List , which is an ordered collection:

  List<Integer> nums1 = Arrays.asList(1,2,-1); List<Integer> nums2 = Arrays.asList(4,5,6); List<Integer> allNums = new ArrayList<Integer>(); allNums.addAll(nums1); allNums.addAll(nums2); System.out.println(allNums); // prints "[1, 2, -1, 4, 5, 6]" 

On int[] vs Integer[]

While int is autoboxable before Integer , a int[] NOT "autoboxing" to Integer[] .

Thus, you get the following behavior:

  List<Integer> nums = Arrays.asList(1,2,3); int[] arr = { 1, 2, 3 }; List<int[]> arrs = Arrays.asList(arr); 

Related Questions

  • Arrays.asList () not working as it should?
+13
source share
 Collection all = new HashList(); all.addAll(list1); all.addAll(list2); 
+2
source share

I am coding some android, and I found this to be very short and convenient:

  card1 = (ImageView)findViewById(R.id.card1); card2 = (ImageView)findViewById(R.id.card2); card3 = (ImageView)findViewById(R.id.card3); card4 = (ImageView)findViewById(R.id.card4); card5 = (ImageView)findViewById(R.id.card5); card_list = new ArrayList<>(); card_list.addAll(Arrays.asList(card1,card2,card3,card4,card5)); 

Compared to this standard way, I used:

  card1 = (ImageView)findViewById(R.id.card1); card2 = (ImageView)findViewById(R.id.card2); card3 = (ImageView)findViewById(R.id.card3); card4 = (ImageView)findViewById(R.id.card4); card5 = (ImageView)findViewById(R.id.card5); card_list = new ArrayList<>(); card_list.add(card1) ; card_list.add(card2) ; card_list.add(card3) ; card_list.add(card4) ; card_list.add(card5) ; 
+1
source share

All Articles