Here is how to join two Lists into one Lis.
List.addAll()
All items in the list passed as an argument to addAll()
can be added to the list.
The following code adds both lists to one list.
List<String> list1 = new ArrayList<>();
list1.add("a1");
list1.add("a2");
List<String> list2 = new ArrayList<>();
list2.add("b1");
list2.add("b2");
List<String> joined = new ArrayList<>();
joined.addAll(list1);
joined.addAll(list2);
System.out.println(joined);
result
[a1, a2, b1, b2]
Guava
You can combine two lists into one using Guavas
Iterablesand
Lists`.
List<String> list1 = new ArrayList<>();
list1.add("a1");
list1.add("a2");
List<String> list2 = new ArrayList<>();
list2.add("b1");
list2.add("b2");
Iterable<String> joinedIterable = Iterables.unmodifiableIterable(
Iterables.concat(list1, list2));
Collection<String> joined = Lists.newArrayList(joinedIterable);
System.out.println(joined);
result
[a1, a2, b1, b2]
Apache
Apaches
ListUtils.union()` is an API that merges two lists into one and returns the combined list.
List<String> list1 = new ArrayList<>();
list1.add("a1");
list1.add("a2");
List<String> list2 = new ArrayList<>();
list2.add("b1");
list2.add("b2");
List<String> joined = ListUtils.union(list1, list2);
System.out.println(joined);
result
[a1, a2, b1, b2]
Stream
You can also create a list using Stream.
List<String> list1 = new ArrayList<>();
list1.add("a1");
list1.add("a2");
List<String> list2 = new ArrayList<>();
list2.add("b1");
list2.add("b2");
List<String> joined = Stream.concat(list1.stream(), list2.stream())
.collect(Collectors.toList());
System.out.println(joined);
result
[a1, a2, b1, b2]
You can select and add only the items you want using Stream`s filter().
The code below adds only strings containing 1 to the joined
list.
List<String> list1 = new ArrayList<>();
list1.add("a1");
list1.add("a2");
List<String> list2 = new ArrayList<>();
list2.add("b1");
list2.add("b2");
List<String> joined = Stream.concat(list1.stream(), list2.stream())
.filter(s->s.contains("1"))
.collect(Collectors.toList());
System.out.println(joined);
result
[a1, b1]
Reference
Related Posts
- Java - Remove items from List while iterating
- Java - How to find key by value in HashMap
- Java - Update the value of a key in HashMap
- Java - How to put quotes in a string
- Java - How to put a comma (,) after every 3 digits
- BiConsumer example in Java 8
- Java 8 - Consumer example
- Java 8 - BinaryOperator example
- Java 8 - BiPredicate Example
- Java 8 - Predicate example
- Java 8 - Convert Stream to List
- Java 8 - BiFunction example
- Java 8 - Function example
- Java - Convert List to Map
- Exception testing in JUnit
- Hamcrest Collections Matcher
- Hamcrest equalTo () Matcher
- AAA pattern of unit test (Arrange/Act/Assert)
- Hamcrest Text Matcher
- Hamcrest Custom Matcher
- Why Junit uses Hamcrest
- Java - ForkJoinPool
- Java - How to use Futures
- Java - Simple HashTable implementation
- Java - Create a file in a specific path
- Java - Mockito의 @Mock, @Spy, @Captor, @InjectMocks
- Java - How to write test code using Mockito
- Java - Synchronized block
- Java - How to decompile a ".class" file into a Java file (jd-cli decompiler)
- Java - How to generate a random number
- Java - Calculate powers, Math.pow()
- Java - Calculate the square root, Math.sqrt()
- Java - How to compare String (==, equals, compare)
- Java - Calculate String Length
- Java - case conversion & comparison insensitive (toUpperCase, toLowerCase, equalsIgnoreCase)