将Object []转换为Hashmap

时间:2015-07-14 08:52:58

标签: java object casting hashmap

我有一个函数来生成接收以下参数的报告

(function(angular) {
    var TaskController = function($scope, $mdDialog, Task) {
        Task.query(function(response) {
            $scope.tasks = response ? response : [];
        });

        $scope.addTask = function(title) {
            new Task({
                title: title,
                checked: false
            }).$save(function(task) {
                $scope.tasks.push(task);
            });
            $scope.newTitle = "";
            $mdDialog.hide();
        };

        $scope.showDialog = function(ev) {
            $mdDialog.show({
                controller: TaskController,
                templateUrl: 'taskdialog.tmpl.html',
                parent: angular.element(document.body),
                targetEvent: ev,
            });
        };

        $scope.updateTask = function(task) {
            task.$update();
        };

        $scope.deleteTask = function(task) {
            task.$remove(function() {
                $scope.tasks.splice($scope.tasks.indexOf(task), 1);
            });
        };
    };

    TaskController.$inject = ['$scope', '$mdDialog', 'Task'];
    angular.module("taskApp.controllers").controller("TaskController", TaskController);
}(angular));

我尝试运行以下代码

public void generateReport(String[] headers, Object[] data)

输出错误:

HashMap<String, String>[] hMap = (HashMap<String, String>[]) data;

Collection c = hMap.values();
Iterator itr = c.iterator();    
while(itr.hasNext())
  System.out.println(itr.next());

1 个答案:

答案 0 :(得分:3)

hMapHashMap的数组,而不是HashMap,因此它没有values()方法。您需要hMap[i].values()才能访问数组中values()'i的{​​{1}}。