출력예): 30, 20, 10 또는 10, 20, 30 또는 20, 10, 30등과같이출력순서는관계없다.
package collection.set.ex;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Scanner;
public class SetEx1 {
//30, 20, 20, 10, 10이 출력되면 중복을 제거하고 출력하면 된다. 출력 순서는 관계없다.
public static void main(String[] args) {
HashSet<Integer> hashSet = new HashSet<>();
Scanner scanner = new Scanner(System.in);
ArrayList<Integer> list = new ArrayList<>();
int a =1;
System.out.println("숫자를 입력(0입력시 종료 or 15개입력시 종료)");
while(a!=0) {
a = scanner.nextInt();
hashSet.add(a);
list.add(a);
}
System.out.println("입력 데이터 : "+list);
System.out.println("순서를 보장하지 않으며 \n"
+ "중복을 허용하지 않는 컬렉션프레임워크만들기");
System.out.println(hashSet);
}
}
숫자를 입력(0입력시 종료 or 15개입력시 종료)
12
12
145
50
40
40
100
100
90
0
입력 데이터 : [12, 12, 145, 50, 40, 40, 100, 100, 90]
순서를 보장하지 않으며
중복을 허용하지 않는 컬렉션프레임워크만들기
[145, 50, 100, 40, 90, 12]
문제2 - 중복 제거와 입력 순서 유지
**문제설명**
여러정수가입력된다. 여기서중복값을제거하고값을출력해라.
30, 20, 20, 10, 10이출력되면중복을제거하고출력하면된다.
단**입력순서대로출력**해라.
출력예): 30, 20, 10
정답
입력한순서대로 출력해야하니
위에 코드에서
HashSet -->LinkedHashSet
package collection.set.ex;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Scanner;
public class SetEx2 {
//30, 20, 20, 10, 10이 출력되면 중복을 제거하고 출력하면 된다. 출력 순서는 관계없다.
public static void main(String[] args) {
HashSet<Integer> hashSet = new LinkedHashSet<>();
Scanner scanner = new Scanner(System.in);
ArrayList<Integer> list = new ArrayList<>();
int a =0;
System.out.println("숫자를 입력(0입력시 종료 or 15개입력시 종료)");
while(true) {
a = scanner.nextInt();
if(a==0) {
break;
}
else {
hashSet.add(a);
list.add(a);}
}
System.out.println("입력 데이터 : "+list);
System.out.println("순서를 보장하고 \n"
+ "중복을 허용하지 않는 컬렉션프레임워크만들기");
System.out.println(hashSet);
for (Integer i : hashSet) {
System.out.print(i+" ");
}
}
}
숫자를 입력(0입력시 종료 or 15개입력시 종료)
30
20
10
10
40
40
50
9
8
0
입력 데이터 : [30, 20, 10, 10, 40, 40, 50, 9, 8]
순서를 보장하고
중복을 허용하지 않는 컬렉션프레임워크만들기
[30, 20, 10, 40, 50, 9, 8]
30 20 10 40 50 9 8
문제와풀이2
문제4 - 합집합, 교집합, 차집합
**문제설명**
두숫자의집합이제공된다.
집합1: `1, 2, 3, 4, 5`
집합2: `3, 4, 5, 6, 7`
두집합의합집합, 교집합, 차집합을구해라. 출력순서는관계없다.
합집합: 두집합의합이다. 참고로중복은제거한다. `[1, 2, 3, 4, 5, 6, 7]`
교집합: 두집합의공통값이다. 참고로중복은제거한다. `[3, 4, 5]`
차집합: 집합1에서집합2와같은값을뺀나머지`[1, 2]`
다음실행결과를참고하자.
`Set`인터페이스의주요메서드를참고하자.
package javaBasic2.collection.set.ex;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class Ex3 {
public static void main(String[] args) {
Set<Integer> set1 = new HashSet<>(List.of(1, 2, 3, 4, 5));
Set<Integer> set2 = new HashSet<>(List.of(3, 4, 5, 6, 7));
Set<Integer>union = new HashSet<Integer>(set1);
union.addAll(set2);
Set<Integer> intercection = new HashSet<Integer>(set1);
intercection.retainAll(set2);
Set<Integer> diffrence = new HashSet<Integer>(set1);
diffrence.removeAll(set2);
System.out.println("합집합(addAll 사용) : "+union);
System.out.println("교집합(retainAll 사용) : "+intercection);
System.out.println("차집합(removeAll 사용) : "+diffrence);
}
}
합집합 : [1, 2, 3, 4, 5, 6, 7]
교집합 : [3, 4, 5]
차집합 : [1, 2]
문제5 - Equals, HashCode 문제 설명 RectangleTest , 실행 결과를 참고해서 다음 Rectangle 클래스를 완성하자. Rectangle 클래스는 width , height 가 모두 같으면 같은 값으로 정의한다
package javaBasic2.collection.set.ex;
import java.util.Objects;
public class Rectangle {
private int width;
private int height;
public Rectangle(int width ,int height) {
this.width = width;
this.height = height;
}
@Override
public String toString() {
return "Rectangle [width=" + width + ", height=" + height + "]";
}
@Override
public int hashCode() {
return Objects.hash(height, width);
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Rectangle other = (Rectangle) obj;
return height == other.height && width == other.width;
}
}
package javaBasic2.collection.set.ex;
import java.util.HashSet;
import java.util.Set;
public class RectangleMain {
public static void main(String[] args) {
Set<Rectangle> set = new HashSet<Rectangle>();
set.add(new Rectangle(10, 20));
set.add(new Rectangle(10, 20)); //똑같은것이라 판단하고 셋에 넣지않음!
set.add(new Rectangle(20, 20));
System.out.println("set : "+set+"\n");
for (Rectangle r : set) {
System.out.println(r+" ");
}
}
}
set : [Rectangle [width=20, height=20], Rectangle [width=10, height=20]]