Programming/JAVA

hashTable 이용해서 단어들 조합하기

현무랑 2019. 8. 23. 09:00
반응형

 

용어를 조합해야하는 일이 간혹?? 생긴다. 

 

이때 hashTable을 이용하면 쉽게 조합할 수있다. 

 

처음에 해쉬테이블을 이용하지 않고 배열로 잘못생각해서 반나절을 날렸다....

 

다음에 이런일이 없기를 바라며...  더 좋은 방법이 있다면 알려주세요..

 

아래는 코딩하면서 만든 예제 코드. 

package test1;

import java.util.Enumeration;
import java.util.Hashtable;

public class TestHashTable {

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		Hashtable<String, String> hash_table = new Hashtable<String, String>(); 
		Hashtable<String, String> hash_table2 = new Hashtable<String, String>(); 
		Hashtable<String, String> resultString = new Hashtable<String, String>(); 
		
		// Inserting Values into table 
		hash_table.put("10", "Geeks"); 
		hash_table.put("15", "happy"); 

		hash_table2.put("10", "hash21"); 
		hash_table2.put("15", "hash22"); 
		hash_table2.put("30", "hash23"); 

    	Enumeration keys = hash_table.keys();
    	while (keys.hasMoreElements()) {
    		// Key 를 찾고 찾은 key로 Value를 찾습니다.
    		String key = (String) keys.nextElement();
    		System.out.println("Key: "+ key +" value: "+ hash_table.get(key));

    		// 반복문 안에 반복 곱을 위해서.. 
    		Enumeration inKeys = hash_table2.keys();
    		while (inKeys.hasMoreElements()) {
    			// Key 를 찾고 찾은 key로 Value를 찾습니다.

    			String key2 = (String) inKeys.nextElement();
    			System.out.println("Key: "+ key2 +" value: "+ hash_table2.get(key2));
    			resultString.put(hash_table.get(key) + "_" + hash_table2.get(key2), hash_table.get(key) + "_" + hash_table2.get(key2));
    		}
    	}
		
    	
		// Displaying the Hashtable 
		System.out.println("The Hashtable is: " + hash_table); 
		System.out.println("The resultString is: " + resultString); 
//		// Clearing the hash table using clear() 
//		hash_table.clear(); 
//
//		
//		// Displaying the final Hashtable 
//		System.out.println("Finally the table looks like this: " + hash_table); 
//		
		
		
	}

}

 

반응형

'Programming > JAVA' 카테고리의 다른 글

[Java] substring , indexOf , charAt 을 이용한 문자열  (0) 2019.08.24
Hashtable clear() Method in Java  (1) 2019.08.22
HashTable 예제  (0) 2019.08.21