简体   繁体   中英

HashMap first key is null

i'am using hasmap and LinkedHasmap for a while but now in this situation is acting strang, i'am adding to a hasmap a key and and a value from 2 array's with the same size in this case with size=4, but the hasmap is putting the first key as null in the debbuging i see it will overwrite the the second key in the same position has the first, it never has the same key and never the same has so i cant understand whats happening.

The hasmap is declared has a field.

    private HashMap<String, String> courses = new HasMap<String, String>();

And in this method i want to fill the hasMap:

private void coursesInit(int coursesListSize) {

    for (int j = 0; j < coursesListSize; j++) {

        LayoutInflater inflater = (LayoutInflater) this
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        LinearLayout inserPoint = (LinearLayout) findViewById(R.id.linear_layout_inside_left);

        LinearLayout row = new LinearLayout(this);
        row.setLayoutParams(new LayoutParams(
                android.view.ViewGroup.LayoutParams.MATCH_PARENT,
                android.view.ViewGroup.LayoutParams.WRAP_CONTENT));
        View view = inflater.inflate(R.layout.courses_button_left_drawer,
                null);

        Button btnTag = (Button) view.findViewById(R.id.course_id);
        btnTag.setLayoutParams(new LayoutParams(
                android.view.ViewGroup.LayoutParams.MATCH_PARENT,
                android.view.ViewGroup.LayoutParams.WRAP_CONTENT));

        if (coursesIdList.isEmpty()) {
            btnTag.setText("Your Course " + (j + 1));
            btnTag.setId(j);

        } else {
            btnTag.setText(coursesNamesList.get(j));
            btnTag.setId(Integer.parseInt(coursesIdList.get(j)));

        // Populate LinkedHasMap in the correct order where Key=
        // course_id && Value= course_fullname
        courses.put(coursesIdList.get(j), coursesNamesList.get(j));

        }

        if (j != 0) {
            btnTag.setBackgroundResource(R.drawable.border_inside);
        }

        row.addView(view);

        inserPoint.addView(row, 3);
    }

}

I also had tried with just Map and LinkedHasMap and inside a method with just a for looping the array's but its the same resut.

The debbuging screenshots:

courses HasMap: Courses Hasmap

Arrays: Arrays

If your problem is that the table[0] element of the HashMap instance is null in the debugger, then this is NOT a problem as long as HashMap works as explained in the documentation (which it does). Internal implementation details should not matter for you (it's a black box for you to use). If you want to retreive the keys of the map, use HashMap#entrySet .

See following example of a single insertion in a HashMap and the resulting HashMap#table field:

 final Map<String, String> map = new HashMap<String, String>();
 map.put("One", "1");

在此输入图像描述

As you can see, the element is inserted at table[12] .

Also keep in mind HashMap allows null as key , so if you set key as null, this is a valid key.

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