0

I have converted this JSON to Hashmap

http://www.mocky.io/v2/5d0dc72d3400008c00ca4a62

I have nested Hashmap and I wanted to convert it to ArrayList

Map<String, Bank> stringBankMap = getValues();

I want to get all the data from stringBankMap and add to the list. I want also the key to the hashmap to also will be imported on the list as a guide.

Here is Bank class

public class Bank {
    private String guid;
    private String title;
    private long date;
    private String logo;
    private HashMap<String, HashMap<String, BankList>> list;
}

Here is BankList class

public class BankList {
    private double buy;
    private double sell;
    private String currency;
}

What I tried

 for(Map.Entry<String, Bank> entry1 : stringBankMap.entrySet()) {
                Bank newBank = new Bank(entry1.getKey());
                Bank bank = entry1.getValue();
                newBank.setDate(bank.getDate());
                newBank.setLogo(bank.getLogo());
                newBank.setTitle(bank.getTitle());
                List<BankList> bankListList = new ArrayList<>();
                for(Map.Entry<String, HashMap<String, BankList>> entry2 : bank.getList().entrySet()) {
                    HashMap<String, BankList> map = entry2.getValue();
                    for (Map.Entry<String, BankList> entry3 : map.entrySet()) {
                        BankList newBankList = new BankList(entry3.getKey());
                        BankList bankList = entry3.getValue();
                        newBankList.setBuy(bankList.getBuy());
                        newBankList.setSell(bankList.getSell());
                        bankListList.add(newBankList);
                    }
                }
                bankArrayList.add(newBank);
            }

But I don't understand why I am getting an exception

Please suggest me other algorithms if you can

 java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to core.model.Bank
fish40
  • 5,738
  • 17
  • 50
  • 69

1 Answers1

0

you can do it this way : make a class node with data members String key and Bank value like

`class Node{
String key;
Bank value;
Node(String k,Bank val){
    key=k;
    value=val;
}

}`

then make an ArrayList of class Node ,then add elements to the list as [list name].add(new Node([pass key and value here]) by traversing through the map like here

and then you can traverse the list in the desired order and get your elements by [arraylist name].get(index).key or [arraylist name].get(index).value

fuzious
  • 400
  • 5
  • 19