0

What is the best way of building an ArrayList<> from a List<> ?

An Example would be great

and another question, should i keep working with a List<> instead of converting all my Lists to ArrayLists

Edit

Just to be sure of my code : can this work :

public List<Rendezvous> getAllNewlyDoneRdvs() {
        if (!isThereNewlyDoneRdvs())return null;
        List<Rendezvous> rdvs = rdvDao.findAllByState(Constants.rdvStateUndone);
        ArrayList<Rendezvous> rendezvousList = new ArrayList<>();
        for (Rendezvous rdv :rdvs ){
            if (rdv.getDateRdv().before(Constants.getCurrentDatetime())) rendezvousList.add(rdv) ;
        }
        rdvs = rendezvousList;
        return rdvs;
    }
Aissasa
  • 231
  • 5
  • 18
  • Check out this question: http://stackoverflow.com/questions/2279030/type-list-vs-type-arraylist-in-java – nem035 Sep 02 '14 at 21:21
  • 1
    It depends on what you do with your `List`. If you just access it by e.g. iterating over it, the basic class `List` should be sufficient. – sebastian_oe Sep 02 '14 at 21:21
  • Related: [what-does-it-mean-to-program-to-an-interface](http://stackoverflow.com/questions/383947/what-does-it-mean-to-program-to-an-interface) – Pshemo Sep 02 '14 at 21:26
  • Note that an ArrayList is a List. – Hot Licks Sep 02 '14 at 21:30
  • 1
    And since a List is a Collection, you can use `new ArrayList<>(someList)` to create a copy of a List as an ArrayList. – Hot Licks Sep 02 '14 at 21:32

7 Answers7

4

assumming both of the type are same

List<> could be already an ArrayList<> in that case just change reference if not iterate and add

jmj
  • 237,923
  • 42
  • 401
  • 438
  • Ah so then i can build an `ArrayList<>` , fill it and then in the end make a `List<>` refernce and affect my arraylist to it and return it ?(since my methods return `List<>`) – Aissasa Sep 02 '14 at 21:24
  • 3
    yes `List` is an interface, it could be `ArrayList` – jmj Sep 02 '14 at 21:24
  • I prefer Collection<> collection = new ArrayList<>(); Because the collections gives you a broader option of set and trees. – StackFlowed Sep 02 '14 at 21:41
  • @JigarJoshi is my code (in the edit) is correct, just to make sure – Aissasa Sep 02 '14 at 22:01
3

If you want to be sure that you have an ArrayList to work with, you can just do new ArrayList(existingList).

The best reason for doing this might be that you want to add, update, or remove items from the list without affecting the original list.

Jon Onstott
  • 13,499
  • 16
  • 80
  • 133
1

It will be good practice using List while declaring.

List<> list = new ArrayList<>();
Niamath
  • 309
  • 4
  • 12
1

You should know that List is an Interfaces and ArrayList is a concrete class, so you should always use

List<Type> myList = new ArrayList<Type>();
kakashy
  • 714
  • 9
  • 24
0

You could use Collections.list() as illustrated here

paulk23
  • 465
  • 3
  • 6
0

You can get caught out if you have a method taking a List parameter because some implementations of List (even in the standard library) are immutable or are of a fixed size. In which case, if you need a mutable list, it's completely reasonable to create an ArrayList with the same contents as your existing uncertain-implementation List

ArrayList<T> newArrayList = new ArrayList<T>(oldList);
khelwood
  • 55,782
  • 14
  • 81
  • 108
0

It sheems you are comparing the dates first and then preparing the new list.

This is absolutely fine. But in this case you should have another method in DAO itself which returns the List of Rendezvous having DateRdv before current date.

Hansraj
  • 174
  • 5
  • well, that's not the approach i opted for, i search List with their state(done or not done yet, i need that for the purpose of my app ;) ) – Aissasa Sep 02 '14 at 22:58