Skip to content

Yet another programming solutions log

Sample bits from programming for the future generations.

Technologies Technologies
  • Algorithms and Data Structures
  • Java Tutorials
  • JUnit Tutorial
  • MongoDB Tutorial
  • Quartz Scheduler Tutorial
  • Spock Framework Tutorial
  • Spring Framework
  • Bash Tutorial
  • Clojure Tutorial
  • Design Patterns
  • Developer’s Tools
  • Productivity
  • About
Expand Search Form

Java add array to list

farenda 2015-06-27 0

Problem:

In Java there is a couple of ways to add an array of items to a collection. In the following examples we’ll cover a few of them.

Solution:

When programming in Java, very often there’s a need to add some elements to a collection. It can be done in different ways as you can see in the following example:

package com.farenda.java;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class CollectionsAddAll {

    public static void main(String[] args) {
        List<String> items = new ArrayList<>();

        // Too verbose:
        items.add("added");
        items.add("manually");
        System.out.println(items);

        // Sometimes better:
        items.addAll(Arrays.asList("as", "an", "array"));
        System.out.println(items);

        // The best way: reuse!
        Collections.addAll(items, "reusing", "Collections", "library");
        System.out.println(items);
    }
}

Many people don’t even know that there’s a method java.util.Collections.addAll(Collection, T… items) to add an array (so varargs too) of elements to a Collection. In most cases the method is better than Arrays.asList version.

Remember Item 47: Know and use the libraries from excellent “Effective Java, 2nd Edition” book! It will return many times! :-)

Share with the World!
Categories Java Tags java-basics, java-collections
Previous: Bash until loop
Next: Bash if else

Recent Posts

  • Java 8 Date Time concepts
  • Maven dependency to local JAR
  • Caesar cipher in Java
  • Java casting trick
  • Java 8 flatMap practical example
  • Linked List – remove element
  • Linked List – insert element at position
  • Linked List add element at the end
  • Create Java Streams
  • Floyd Cycle detection in Java

Pages

  • About Farenda
  • Algorithms and Data Structures
  • Bash Tutorial
  • Bean Validation Tutorial
  • Clojure Tutorial
  • Design Patterns
  • Java 8 Streams and Lambda Expressions Tutorial
  • Java Basics Tutorial
  • Java Collections Tutorial
  • Java Concurrency Tutorial
  • Java IO Tutorial
  • Java Tutorials
  • Java Util Tutorial
  • Java XML Tutorial
  • JUnit Tutorial
  • MongoDB Tutorial
  • Quartz Scheduler Tutorial
  • Software Developer’s Tools
  • Spock Framework Tutorial
  • Spring Framework

Tags

algorithms bash bean-validation books clojure design-patterns embedmongo exercises git gof gradle groovy hateoas hsqldb i18n java java-basics java-collections java-concurrency java-io java-lang java-time java-util java-xml java8 java8-files junit linux lists log4j logging maven mongodb performance quartz refactoring regex rest slf4j solid spring spring-boot spring-core sql unit-tests

Yet another programming solutions log © 2021

sponsored