-1

I have a task to merge few lists in one. I need to get (i) element from each list and place it to (i) cell of 'books' list. It's hard for me because lists have different size. How can i do that? Here is some my code, but it doesnt work because of different sizes of lists:

 private LinkedList<String> books = new LinkedList<>();

    private LinkedList titles = new LinkedList();
    private LinkedList authors = new LinkedList<>();
    private LinkedList prices = new LinkedList<>();
    private LinkedList bestSellers = new LinkedList<>();

    public LinkedList createBooks() {

        for (int i = 0; i < 16; i++) {
            String temp, temp1, temp2, temp3;
             temp = (String) titles.get(i);
             temp1 = (String) authors.get(i);
             temp2 = (String) prices.get(i);
             temp3 = (String) bestSellers.get(i);

            books.addAll(i, Arrays.asList(temp, temp1, temp2, temp3));

        }

        return books;
    }
maitre
  • 25
  • 4
  • So you want to get `i`th element of each list, concat them and add it to `i`th element of `books` list? Is my understanding correct? – Shyam Baitmangalkar May 05 '21 at 12:41
  • 1
    Perhaps if you tell us which part is failing or which part you dont understand? – Gavin May 05 '21 at 12:42
  • I need to get all information about book(title, author, price, bestseller) to be placed in one cell of "books" linkedlist and if something of these 4 components is empty, it can be placed like null or just "". – maitre May 05 '21 at 12:48
  • Hint: it seems (your) class LinkedList takes a type parameter. That turns `private LinkedList titles = new LinkedList();` into a RAW type. Also note that 3 of your new calls use `new LinkedList<>()` , but one does not. (so: 3 lines do it right, one is wrong). Such details matter in programming. – GhostCat May 05 '21 at 13:42
  • So see https://stackoverflow.com/questions/2770321/what-is-a-raw-type-and-why-shouldnt-we-use-it – GhostCat May 05 '21 at 13:42

2 Answers2

1

Firstly, it doesn't make sense if the given lists like titles, authors etc are of different sizes. There is no way of knowing which author has written which title or what is the price of a specific book. Data is randomly scattered across multiple lists. But even if you want to create a list out of this scattered data, then here is one way of doing it:

Create a class called Book that holds the required book related information:

public class Book {
    private String title;
    private String author;
    private String price;

    public Book(String title, String author, String price) {
        this.title = title;
        this.author = author;
        this.price = price;
    }

    // add getters, toString, hashCode, equals
    
 }

Then find the size of largest list among the given lists: If using Java 8+:

List<List> temp = Arrays.asList(titles, authors, prices);
int maxSize = temp.stream().mapToInt(List::size).max().getAsInt();

If running Pre-Java 8:

List<List> temp = Arrays.asList(titles, authors, prices);
int maxSize = 0;
    for (List list : temp) {
      if (list.size() > maxSize) {
        maxSize = list.size();
      }
    }

Now, iterate till maxSize and build the List<Book>:

List<Book> books = new ArrayList<>();
    for (int i = 0; i < maxSize; i++) {
        String title = null;
        String author = null;
        String price = null;
        if(i < titles.size()) {
            title = titles.get(i);
        }

        if(i < authors.size()) {
            author = authors.get(i);
        }

        if(i < prices.size()) {
            price = prices.get(i);
        }
      books.add(new Book(title, author, price));
    }

The resulting collection will have Book related details.

Shyam Baitmangalkar
  • 1,075
  • 12
  • 18
0

The easiest way to accomplish this is to use Collection.addAll() docs:

LinkedList<String> books = new LinkedList<>();
books.addAll(this.titles);
books.addAll(this.authors);
...
Pieter12345
  • 1,713
  • 1
  • 11
  • 18
  • thank you, it works, but not in a way it's expected to work. I need to get all information about book(title, author, price, bestseller) to be placed in one cell of "books" linkedlist and if something is empty, it can be placed like null or just "". – maitre May 05 '21 at 12:45
  • Sounds like you would be better off creating a `Book` class containing an `author`, `title`, `price` etc field to store this data. If your arrays are not of equal size, then it sounds to me like you are missing data (e.g. what would a title be when it's not present in the list?) – Pieter12345 May 05 '21 at 12:49
  • Any missing parts of book can be null or just "". Title is always present. How can I implement it with Book class? – maitre May 05 '21 at 12:52
  • If the arrays are not of equal size, then you won't know which authors etc are missing. The idea is to implement the Book class as I said and pass in the title, author, etc into the constructor. You'll end with a `List` which is a more convenient way to represent your data, and which you can then continue to work on. – Pieter12345 May 05 '21 at 12:58