Jam's story

[컬렉션 프레임워크] - TreeSet , HashMap 본문

Java

[컬렉션 프레임워크] - TreeSet , HashMap

애플쩀 2022. 3. 26. 23:27
TreeSet

public class Ex13 {

	public static void main(String[] args) {
	TreeSet set=new TreeSet();
	String from="b";
	String to="d";
	
set.add("abc"); set.add("alien"); set.add("bat");
set.add("car"); set.add("Car"); set.add("disc");
set.add("dance"); set.add("dZZZ");

System.out.println(set);
System.out.println("range Search"+from+ to);
System.out.println("result1:"+set.subSet(from, to));
System.out.println("result2:"+set.subSet(from, to+"zzz"));


}
}
subSet(a,b)를 사용할때, b전까지만 포함되지만  
뒤에 "zzz"를 붙이게 되면 
b~ dzzz까지 허용한다는 것이기 때문에, d로 시작하는 단어는 다 포함된다. 
headSet, tailSet  지정된 기준값으로 나누어진다. 
TreeSet set=new TreeSet();
	int[] score={80, 95,50,35,45,65,20,100};
	
	for (int i = 0; i < score.length; i++) {
		set.add(new Integer(score[i]));
	}
	
	System.out.println(set.headSet(new Integer(50)));
	System.out.println(set.tailSet(new Integer(50)));
HashMap HashTable 
  • HahsMap을 더 자주쓴다 .
  • 하나의 클래스로 정의해서 하나의 배열로 다루듯이 데이터의 무결성 면으로는 더 바람직하다 
  • 키는 유일 값은 중복허용 
  • null값 허용- HashMap  / HashTable은 불가능 
    • map.get(null); map.put(null); 가능 
public class Ex13 {

	public static void main(String[] args) {
HashMap map=new HashMap();
map.put("myid", "1234");
map.put("asdf","1111");
map.put("asdf","1234");

Scanner s=new Scanner(System.in);

while(true) {
	System.out.println("id와 passwd를 입력하세요 ");
	System.out.println("id:");
	String id=s.nextLine().trim();
	
	System.out.println("password");
	String password=s.nextLine().trim();
	System.out.println();
	
	if(!map.containsKey(id)) {
		System.out.println("입력한 키는 존재하지 않습니다.");
		continue;
	}
	if(!map.get(id).equals(password)) {
		System.out.println("비밀번호가 일치하지 않스빈다.");
	}else {
		System.out.println("id랑 비밀번호가 일치합니다.");
		break;
	}
}

 

entrySet()을 이용하면  키 값을 모두 가져올 수 있다. 
getKey()
getValue() 따로 이용도 가능 
public class HashMapEx02 {
public static void main(String[] args) {
	HashMap map=new HashMap();
	map.put("김자바", new Integer(100));
	map.put("이자바", new Integer(100));
	map.put("강자바", new Integer(80));
	map.put("안자바", new Integer(90));
	
	Set set=map.entrySet();
	Iterator it=set.iterator();
	while (it.hasNext()) {
		Map.Entry e = (Map.Entry) it.next();
//		System.out.println( e.getKey() + e.getValue());

	}
	set=map.keySet();
	System.out.println("참가자 명단:"+set);
	
	Collection values=map.values();
	it=values.iterator();
int total=0;
while (it.hasNext()) {
	Integer i = (Integer) it.next();
	total+=i.intValue();
}
System.out.println("총점"+total);
System.out.println("평균"+total/set.size());
System.out.println("최고점수 "+ Collections.max(values));
System.out.println("최저점수:"+Collections.min(values));
}
}
public class HashMapex3 {
	static HashMap phoneBook=new HashMap();
public static void main(String[] args) {
	addPhoneNo("친구","이자바","010-111-11111");
	addPhoneNo("친구","김자바	","2");
	addPhoneNo("친구","김자바","010-3-");
	addPhoneNo("회사","이과장","010-3");
	addPhoneNo("회사","김대리","010-4");
	addPhoneNo("회사","박대리","010-6");
	addPhoneNo("회사","김대리","010-5");
	addPhoneNo("세탁","010-8");
	
	printList();
}//main
static void addPhoneNo(String groupName, String name, String tel) {
	addGroup(groupName);
	HashMap group=(HashMap)phoneBook.get(groupName);
	group.put(tel, group);
}

private static void addGroup(String groupName) {
	// TODO Auto-generated method stub
	if(!phoneBook.containsKey(groupName)) {
			phoneBook.put(groupName, new HashMap());
	}
}
static void addPhoneNo(String name, String tel) {
	addPhoneNo("기타", name, tel);
	
}
static void printList() {
	Set set=phoneBook.entrySet();
	Iterator it=set.iterator();
	
	while (it.hasNext()) {
		Map.Entry e = (Map.Entry) it.next();
		Set subSet=((HashMap)e.getValue()).entrySet();
		Iterator subIt=subSet.iterator();
		while (subIt.hasNext()) {
			Map.Entry subE = (Map.Entry)subIt.next();
			String telNo=(String)subE.getKey();
			String name=(String)subE.getValue();
			System.out.println(name+telNo);
		}
	}
}
	

}//class
public class HashMapex3 {

public static void main(String[] args) {

	String[] data= {"a","k", "a","k","d","k","a", "k","k","z","d"};
	HashMap map=new HashMap();
	for (int i = 0; i < data.length; i++) {
		if(map.containsKey(data[i])) {
			Integer value=(Integer)map.get(data[i]);
			map.put(data[i], new Integer(value.intValue()+1));
		}else {
			map.put(data[i], new Integer(1));
		}
		
	}
	Iterator it=map.entrySet().iterator();
	while (it.hasNext()) {
		Map.Entry entry=(Map.Entry) it.next();
		int  value=((Integer)entry.getValue()).intValue();
		System.out.println(entry.getKey()+":"+printBar('#', value)+""+value);
	}
	
	
}//main

private static String printBar(char c, int value) {
	char[] bar=new char[value];
	for (int i = 0; i < bar.length; i++) {
		bar[i]=c;
	}
	return new String(bar);
}
}//class

 

 

 

'Java' 카테고리의 다른 글

28일차 -입출력 IO  (0) 2022.03.29
days27 -입출력  (0) 2022.03.28
26일차  (0) 2022.03.25
[컬렉션 프레임워크] - 스택 , 큐  (0) 2022.03.25
[컬렉션 프레임워크] -ArrayList  (0) 2022.03.24
Comments