관리 메뉴

Frog is cry

Set 본문

JAVA/복습

Set

Frog is cry 2020. 7. 29. 19:38
package Set;

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

public class SetTest {
	public static void main(String[] args) {
		Set<String> set = new HashSet<>();
		set.add("A");
		set.add("O");
		set.add("AB");
		set.add("A");
		set.add("B");
		set.add("A");
		set.add("A");
		set.add("A");
		set.add("A");
		set.add("A");

		
		System.out.println("총 수 : " + set.size());
		System.out.println(set);
		//반복자 : Iterator : 반복해서 값을 순서대로 뽑을떄 사용한다.
		Iterator<String> iter = set.iterator();
		while(iter.hasNext()) {
			System.out.println(iter.next());
		}
		set.remove("AB");
		System.out.println(set.size());
		iter = set.iterator();
		
		while(iter.hasNext()) {
			System.out.println(iter.next());
		}
	}
	
}
















package Set;

import java.time.Duration;
import java.time.Instant;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.IntStream;

public class WhySet {
	public static void main(String[] args) {
		final int SIZE = 10_000_000;
		final List<Integer> arrayList = new ArrayList<>(SIZE);
		final Set<Integer> hashSet = new HashSet<>(SIZE);
		final int data = 9_000_000;
		
		//순차 병렬 집계연산
		IntStream.range(0, SIZE).forEach(value -> {
			arrayList.add(value);
			hashSet.add(value);
		});
		
		//끝시간 - 시작시간 = 걸린시간;
		Instant start = Instant.now();
		arrayList.contains(data);
		Instant end = Instant.now();
		long eTime = Duration.between(start, end).toMillis();
		System.out.println("array list search tie : " + eTime + "초");
		
		start = Instant.now();
		hashSet.contains(data);
		end = Instant.now();
		eTime = Duration.between(start, end).toMillis();
		System.out.println("hash set array list search tie : " + eTime + "초");
		
	}
}

'JAVA > 복습' 카테고리의 다른 글

컬렉션 프레임워크 (Hash)  (0) 2020.07.29
컬렉션 프레임워크(Array List)  (0) 2020.07.29
제네릭  (0) 2020.07.29
오브젝트  (0) 2020.07.29
Wrapper 클래스  (0) 2020.07.29
Comments