본문 바로가기
Java/이것이 자바다

이자바 15장(컬렉션 프레임워크) 확인문제

by k-mozzi 2023. 5. 2.
반응형

 

- 5번

List<Board> 변수 = new ArrayList<Board>();

 

 

- 6번

Map<String, Integer> 변수 = new HashMap<String, Integer>();

 

 

- 7번

package ch15;

import java.util.ArrayList;
import java.util.List;

class Board {
	private String title;
	private String content;

	public Board(String title, String content) {
		this.title = title;
		this.content = content;
	}

	public String getTitle() {
		return title;
	}

	public String getContent() {
		return content;
	}
}

class BoardDao {
	public List<Board> getBoardList() {
		List<Board> list = new ArrayList<Board>();
		list.add(new Board("제목1", "내용1"));
		list.add(new Board("제목2", "내용2"));
		list.add(new Board("제목3", "내용3"));

		return list;
	}
}

public class Exercise7 {

	public static void main(String[] args) {

		BoardDao dao = new BoardDao();
		List<Board> list = dao.getBoardList();
		for (Board board : list) {
			System.out.println(board.getTitle() + " - " + board.getContent());
		}

	}
}

 

 

- 8번

package ch15;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

class Student {
	public int studentNum;
	public String name;

	public Student(int studentNum, String name) {
		this.studentNum = studentNum;
		this.name = name;
	}

	@Override
	public int hashCode() {
		return studentNum;
	}

	@Override
	public boolean equals(Object obj) {
		if (obj instanceof Student) {
			Student stu = (Student) obj;
			if (stu.studentNum == studentNum)
				return true;
		}
		return false;
	}
}

public class Exercise8 {

	public static void main(String[] args) {

		Set<Student> set = new HashSet<Student>();

		set.add(new Student(1, "홍길동"));
		set.add(new Student(2, "신용권"));
		set.add(new Student(1, "조민우")); // 학번이 같으므로 저장되지 않음

		Iterator<Student> iterator = set.iterator();
		while (iterator.hasNext()) {
			Student student = iterator.next();
			System.out.println(student.studentNum + " : " + student.name);
		}
	}

}

 

 

- 9번

package ch15;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

public class Exercise9 {

	public static void main(String[] args) {
		Map<String, Integer> map = new HashMap<String, Integer>();

		map.put("blue", 96);
		map.put("hong", 86);
		map.put("white", 92);

		String name = null; // 최고 점수를 받은 아이디 저장
		int maxScore = 0; // 최고 점수 저장
		int totalScore = 0; // 점수 합계 저장
		int avgScore = 0;

		Set<Map.Entry<String, Integer>> entrySet = map.entrySet();
		Iterator<Map.Entry<String, Integer>> entryIterator = entrySet.iterator();

		while (entryIterator.hasNext()) {
			Map.Entry<String, Integer> entry = entryIterator.next();
			Integer value = entry.getValue();
			totalScore += value;
			if (value > maxScore) {
				maxScore = value;
				name = entry.getKey();
			}
		}

		avgScore = totalScore / map.size();

		System.out.println("평균 점수: " + avgScore);
		System.out.println("최고 점수: " + maxScore);
		System.out.println("최고 점수를 받은 아이디: " + name);

	}

}

 

 

- 10번

package ch15;

import java.util.TreeSet;

class Student3 implements Comparable<Student3> {
	public String id;
	public int score;

	public Student3(String id, int score) {
		this.id = id;
		this.score = score;
	}

	@Override
	public int compareTo(Student3 o) {
		if (score < o.score) {
			return -1;
		} else if (score == o.score) {
			return 0;
		} else {
			return 1;
		}
	}
}

public class Exercise10 {

	public static void main(String[] args) {

		TreeSet<Student3> treeSet = new TreeSet<Student3>();
		treeSet.add(new Student3("blue", 96));
		treeSet.add(new Student3("hong", 86));
		treeSet.add(new Student3("white", 92));

		Student3 student = treeSet.last();
		System.out.println("최고점수 : " + student.score);
		System.out.println("최고점수를 받은 아이디 : " + student.id);
	}

}

 

728x90
반응형

'Java > 이것이 자바다' 카테고리의 다른 글

이자바 16장(스트림과 병렬 처리) 확인문제  (0) 2023.05.07
스트림과 병렬 처리  (0) 2023.05.07
컬렉션 프레임워크  (0) 2023.05.02
이자바 14장(람다식) 확인문제  (0) 2023.04.28
람다식  (0) 2023.04.27

댓글