question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

sometimes I need to merge List<A> and List<B> to List<C>, the object of A and B have a combine property such as id. Is there any function like below?

List<T> merge(List<F1> list1, List<F2> list2, Function<T, F1, F2> func);

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:20 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
memimy2015commented, Dec 10, 2016

@loki-1 Correctly! Your method can solve my request. I want to know if there is a method I can do this by 1 line code.

1reaction
loki-1commented, Dec 10, 2016

If I understood the original request correctly, it’s not a ‘zip’ operation because elements should match based on their key, not on their positions. So if lists are not sorted, there should be additional operation to find the matching element by key, something like

Map<Long, Exam> examByStudentId = Maps.uniqueIndex(exams, e -> e.studentId);
List<ScoreRecord> records = Lists.transform(students, student -> {
    Exam exam = examByStudentId.get(student.studentId);
    return new ScoreRecord(student.name, exam.score);
});
Read more comments on GitHub >

github_iconTop Results From Across the Web

How do I concatenate two lists in Python? - Stack Overflow
heapq.merge will work, but its use case is for merging sorted lists in linear time. ... You can use sets to obtain merged...
Read more >
Python : Join / Merge lists ( two or more) - thisPointer
Join / Merge two lists in python using + operator. In python, we can use the + operator to merge the contents of...
Read more >
Python: Combine Lists – Merge Lists (8 Ways) - Datagy
Learn how to combine Python lists and how to merge lists, including in alternating sequence and with unique or common elements.
Read more >
std::list::merge - CPlusPlus.com
Merge sorted lists. Merges x into the list by transferring all of its elements at their respective ordered positions into the container (both...
Read more >
Merge Two List in Python - Scaler Topics
One simple and popular way to merge(join) two lists in Python is using the in-built append() method of python. The append() method in...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found