47

I have 2 Lists:

List<String> subjectArr = Arrays.asList<String>("aa", "bb", "cc");
List<Long> numArr = Arrays.asList<Long>(2L, 6L, 4L);

How do I create new List and zip two Lists into it?

List<?> subjectNumArr = zip(subjectArr, numArr);
// subjectNumArr == [{'aa',2},{'bb',6},{'cc',4}]
tkruse
  • 10,222
  • 7
  • 53
  • 80
ahad bahmanian
  • 487
  • 1
  • 4
  • 3

10 Answers10

47

Here's Java-8 solution using the Pair class (like in @ZhekaKozlov answer):

public static <A, B> List<Pair<A, B>> zipJava8(List<A> as, List<B> bs) {
    return IntStream.range(0, Math.min(as.size(), bs.size()))
            .mapToObj(i -> new Pair<>(as.get(i), bs.get(i)))
            .collect(Collectors.toList());
}

In Java 9 onwards you can use Map.entry():

    public static <A, B> List<Map.Entry<A, B>> zipJava9(List<A> as, List<B> bs) {
        return IntStream.range(0, Math.min(as.size(), bs.size()))
                .mapToObj(i -> Map.entry(as.get(i), bs.get(i)))
                .collect(Collectors.toList());
    }

 

candied_orange
  • 7,036
  • 2
  • 28
  • 62
Tagir Valeev
  • 97,161
  • 19
  • 222
  • 334
13

As per related question, you can use Guava (>= 21.0) to do this:

List<String> subjectArr = Arrays.asList("aa", "bb", "cc");
List<Long> numArr = Arrays.asList(2L, 6L, 4L);
List<Pair> pairs = Streams.zip(subjectArr.stream(), numArr.stream(), Pair::new)
        .collect(Collectors.toList());

Note that the guava method is annotated as @Beta, though what that means in practice is up to interpretation, the method has not changed since version 21.0.

tkruse
  • 10,222
  • 7
  • 53
  • 80
12

To get an Iterator<C> from an Iterator<A>, an Iterator<B>, and a BiFunction<A, B, C>:

public static <A, B, C> Iterator<C> map(Iterator<A> a, Iterator<B> b, BiFunction<A, B, C> f) {
    return new Iterator<C>() {
        public boolean hasNext() {
            return a.hasNext() && b.hasNext(); // This uses the shorter of the two `Iterator`s.
        }

        public C next() {
            return f.apply(a.next(), b.next());
        }
    };
}
Solomon Ucko
  • 5,724
  • 3
  • 24
  • 45
8

Use an ArrayList of Map.Entry<String, Long>, checking that both arraylists have equal size (as it seems to be your requirement), like that:

List<Map.Entry<String,Long>> subjectNumArr = new ArrayList<>(numArr.size());
if (subjectArr.size() == numArr.size()) {
    for (int i = 0; i < subjectArr.size(); ++i) {
        subjectNumArr.add(new AbstractMap.SimpleEntry<String, Long>(subjectArr.get(i), numArr.get(i));
    }   
}

That's all the code you need!

Then, to iterate over the results, use something like:

for (Map.Entry<String, Long> entry : subjectNumArr) {
    String key = entry.getKey(); 
    Long value = entry.getValue();
}

or, you can simply get the pair at position i (keeping insertion order), by:

Map.Entry<String, Long> entry = subjectNumArr.get(i);

This can also hold duplicate entries, unlike the Map solution that I initially suggested, without requiring to define your own (Pair) class.

vefthym
  • 7,422
  • 6
  • 32
  • 58
7

The operation you want is called zipping.

You need to implement a method zip:

public static <A, B> List<Pair<A, B>> zip(List<A> as, List<B> bs) {
  Iterator<A> it1 = as.iterator();
  Iterator<B> it2 = bs.iterator();
  List<Map.Entry<A, B>> result = new ArrayList<>();
  while (it1.hasNext() && it2.hasNext()) {
    result.add(Map.entry(it1.next(), it2.next()));
  }
  return result;
}

And you use it like this:

zip(subjectArr, numArr);
ZhekaKozlov
  • 36,558
  • 20
  • 126
  • 155
2

I agree with vefthym however if you have to do using list then create a class like below -:

class DirtyCoding{
   String subject;
   int numbr;
}

Then iterate over the your list, create object of DirtyCoding, populate it and add then add it to List<DirtyCoding>.

Shekhar Khairnar
  • 2,643
  • 3
  • 26
  • 44
Ankit
  • 55
  • 7
2

My ideas:

  • Define a class for your pairs. This makes your code extendable (i.e. if you want to add a third field).
  • Define your Lists with the convinient method Arrays.asList. It is easy to understand, short and automatically generates generic collections.
  • Use superclasses or interfaces as variable types. I used List in the example, maybe Collection would be even better. Only declare variables as ArrayList if you need the list to be so specific. That will give you the possibility to use other implementations, without having to change much code.

I would create Pair objects like this:

import java.util.*;

class Pair {
    String subject;
    Long num;
}

public class Snippet {

    public static void main(String[] args) {
        List<String> subjectArr = Arrays.asList("aa", "bb", "cc");
        List<Long> numArr = Arrays.asList(2l,6l,4l);

        // create result list
        List<Pair> pairs = new ArrayList<>();

        // determine result size
        int length = Math.min(subjectArr.size(), numArr.size());

        // create pairs
        for (int position = 0; position < length; position++) {
            Pair pair = new Pair();
            pair.subject = subjectArr.get(position);
            pair.num = numArr.get(position);
            pairs.add(pair);
        }
    }
}
slartidan
  • 20,403
  • 15
  • 83
  • 131
2

Use one of the answers from Zipping streams using JDK8 with lambda (java.util.stream.Streams.zip) to zip and apply a function at the same time

e.g. Using a zipped Stream:

<A,B,C>  Stream<C> zipped(List<A> lista, List<B> listb, BiFunction<A,B,C> zipper){
     int shortestLength = Math.min(lista.size(),listb.size());
     return IntStream.range(0,shortestLength).mapToObject( i -> {
          return zipper.apply(lista.get(i), listb.get(i));
     });        
}

for which you may also use Guava's Streams.zip()

tkruse
  • 10,222
  • 7
  • 53
  • 80
1

You should create an ArrayList of List:

ArrayList<List> subjectNumArr = new ArrayList<>();
Iterator iter = subjectArr.iterator();
int count=0;
while(iter.hasNext()){
    subjectNumArr.add(Arrays.asList(iter.next(),numArr.get[count++]);
}
Pang
  • 9,564
  • 146
  • 81
  • 122
Kumar Abhinav
  • 6,565
  • 2
  • 24
  • 35
-2

In Java 8: You can do this in one line using Stream and Collectors class.

In Java 7/6/5:

List list = new ArrayList();

        if(subjectArr.size() == numArr.size())
        {
            for (int i = 0; i < subjectArr.size(); i++) { // Loop through every subject/name
                list.add(subjectArr.get(i) + " " + numArr.get(i)); // Concat the two, and add it
            }
        }
kandarp
  • 4,979
  • 11
  • 34
  • 43