- Hashtable은 저장되는 순서가 유지 되지 않는다. 또한 null key나 value를 가질 수 없다.

- HashMap은 저장되는 순서가 유지 된다. 또한 null key나 value를 가질 수 있다.

- 둘 다 key 중복시 마지막 저장한 value로 덮어쓴다. 또한 다른 자료구조보다 빠른 검색이 가능하다.


import java.util.HashMap;

import java.util.Hashtable;

import java.util.Set;


public class dailyCode {

public static void main(String[] args) {

Hashtable<String, String> ht = new Hashtable<String, String>();

ht.put("name", "Alex");

ht.put("age", "21");

//ht.put("age", null); Hashtable can't have null value.

HashMap<String, String> hm = new HashMap<String, String>();

hm.put("name", null);

hm.put("age", null); //HashMap can have null value.

hm.put("name", "James");

Set<String> keys = ht.keySet();

for(String key : keys) {

System.out.println(key + " = " + ht.get(key));

}

keys = hm.keySet();

for(String key : keys) {

System.out.println(key + " = " + hm.get(key));

}

}


'프로그래밍 > JAVA' 카테고리의 다른 글

Find Google IP (InetAddress class Example)  (0) 2016.11.25
람다 표현식 (Lambda Expression)  (0) 2016.11.24
Arrays class Example  (0) 2016.11.23
Generics and Wildcard  (0) 2016.11.20
arraycopy Example  (0) 2016.11.19
scheduleAtFixedRate vs. scheduleWithFixedDelay  (0) 2016.11.18
Callback using interface  (0) 2016.11.17

Collection<dataType> variable = new Collection<dataType>();


import java.util.List;

import java.util.ArrayList;


public class dailyCode {

public static void main(String[] args) {

List<String> strList = new ArrayList<String>();

strList.add("aaa");

strList.add("bbb");

strList.add("ccc");

List<Integer> intList = new ArrayList<Integer>();

intList.add(111);

intList.add(222);

intList.add(333);

printList(strList);

printList(intList);

}

private static void printList(List<?> list) {

for(Object obj : list) {

System.out.println(obj);

}

System.out.println("---");

}


'프로그래밍 > JAVA' 카테고리의 다른 글

람다 표현식 (Lambda Expression)  (0) 2016.11.24
Arrays class Example  (0) 2016.11.23
Hashtable and HashMap Example  (0) 2016.11.21
arraycopy Example  (0) 2016.11.19
scheduleAtFixedRate vs. scheduleWithFixedDelay  (0) 2016.11.18
Callback using interface  (0) 2016.11.17
Singleton Design Pattern  (0) 2016.11.16

void java.lang.System.arraycopy(Object src, int srcPos, Object dest, int destPos, int length)



public class dailyCode {

public static void main(String[] args) {

int[] src = {1, 2, 3, 4, 5};

int[] dst = {0, 0, 0, 0, 0, 6, 7, 8, 9, 10};

int[] newSrc = {0, 0, 0, 0, 0};

System.arraycopy(src, 0, dst, 0, src.length);

for(int i : dst) {

System.out.print(i + " ");

}

System.out.println("");

System.arraycopy(newSrc, 0, dst, 5, src.length);

for(int i : dst) {

System.out.print(i + " ");

}

System.out.println("");

System.arraycopy(newSrc, 0, dst, 1, 2);

for(int i : dst) {

System.out.print(i + " ");

}

}


'프로그래밍 > JAVA' 카테고리의 다른 글

Arrays class Example  (0) 2016.11.23
Hashtable and HashMap Example  (0) 2016.11.21
Generics and Wildcard  (0) 2016.11.20
scheduleAtFixedRate vs. scheduleWithFixedDelay  (0) 2016.11.18
Callback using interface  (0) 2016.11.17
Singleton Design Pattern  (0) 2016.11.16
초기화 블록 (Initialization Block)  (0) 2016.11.15

+ Recent posts