Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I make casting to BasicDBList?

I have an ArrayList (json.get("pecas")) in which needs to be transformed into BasicDBList, I tried to turn on BasicDBObject before. How do I do that?

BasicDBList pecas = ((BasicDBList) json.get("pecas"));

Error

java.lang.ClassCastException: java.util.ArrayList cannot be cast to com.mongodb.BasicDBList
like image 805
Daniela Morais Avatar asked Mar 31 '26 20:03

Daniela Morais


1 Answers

Use ArrayList's .addAll() see here for details

Here is an example of converting an ArrayList to a BasicDBList. I just used String since I don't know what type your ArrayList is. Make changes accordingly.

    BasicDBList dblist = new BasicDBList();
    // create arraylist to use
    ArrayList<String> alist = new ArrayList<String>();
    alist.add("blip");
    alist.add("bloop");
    alist.add("blap");

    dblist.addAll(alist); 

    System.out.println(dblist);
}
like image 106
miss.serena Avatar answered Apr 02 '26 12:04

miss.serena



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!