FIF's 코딩팩토리

자바 기초 NCS교육과정(45)-컬렉션4 본문

Back-End/국비 NCS교과과정

자바 기초 NCS교육과정(45)-컬렉션4

FIF 2019. 8. 2. 13:52
반응형

Java194_TreeSet.java

public class Java194_TreeSet {

	public static void main(String[] args) {
		TreeSet<Integer> tree = new TreeSet<Integer>();
		tree.add(new Integer(10));
		tree.add(new Integer(40));
		tree.add(new Integer(20));
		tree.add(new Integer(30));
		tree.add(new Integer(20));
		
		//기본 오름차순
		for(Integer it:tree)
			System.out.println(it);
		
		
		Iterator<Integer> ita=tree.iterator();
		while(ita.hasNext())
			System.out.println(ita.next());
		
		//내림차순
		System.out.println("내림차순");
		Iterator<Integer> ite=tree.descendingIterator();
		while(ite.hasNext())
			System.out.println(ite.next());
		
	}

}

출력결과

 

 HashSet
  1 set인터페이스를 구현한 클래스이다


  2 중복허용안되고, 순서유지 안된다

 

Java195_HashSet.java

public class Java195_HashSet {

	public static void main(String[] args) {
		HashSet<Integer> set=new HashSet<Integer>();
		set.add(new Integer(10));
		set.add(new Integer(20));
		set.add(new Integer(30));
		set.add(new Integer(30));
		for(Integer it:set)
			System.out.println(it);

	}

}

출력결과

 

Hashtable
  1 Map인터페이스 구현해놓은 클래스


  2 Map인터페이스를 구현해놓은 클래스들은 key,value쌍으로 저장한다.


  3 value을 구현해주는 것은 key이므로 key은 중복을 허용하지 않는다.

 

Java196_Hashtable.java

public class Java196_Hashtable {

	public static void main(String[] args) {
		Hashtable<Integer, String> table= new Hashtable<Integer,String>();
		
		table.put(10, "java");
		table.put(20, "jsp");
		table.put(30, "spring");
		
		System.out.println(table.get(20)); //jsp
		System.out.println("//Enumeration////////////");
		Enumeration<Integer> enu = table.keys();
		while(enu.hasMoreElements()) {
			Integer key=enu.nextElement();
			System.out.printf("%d:%s\n",key,table.get(key));
		}
		
		System.out.println("//////Iterator///////////");
		Set<Integer> set=table.keySet();
		Iterator<Integer> ite= set.iterator();
		while(ite.hasNext()) {
			Integer key=ite.next();
			System.out.printf("%d:%s\n",key,table.get(key));
		}
	}
}

출력결과

 

 

HashMap
  1 Map인터페이스를 구현한 클래스이다.


  2 Hashtable은 동기화 처리하고, HashMap은 비동화 처리한다.


  3 Hashtable과 Hashmap의 다른 기능들은 거의 같다.

 

Java197_HashMap.java

public class Java197_HashMap {

	public static void main(String[] args) {
		HashMap<Integer, Number> map = new HashMap<Integer, Number>();
		map.put(1, new Integer(10));
		map.put(2, new Double(4.5));
		map.put(3, new Float(2.3f));

		System.out.println(map.get(2));
		Set<Integer> set = map.keySet();
		Iterator<Integer> ite = set.iterator();
		while (ite.hasNext()) {
			Integer key = ite.next();
			System.out.printf("%d:%s\n", key, map.get(key));
		}
	}
}

출력결과

 

 

TreeMap
  1 Map인터페이스를 구현한 클래스이다.


  2 정렬을 제공하는 클래스이다.

 

Java198_TreeMap.java

public class Java198_TreeMap {

	public static void main(String[] args) {
		TreeMap<Integer,String> tree=new TreeMap<Integer,String>();
				tree.put(10, "java");
		tree.put(30, "spring");
		tree.put(20, "jsp");
		Set<Integer> set=tree.keySet();
		Iterator<Integer>ite=set.iterator();
		while(ite.hasNext()) {
			Integer key=ite.next();
			System.out.printf("%d:%s\n",key,tree.get(key));
		}
		
	}

}

출력결과

반응형
Comments