简体   繁体   中英

How to Assign ArrayList hashmap<String,Double[]> to List

I'm trying to assign Hashmap values of Double[] to List, but it is throwing an error:

"The method add(Double) in the type List is not applicable for the arguments (Double[])"

ArrayList<HashMap<String, Double[]>> arl = (ArrayList<HashMap<String, Double[]>>) pd.getArrayList();

List<Double> empid = new ArrayList<Double>();
Iterator itr = arl.iterator();

while (itr.hasNext()) {
    HashMap<String, Double[]> map = (HashMap<String, Double[]>) itr.next();
    empid.add(map.get("id")));
}

How can I cast Double[] to Double.Request ?

If converting to List<Double> is your only option, try this:

ArrayList<HashMap<String, Double[]>> arl = (ArrayList<HashMap<String, Double[]>>) pd.getArrayList();

List<Double> empid = new ArrayList<Double>();
Iterator itr = arl.iterator();

while (itr.hasNext()) {
    HashMap<String, Double[]> map = (HashMap<String, Double[]>) itr.next();
    for (Double d : map.get("id") {
        empid.add(d);
    }

}

I think you have a bigger issue than casting. You have an array of Doubles , and you ultimately want a single Double .

That's not a casting problem, but a problem of how you manipulate those Doubles to give you one only.

Perhaps what you're really wanting is to iterate through that array, and add each Double in turn to the list. But this is really a requirements issue.

In the code you provide, you must change

List<Double> empid = new ArrayList<Double>();

by

List<Double[]> empid = new ArrayList<Double[]>();

to make it compiles. But I guess this is not what you are trying to achieve. It that what you want to do ? :

        ArrayList<HashMap<String, Double[]>> arl = (ArrayList<HashMap<String, Double[]>>) pd.getArrayList();
        List<Double> empid = new ArrayList<Double>();
        Iterator itr = arl.iterator();
        while (itr.hasNext()) {
            HashMap<String, Double[]> map = (HashMap<String, Double[]>) itr.next();
            empid.addAll(Arrays.asList(map.get("id")));
        }

The problem is map.get("id") will return Double[] but empid is of type Double .

You just change

List<Double> empid = new ArrayList<Double>();

to

List<Double[]> empid = new ArrayList<Double[]>();

You are trying to insert an array of type Double in a list where the elements are "single" Doubles.

To insert an array in the empid list you should declare it in this way:

List<Double[]> empid = new ArrayList<Double[]>();

Then when you call add on this list you can pass an argument of type Double[].

What you seem to be looking is this line:

empid.add(map.get("id")));

changed to this:

empid.addAll(Arrays.asList(map.get("id"))));

It will convert an array of Double to a List of Double , and addAll will add all items from the resulting collection to your target collection empid .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM