자바 hashMap, hashSet, Hashtable 옮 겨 다 니 기

3154 단어
import java.io.File;
import java.io.FileOutputStream;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.Map.Entry;

//Java    
public class Test {

	public static void main(String[] args) throws Exception {
		/**
		 * String osArch = System.getProperty("os.arch"); String model =
		 * System.getProperty("sun.arch.data.model"); String cpu =
		 * System.getProperty("sun.cpu.isalist"); String vm_name =
		 * System.getProperty("java.vm.name"); //java.vm.name=Java HotSpot(TM)
		 * Client VM System.out.println(osArch); System.out.println(model);
		 * System.out.println(cpu);
		 */
		// traverseHashtable_property();
		// traverseHashMap();
		// traverseHashSet();
		traverseHashtable();

	}

	private static void traverseHashtable() {
		Hashtable<Object,Object> table = new Hashtable<Object, Object>();
		table.put(1, "11");
		table.put(2, "12");
		table.put(3, "13");
		//   key
		Enumeration e = table.keys();
		while (e.hasMoreElements()) {
			System.out.println(e.nextElement());
		}
		//   value
		e = table.elements();
		while (e.hasMoreElements()) {
			System.out.println(e.nextElement());
		}
		
		Set<Map.Entry<Object, Object>> entrys = table.entrySet();
		for (Entry<Object, Object> entry : entrys) {
			System.out.println(entry.getKey() + "=" + entry.getValue());
		}

	}

	//   HashSet
	private static void traverseHashSet() {
		Set set = new HashSet();
		for (int i = 0; i < 100; i++) {
			set.add("123" + i);
		}

		for (Iterator it = set.iterator(); it.hasNext();) {
			System.out.println(it.next());
		}

	}

	//   properties
	private static void traverseHashtable_property() throws Exception {
		Properties properties = System.getProperties();//   hashtable
		StringBuilder sb = new StringBuilder();
		Set<Map.Entry<Object, Object>> entrys = properties.entrySet();
		for (Entry<Object, Object> entry : entrys) {
			sb.append(entry.getKey() + "=" + entry.getValue() + "
"); } System.out.print(sb.toString()); File file = new File("log.txt"); if (file.exists()) { file.delete(); } writeTXT("log.txt", sb.toString()); } /** HashMap */ private static void traverseHashMap() { Map<Integer, String> map = new HashMap<Integer, String>(); for (int i = 0; i < 100; i++) { map.put(i, "123" + i); } for (Entry<Integer, String> entry : map.entrySet()) { System.out.println(entry.getKey() + "=" + entry.getValue()); } for (Object obj : map.keySet()) { Object key = obj; Object value = map.get(obj); System.out.print(key + "=" + value); } } private static void writeTXT(String filePath, String data) throws Exception { FileOutputStream outStream = new FileOutputStream(filePath); outStream.write(data.getBytes("UTF-8")); outStream.flush(); outStream.close(); } }

좋은 웹페이지 즐겨찾기