ㅋㄷㅋㅌ
HashSet
java.util 패키지에 포함된 집합 자료 구조.
중복 비허용, 순서 유지 x, 빠른 검색, 추가, 삭제 작업 제공
해시 테이블 기반으로 동작
특징
1. 중복 불허
2. 순서 보장하지 않음
3. 빠른 검색, 삽입, 삭제: O(1) 시간 복잡도로 요소를 검색, 추가, 삭제할 수 있음
4. null 허용
5. 스레드 안전하지 않음: 멀티스레드 환경에서는 Collections.synchronizedSet() 또는 ConcurrentHashMap 사용
주요 메서드
| 메서드 | 설명 |
|---|---|
| add(E e) | 요소를 추가. 중복된 값은 추가되지 않음 |
| contains(Object o) | 특정 요소가 포함되어 있는지 확인 |
| remove(Object o) | 특정 요소를 제거 |
| size() | 현재 집합에 포함된 요소의 개수를 반환 |
| isEmpty() | 집합이 비어 있는지 확인 |
| clear() | 모든 요소를 제거하여 집합을 비움 |
| iterator() | 요소를 순회할 수 있는 Iterator를 반환 |
| addAll(Collection<\? extends E> c) | 다른 컬렉션의 모든 요소를 추가 |
| retainAll(Collection<\?> c) | 특정 컬렉션과의 교집합만 유지 |
| removeAll(Collection<\?> c) | 특정 컬렉션에 포함된 요소를 모두 제거 |
정규식 사용
class Solution {
public int solution(String[] babbling) {
int cnt = 0;
String[] validWords = {"aya", "ye", "woo", "ma"};
for(String talk: babbling) {
if(talk.matches("^(aya|ye|woo|ma)+$")) {
boolean isValid = true;
for(int i = 0; i < validWords.length; i++) {
if(talk.contains(validWords[i] + validWords[i])) {
isValid = false;
break;
}
}
if(isValid){
cnt++;
}
}
}
return cnt;
}
}
"^(aya|ye|woo|ma)+$"dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-redis'
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
}
spring:
data:
redis:
host: localhost
port: 6379
username: <username>
password: <비밀번호>
public interface ItemRepository extends CrudRepository<Item, String> {
}
@SpringBootTest
public class RedisRepositoryTest {
@Autowired
private ItemRepository itemRepository;
@Test
public void createTest() {
Item item = Item.builder()
.name("keyboard")
.description("too expensive")
.price(2300091200)
.build();
itemRepository.save(item);
}
}

테스트 성공, 데이터가 삽입되었음을 확인할 수 있다.
sets에는 생성된 id가, hash tables에는 id별로 데이터가 삽입된다.

id의 타입으로는 String으로 지정하면 자동으로 UUID를 생성해서 Id로 쓴다.
간단한 CRUD 작업은 위의 방법으로 쉽게 사용할 수 있다.
setgetdeletehasKeyValueOperations
ValueOperations는 RedisTemplate의 opsForValue() 메서드를 통해 얻을 수 있음@SpringBootTest
public class RedisTemplateTests {
@Autowired
private StringRedisTemplate stringRedisTemplate;
@Test
public void stringOpsTest() {
// ValueOperations<String, String>: String 조작을 위한 클래스
// RedisTemplate에 설정된 타입을 바탕으로 Redis 문자열 조작
ValueOperations<String, String> ops = stringRedisTemplate.opsForValue();
ops.set("key", "value");
System.out.println(ops.get("key"));
}
}
개인 과제로 MSA 프로젝트 구성을 하게 되었다.
프로젝트 구성 중 찾아본 것 정리

비교
| 특징 | Spring Cloud Gateway | Reactive Gateway |
|---|---|---|
| 용도 | API Gateway의 전체 기능 제공 | Spring Cloud Gateway의 Reactive 모델 구성 요소 |
| 의존성 | spring-cloud-starter-gateway | Spring Cloud Gateway에 포함 |
| Spring WebFlux 기반 | 포함됨 | Reactive Streams 관련 기능을 지원함 |
결론
Spring Cloud Gateway만 추가하면 Reactive Gateway 기능도 자동 포함됨