I/T/Java2009. 4. 27. 21:57
※ 이 글은 이전 제 블로그에 있던 글을 옮겨 놓은겁니다.

HashMap을 ArrayList로 변경하는 방법입니다.
총 두 가지 방법이 있습니다.
HashMap 뿐만 아니라 Collection 객체들은 다 변경이 가능할 것 같더군요!

package h2m;
import java.util.ArrayList;
import java.util.HashMap;

/**
 * @Project	: Blog
 * @Name : HashMapToArrayList.java
 * @Date : 2008. 8. 8.
 * @Author : Keep Burning
 * @Blog : http://keepburning.tistory.com
 * @Description : Converting HashMap To ArrayList 
 */
public class HashMapToArrayList {
	
	static public ArrayList getArrayListFromHashMap1(HashMap hashMap) {
		return new ArrayList( hashMap.values() );
	}
	
	static public ArrayList getArrayListFromHashMap2(HashMap hashMap) {
		ArrayList arrayList = new ArrayList();
		arrayList.addAll( hashMap.values() );
		return arrayList;
	}
	
	public static void main(String[] args) {
		HashMap hashMap = new HashMap();
		ArrayList arrayList1;
		ArrayList arrayList2;
			
		hashMap.put("key1", "value1");
		hashMap.put("key2", "value2");
		hashMap.put("key3", "value3");

		arrayList1 = getArrayListFromHashMap1(hashMap);
		arrayList2 = getArrayListFromHashMap2(hashMap);
		
		for(int i=0 ; i<arrayList1.size() ; i++) {
			System.out.println("arrayList1[" + i + "] : " + arrayList1.get(i));
		}
		
		for(int i=0 ; i<arrayList2.size() ; i++) {
			System.out.println("arrayList2[" + i + "] : " + arrayList2.get(i));
		}
		
	}

}


Posted by 황타