728x90

이번에는 MainActivity.java 에서

ArrayList<HashMap<String, String>> calList = new ArrayList<HashMap<String, String>>();

LinkedHashMap<String, Calendar_Item> calList = new LinkedHashMap<String, Calendar_Item>();

로 변경하면 어떤 것들을 수정해줘야 할까?


먼저 https://link2me.tistory.com/1717 게시글을 읽고 나서 아래 비교 내용을 보면 좀 더 이해하는데 도움이 될 것으로 본다.

HashMap 을 사용하면 입력한 순서대로 출력이 될 것을 기대하지만 뒤죽박죽으로 결과를 보여준다.


MainActivity.java 수정사항

                if(CurrentMonth-1 == thisMonth){ // 현재월이면
                    if(i == thisDay){
                        Log.e(TAG, "key := " + key);
                        int index = getIndexOfCalList(key);
                        HashMap<String, String> item = calendarItem(String.valueOf(yy),String.valueOf(CurrentMonth),String.valueOf(i),weekday,"CYAN","",key);
                        calList.set(index,item);
                    }
                }


    private HashMap<String, String> calendarItem(String year, String month, String day, int weekday, String color, String name, String key){
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("year",year);
        map.put("month",month);
        map.put("day",day);
        map.put("weekday", String.valueOf(weekday));
        map.put("color",color);
        map.put("event",name);
        map.put("key",key);
        return map;
    }

    private void addCalendarItem(String year, String month, String day, int weekday, String color,String name, String key){
        HashMap<String, String> item = calendarItem(year,month,day,weekday,color,name,key);
        calList.add(item);
    }

    private int getIndexOfCalList(String search_key) {
        for (int temp = 0; temp < calList.size(); temp++) {
            String key = calList.get(temp).get("key");
            if (key != null && key.equals(search_key)) {
                return temp;
            }
        }
        return -1;
    }

                if(CurrentMonth-1 == thisMonth){ // 현재월이면
                    if(i == thisDay){
                        Log.e(TAG, "key := " + key);
                        addCalendarItem(String.valueOf(yy),String.valueOf(CurrentMonth),String.valueOf(i),weekday,"CYAN","",key);
                    }
                }

    private Calendar_Item calendarItem(String year, String month, String day, int weekday, String color, String name, String key){
        Calendar_Item item = new Calendar_Item();
        item.setYear(year);
        item.setMonth(month);
        item.setDay(day);
        item.setWeekday(weekday);
        item.setColor(color);
        item.setEvent(name);
        return item;
    }

    private void addCalendarItem(String year, String month, String day, int weekday, String color,String name, String key){
        Calendar_Item item = calendarItem(year,month,day,weekday,color,name,key);
        calList.put(key,item);
    }


GridCellAdapter.java 수정사항

    private final ArrayList<HashMap<String, String>> calList;

    public GridCellAdapter(Context context, ArrayList<HashMap<String, String>> list) {
        mContext = context;
        calList = list;
    }

    public HashMap<String, String> getItem(int position) {
        return calList.get(position);
    }

        String theday = calList.get(position).get("day");
        int themonth = Integer.parseInt(calList.get(position).get("month"));
        String theyear = calList.get(position).get("year");
        String holiday = calList.get(position).get("event");

    private final LinkedHashMap<String, Calendar_Item> calList;
    private String[] mKeys;

    public GridCellAdapter(Context context, LinkedHashMap<String, Calendar_Item> list) {
        mContext = context;
        calList = list;
        mKeys = calList.keySet().toArray(new String[list.size()]);
    }

    public Calendar_Item getItem(int position) {
        return calList.get(mKeys[position]);
    }

        String theday = calList.get(mKeys[position]).getDay();
        int themonth = Integer.parseInt(calList.get(mKeys[position]).getMonth());
        String theyear = calList.get(mKeys[position]).getYear();
        String holiday = calList.get(mKeys[position]).getEvent();


어떤 것을 사용하든지 검색 속도가 느리지 않으면서 확장성을 고려한 메소드를 적절하게 사용하면 된다.


공휴일 등록, 기념일 등록, 일정 등록 등의 루틴은 여기에는 적지 않았다.


도움이 되셨다면 ... 해 주세요. 좋은 글 작성에 큰 힘이 됩니다.

블로그 이미지

Link2Me

,
728x90

HashMap ketSet<String> 을 String 배열로 만들는 법을 적어둔다.

String[] mKeys = map.keySet().toArray(new String[map.size()]);

를 활용할 기회가 있었다.

key 로부터 map.get(key); 로 값을 얻을 수 있다.


key 를 배열로 만들어야 value를 얻을 수 있을 때 아래 예제를 살펴보면 도움이 될 것이다.


import java.util.HashMap;
import java.util.Map;

public class HashMap_Test {

    public static void main(String[] args) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("1", "대조영");
        map.put("2", "이순신");
        map.put("4", "강감찬");
        map.put("3", "양만춘");
        map.put("5", "을지문덕");
        map.put("3", "연개소문"); // HashMap 은 key 가 같고 값이 다르면 value를 덮어쓰기를 한다.
       
        // Getting the value of "4" key.
        System.out.println("The Value is: " + map.get("4"));

        map.containsKey("강아지"); // key 가 존재하면 true 반환
        map.containsValue("조나비"); // value 가 존재하면 true 반환

        // Using keySet() to get the set view of keys
        System.out.println("The set is: " + map.keySet());
       
        String[] mKeys = map.keySet().toArray(new String[map.size()]);
        for (String key : mKeys) {
            System.out.println(key);
        }
       
        for(int i=0; i < mKeys.length;i++) {
            System.out.println("key :" + mKeys[i] + ", value : " + map.get(mKeys[i]));
        }
    }

}
 


HashMap의 경우 단점이 put을 통해 데이터나 객체를 넣을때 key의 순서가 지켜지지 않는다는 것이다.
개발을 할때 코드상으로 순차적으로 key/value를 넣어도, 실제 HashMap에서는 해당 순서가 지켜지지 않는다.
만약 입력된 Key의 순서가 보장되어야 한다면 LinkedHashMap을 사용하면 된다.

LinkedHashMap 은 기본적으로 HashMap을 상속받아 만들어져 있게 때문에 HashMap의 기능을 그대로 사용 가능하다.
대신 여기에 순서라는 개념이 들어 갔다.



참고하면 도움이 될 자료

https://stackoverflow.com/questions/5234576/what-adapter-shall-i-use-to-use-hashmap-in-a-listview


블로그 이미지

Link2Me

,