简体   繁体   中英

Is it a bad way to force a List to a HashMap?

I will submit my code, explain it, and ask you questions.

The following code is part of the Controller code.

        HashMap<String, Object> result = bService.getBoardListPage(params, page-1);
     System.out.println("result : " + result);
     // Deriving the desired value among the List values ??existing in the HashMap -----------------------------------------
     List<?> list = (List<?>) result.get("boardList");

     for(int i = 0; i < list.size(); i++)
     {
        HashMap<String, Object> map = (HashMap<String, Object>) list.get(i);
        System.out.println("map 1 : " + map);
        int all_boardIDX = (Integer) map.get("board_idx");
        HashMap<String, Object> replyBoard = bService.boardApplicableReplyBoard(all_boardIDX);

        map.put("hh", replyBoard);
        System.out.println("map 2 : " + map);
     }
     // -------------------------------------------------------------------------------------

Above that, the code is to reply to the post. (It's Reply. not comment)

HashMap<String, Object> result = bService.getBoardListPage(params, page-1);

The above result contains information about the posts.

The log of result is shown below.

result : {current=1, last=1, start=1, end=10, boardList=[{phone_2=9959, comment_count=0, phone_1=010, board_idx=6, phone_3=7432, board_title=zzz, pw=1, sex=false, birth=1993-12-04, board_writer=test2, board_readNum=1, board_content=hhh, fileCount=0, board_writeDate=2017-12-07 18:44:44.0, authority=false, name=이, id=test2, idx=3, board_goodNum=0, email=rkdhfl1470@naver.com}

I want to put information about the reply in the boardList that exists in result !

So, I got the boardList as below.

List<?> list = (List<?>) result.get("boardList");

Then I turned around the for statement for the size of the list, and worked on the for statement as follows to include the reply information.

          for(int i = 0; i < list.size(); i++)
         {
            HashMap<String, Object> map = (HashMap<String, Object>) list.get(i);
            System.out.println("map 1 : " + map);
            int all_boardIDX = (Integer) map.get("board_idx");
            HashMap<String, Object> replyBoard = bService.boardApplicableReplyBoard(all_boardIDX);

            map.put("hh", replyBoard);
            System.out.println("map 2 : " + map);
         }

As a result, it worked the way I wanted!

map 2 : {phone_2=9959, comment_count=0, hh={rb_title=[답글] Test, rb_writeDate=2017-12-07 15:38:03.0, board_idx=1, rb_idx=1, rb_writer=admin, rb_content=HH, rb_readNum=0, idx=1}

But I'm not feeling well.

The reason is that, This is because in the code below, a warning such as "Type safety: Uncheckd cast from capture # 3-of? To HashMap " has occurred.

HashMap<String, Object> map = (HashMap<String, Object>) list.get(i);

So I'm curious. Did I write the code correctly?

How can I improve my code?

And is not it right to cast the List type to a HashMap?

As a beginner I have a lot of questions. Please tell me your opinion.

Thank you for seeing the long sentence.

And is not it right to cast the List type to a HashMap?

It's fine if you can guarantee it won't throw a java.lang.ClassCastException . You know exactly what objects are going into the lists/maps, so its fine.

OR

You can define your list like this:

List<HashMap<String, Object>>

Which I think will get rid of the warning. Then you don't have to cast it anymore either.

Type safety is really there to help you. Or others that need to maintain your code in the future. Using List<HashMap<String, Object>> you are telling the compiler that this List will always contain elements of HashMap<String, Object> . So you'll actually get compile-time errors if you attempt to treat the elements of your List as if they were objects of a different Class .

Whereas with List<?> you aren't telling the compiler anything that will allow it keep your code type-safe (ie. avoid throwing java.lang.ClassCastException ). So if you screw up, you'll only see the errors at Runtime and only under a failure condition.

Hope that makes sense!

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