-
[Java] Map과 Hash MapP.study/JAVA 2020. 6. 5. 19:29
기초를 탄탄히!
Map이란?
-
key와 value 형태로 값이 저장되어 있음.
-
값을 조회할 때 key 값을 통해 value를 구할 수 있음.
-
리스트나 배열처럼 순차적으로 해당 요소를 찾지 않고 key를 통해 value를 찾음.
Key value 91811122 홍길동 91913344 이몽룡 key는 중복되지 않는 학번, value는 이름 / " 사전 "처럼 key를 통해 value를 찾는다!
Map을 구현한 클래스
: HashMap, LinkedHashMap, TreeMap
HashMap
-
value의 Null 값을 허용
-
많은 Map을 저장해야하는 경우 충분한 대용량으로 매핑을 효율적으로 저장할 수 있음
LinkedHashMap
-
put을 통해 넣는 key의 순서가 보장된다.ex) 넣는 순서대로 저장된다.
TreeMap
-
숫자, 대문자, 소문자, 한글 순서대로 정렬하여 저장한다.
ex) z~a 순서대로 넣어도 a~z 순서대로 저장됨.
https://docs.oracle.com/javase/6/docs/api/java/util/Map.html
Map (Java Platform SE 6)
Returns the value to which the specified key is mapped, or null if this map contains no mapping for the key. More formally, if this map contains a mapping from a key k to a value v such that (key==null ? k==null : key.equals(k)), then this method returns v
docs.oracle.com
https://docs.oracle.com/javase/6/docs/api/java/util/HashMap.html
HashMap (Java Platform SE 6)
java.util Class HashMap java.lang.Object java.util.AbstractMap java.util.HashMap Type Parameters:K - the type of keys maintained by this mapV - the type of mapped values All Implemented Interfaces: Serializable, Cloneable, Map Direct Known Subclasses: Link
docs.oracle.com
Map 메소드
put
: 값 넣는다.
HashMap<String, String> map = new HashMap<String, String>(); map.put("people", "사람"); map.put("baseball", "야구");
get
: key 값을 통해 value 구한다.
System.out.println(map.get("people"));
containsKey
: key 값을 검색해 있으면 true, 없으면 false를 리턴한다.
System.out.println(map.containsKey("people"));
remove
: key값에 해당되는 value를 삭제하고 value 값을 리턴한다.
System.out.println(map.remove("people"));
size
: 해당 Map의 크기를 리턴한다.
System.out.println(map.size());
Volley 에서 Map과 HashMap을 알아보자.
예제 )
protected Map<String, String> getParams() throws AuthFailureError { Map<String,String> params = new HashMap<String,String>(); return params; }
Map<String, String>
: key = String 와 value = String 인 Map 인터페이스
throws
: 예외 처리를 다른 함수로 넘겨주는 예약어
protected Map<String, String> getParams() throws AuthFailureError
: getParams 메소드 재정의
Map<String,String> params = new HashMap<String,String>();
: HashMap 객체 변수 params 생성 (key = String , value = String)
https://www.edwith.org/boostcourse-android/lecture/17091/
[LECTURE] 1) Volley 사용하기 : edwith
들어가기 전에 웹서버에 요청하고 응답을 받을 때는 HttpURLConnection을 사용할 수 있습니다. 하지만 요청과 응답을 위한 코드의 양이 많은 데다가 스레드를 사용하면서 더... - 부스트코스
www.edwith.org
-