我试图把ArrayList<Burger>
为ArrayList<Object>
,但我得到这个错误。
我想要ArrayList<Object>
作为对象,我该如何去做这件事?
这是它给我的错误thisModelInfo.put(…);
The method put(String, ArrayList<Object>) in the type HashMap<String,ArrayList<Object>> is not applicable for the arguments (String, ArrayList<Burger>)
这是我的代码。
package com.ashgharibyan.apiofapis.controllers;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ashgharibyan.apiofapis.models.Burger;
import com.ashgharibyan.apiofapis.models.User;
import com.ashgharibyan.apiofapis.services.BurgerService;
import com.ashgharibyan.apiofapis.services.UserService;
@RestController
public class UserAPIController {
@Autowired
private UserService userServ;
@Autowired
private BurgerService burgerServ;
@RequestMapping("/api/all")
public ArrayList<HashMap<String, ArrayList<HashMap<String, ArrayList<Object>>>>> index() {
List<User> allUsers = (List<User>) userServ.allUsers();
ArrayList<HashMap<String, ArrayList<HashMap<String, ArrayList<Object>>>>> mainAPI = new ArrayList<>();
for(int i=0; i<allUsers.size();i++) {
// One User Level
HashMap<String, ArrayList<HashMap<String, ArrayList<Object>>>> thisUserInfo = new HashMap<String, ArrayList<HashMap<String, ArrayList<Object>>>>();
//All Model Level
ArrayList<HashMap<String, ArrayList<Object>>> allModelInfo = new ArrayList<>();
//Current Model Level
HashMap<String, ArrayList<Object>> thisModelInfo = new HashMap<String, ArrayList<Object>>();
ArrayList<Burger> allBurgers = (ArrayList<Burger>) burgerServ.allBurgers();
thisModelInfo.put("burger", allBurgers);
allModelInfo.add(thisModelInfo);
thisUserInfo.put(allUsers.get(i).getUserName(), allModelInfo);
mainAPI.add(thisUserInfo);
}
return mainAPI;
}
}
我不知道该怎么做才能使它工作。我需要它是动态的,因为我将改变我正在导入的模型名称。
也可以将ArrayList<Burger>
修改为ArrayList<Object>
或把
HashMap<String, ArrayList<HashMap<String, ArrayList<Object>>>> thisUserInfo = new HashMap<String, ArrayList<HashMap<String, ArrayList<Object>>>>();
HashMap<String, ArrayList<HashMap<String, ArrayList<Burger >>>> thisUserInfo = new HashMap<String, ArrayList<HashMap<String, ArrayList< Burger>>>>();