인천일보아카데미/- 학습일지
[학습일지]JAVA교육일지 39일차
w1z
2022. 6. 15. 14:27
1. HashSet
1. HashSet
[정의]
- Set인터페이스를 구현한 가장 대표적인 컬렉션
- 순서가 없는 배열이다. --> 방 번호가 존재하지않으며, 첨자(index)를 사용하지 않는다.
- 요소가 중복값을 가질 수 없다.
[ArrayList와 HashSet의 비교1]
ArrayList<String> list = new ArrayList<String>();
HashSet<String> set = new HashSet<String>();
//ArrayList & HashSet 데이터 추가
list.add("사과");
list.add("딸기");
list.add("바나나");
list.add("딸기");
set.add("사과");
set.add("딸기");
set.add("바나나");
set.add("딸기");
System.out.println("ArrayList & HashSet 개수");
System.out.println(list.size());
System.out.println(set.size());
//출력용X... -> 개발자 확인용
System.out.println("ArrayList & HashSet 덤프");
System.out.println(list);
System.out.println(set);
System.out.println("ArrayList & HashSet 요소 접근/탐색");
for (String item : list) {
System.out.println(item);
}
set.iterator();
Iterator<String> iter = set.iterator();
while (iter.hasNext()) {
System.out.println(iter.next());
}
//OUTPUT
ArrayList & HashSet 개수
4
3
ArrayList & HashSet 덤프
[사과, 딸기, 바나나, 딸기]
[사과, 바나나, 딸기]
ArrayList & HashSet 요소 접근/탐색
사과
딸기
바나나
딸기
사과
바나나
딸기
----> ArrayList는 각 방 마다 중복 값("딸기")이 포함해서 들어가 있다.
----> HashSet은 중복값("딸기")은 포함하지 않는다.
[ArrayList와 HashSet의 비교2 - 중복값 없는 로또 번호 만들기]
Random rnd = new Random();
//List
ArrayList<Integer> lotto = new ArrayList<Integer>();
for (int i=0; i<6; i++) {
int n = rnd.nextInt(45) + 1;
boolean flag = false;
//같은 숫자 검사
for (int j=0; j<i; j++) {
if (n == lotto.get(j)) {
flag = true;
break;
}
}
if (flag) i--;
else lotto.add(n);
}
Collections.sort(lotto);
System.out.println(lotto);
//Set
HashSet<Integer> lotto2 = new HashSet<Integer>();
while (lotto2.size() < 6) {
int n = rnd.nextInt(45) + 1;
lotto2.add(n);
}
System.out.println(lotto);
//OUTPUT
[3, 6, 24, 42, 44, 45]
[32, 17, 4, 20, 13, 31]
----> ArrayList는 같은숫자를 조건문을 통해 걸러주지 않으면 중복값이 나올 수 있다.
----> HashSet은 중복값이 안되므로, 코드가 List와 비교해서 간결하다.
[HashSet 중복값 허용 예시1]
class Keyboard {
private String model;
private int price;
public Keyboard(String model, int price) {
this.model = model;
this.price = price;
}
@Override
public String toString() {
return "[model=" + model + ", price=" + price + "]";
}
}
HashSet<Integer> set1 = new HashSet<Integer>();
set1.add(10);
set1.add(20);
set1.add(10); //중복값 허용X
System.out.println(set1);
HashSet<Keyboard> set2 = new HashSet<Keyboard>();
set2.add(new Keyboard("K810", 200000));
set2.add(new Keyboard("K310", 50000));
set2.add(new Keyboard("K810", 200000));
System.out.println(set2);
//OUTPUT
[10, 20]
[model=K810, price=200000], [model=K310, price=50000], [model=K810, price=200000]
----> set2에서의 첫번째값과, 세번째값은 똑같은 값이 아니다!
----> 모든 참조형(객체)는 아무리 상태(멤버 변수의 값)가 동일해도, 다른 객체로 취급한다. (쌍둥이 개념)
[HashSet 중복값 허용 예시2 - hashCode+equals Override]
class Keyboard {
private String model;
private int price;
public Keyboard(String model, int price) {
this.model = model;
this.price = price;
}
@Override
public String toString() {
return "[model=" + model + ", price=" + price + "]";
}
//hashCode 오버라이딩 + equals 오버라이딩
@Override
public int hashCode() {
//본인의 상태에 따라 달라지는 값을 반환하도록 재정의
//k1: "K810", 200000 -> "K810200000"
//k2: "K810", 200000 -> "K810200000" -> 100
//k4: "K990", 350000 -> "K990350000" -> 200
return (this.model + this.price).hashCode();
}
@Override
public boolean equals(Object obj) {
return this.hashCode() == obj.hashCode();
}
}
Keyboard k1 = new Keyboard("K810", 200000);
Keyboard k2 = new Keyboard("K810", 200000);
Keyboard k3 = k1;
Keyboard k4 = new Keyboard("K990", 350000);
System.out.println("===참조형 객체의 비교===");
System.out.println(k1 == k2);
System.out.println(k1.equals(k2));
System.out.println(k1.equals(k3));
System.out.println("===참조형 객체의 hashCode===");
System.out.println(k1.hashCode()); //hashCode: 객체의 위치(메모리 주소값)
System.out.println(k2.hashCode());
System.out.println(k4.hashCode());
//hashCode + euqals :오버라이드(재정의)
System.out.println(k1 == k2);
System.out.println(k1.equals(k2));
System.out.println(k1.equals(k4));
//OUTPUT
===참조형 객체의 비교===
false
true
true
===참조형 객체의 hashCode===
634282602
634282602
-82344313
false
true
false
----> 문자열(참조형)은 불변이다. 모든 참조형 변수의 비교는 주소값을 비교한다.
----> "equals":OK / "==":NO
----> hashCode에서의 객체 중복을 허용 하려면, hashCode와 equals 메소드를 오버라이드 해줘야 한다.
MEMO >
# 나중에 면접에서 List와 Set의 차이를 물어볼 수 있으니 핵심 키워드와 차이점을 명확히 알자!
--> List는 순서가 있는 배열이며, 요소가 중복값을 가질 수 있다!
--> Set은 순서가 없는 배열이며, 요소가 중복값을 가질 수 없다!
# 컬렉션에서의 덤프(toString) 사용은 절대 출력용으로 사용하지 않는다.
--> why? 개발자만 확인 할 수 있는 용도이다.
# 문자열(참조형) 비교에서 "==" 사용금지!! "equals()" 무조건 사용!!
JS
데이터 변수선언 총 4번 (변수선언을 잘해야됨 이게 헷갈림!!!주의주의)
외워야될 코딩구조 적음
공식구조
AJAX 코딩
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>AJAX </title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
</head>
<body>
<!--http://sample.bmaster.kro.kr/-->
<script>
const url='http://sample.bmaster.kro.kr/contacts?pageno=1&pagesize=10';
//데이터받아올 사이트 링크마저 const로 걸어준다 ;;; 지겨운 const
window.onload=function() {
$.ajax(url).done(
function(response){
//response 라는 변수로 데이터를 받아오는 함수 선언 *내가받아오는 데이터 변수이름: response
const contacts = response.contacts;
//response안의 연락처(받아올 데이터) 를 임의변수로 저장한다. *내가받아오는 데이터 변수이름: contacts
const $target = $('#target');
//바디안의 아이디 #target 변수 지정 ; 데이터를 표시할 영역을 변수로 지정한다.
for(c of contacts) {
//받아올 데이터를 (연락처) 하나씩 돌려서 c라는 변수로 집어넣기 *내가받아오는 데이터 변수이름: c
const str = `<p>이름: ${c.name}, 주소: ${c.address}, 연락처: ${c.tel}</p>`;
//받아올 데이터안에 이름/주소/전화번호 가져와서 p태그로 만들고 str이라는 변수에 저장때림 *내가받아오는 데이터 변수이름: str
$(str).appendTo($target);
//아까 저장한 변수를 아까아까 저장한 #target (line:18 참고) 안에 집어넣기
}
}
); //done 괄호 ※done 괄호 주의
}
</script>
<table>
<div id="target">
</div>
</body>
</html>
JSON 코딩 (마지막에 외우기 지금은 안중요함;)
<script>
const obj = {ireum: '홍길동', nai:20};
//객체를 JSON 문자열로 변환
const json = JSON.stringify(obj);
console.log(json);
//json 문자열을 js객체로 변환
const result = JSON.parse(json);
console.log(result);
</script>
JS & JAVA 로 html 만들고 데이터추출하고 함수만들고 집어넣기
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>객체안의 값 부분출력 / 다시구현하기 </title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
</head>
<body>
<script>
//점수를 ul안에 출력하는 함수를 만드시오
window.onload=function(){
const ar=[
{ireum:'홍길동', jumsu:80}, {ireum:'전우치',jumsu:75}
];
const $list=$('#list');
const $div=$('#target');
let sum=0;
function printAr(arr){
for(const ob of arr){
$( `<li>${ob.ireum}의 점수는 ${ob.jumsu}점</li>`).appendTo($list);
sum+=ob.jumsu;
}
$( `<p>점수합계: ${sum}점</p>`).appendTo($div);
}
// $(`<li>$(ob.eng)</li>`).appendTo($list).css('color','red');
// $(`<li>$(ob.eng)</li>`).appendTo($list);
printAr(ar);
}
//다시구현해보기
//변수선언 위치 파악을 잘모르겠음 여러번 찍어봐야됨 변수선언 위치 다시 파악하기가 제일 중요함 !
</script>
<ul>
<li id="list"></li>
</ul>
<div id="target"></div>
</body>
</html>