-3

I have taken data from a database and it is in List<> myList. There are 14 columns. I need to keep this list as it is for use later in the process I am doing.

Is it possible to copy certain columns into a new list or an object? if it is possible, how can i do it? unfortunately these are not columns 1, 2, 3, 4, 5.

The data i need is in columns 1, 7, 9.

As well as this I need an extra bit of data from a variable tagged on the end.

I want my newly created list to be ID(column1 of original data), title(column7), surname(column9), todays date(variable).

  • 3
    So what have you tried? – BackSlash Jan 10 '19 at 13:54
  • 1
    The only question I see is `Is it possible to copy certain columns into a new list or an object?`; The answer is `Yes` – xtratic Jan 10 '19 at 13:56
  • If you've edited to add your actual question then please also edit to include what you've tried. This isn't a "do my work for me" site. – xtratic Jan 10 '19 at 14:04
  • i apologise, i am new to this and just looking for a bit of help. –  Jan 10 '19 at 14:07
  • so far I have tried to clone from one list to another, similar to this https://stackoverflow.com/questions/715650/how-to-clone-arraylist-and-also-clone-its-contents . i dont want the whole list, so obviously cloning is incorrect. i have also tried using a Collectors.toList() and subList. i dont want a range, I am just not sure how to pick out certain columns. sorry for my lack of understanding. –  Jan 10 '19 at 14:15

1 Answers1

1

It is far better style to make your own data transfer objects, something like:

public class Person {
    long id;
    String title;
    String surname;
    LocalDateTime todays;
}

You'll need to create new objects, and copy the fields.

But will be repaid by less search for index errors, and nicer code.

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138