자바 바 텀 은 링크 를 바탕 으로 집합 과 맵-집합 set 작업 에 대한 상세 한 설명 을 실현 합 니 다.
자바 바 텀 은 이 진 트 리 를 기반 으로 집합 과 맵 을 실현 합 니 다.에서 우 리 는 밑 층 이 이 진 트 리 를 바탕 으로 하 는 집합 을 실현 했다.본 절 은 밑 층 이 체인 표를 바탕 으로 어떻게 학습 하 는 지 에 대해 주 의 를 기울 인 다.이곳 의 체인 시 계 는 이전에 스스로 포 장 된 것 이다.
1.집합 set 관련 기능
1.1 add()의 차이
링크 자체 에 무 거 운 효 과 를 제거 하지 못 하기 때문에 우 리 는 링크 를 바탕 으로 하 는 집합 을 할 때
add()
방법 에 대해 특수 처 리 를 해 야 한다.다음 과 같이 판단 을 추가 하면 된다.
@Override
public void add(E e) {
if (!list.contains(e)) {
list.addFirst(e);
}
}
2.집합 실현2.1 인터페이스 정의 설정
/**
*
*/
public interface Set<E> {
void add(E e);// <――<
void remove(E e);//
int getSize();//
boolean isEmpty();//
boolean contains(E e);//
}
3.2 링크 를 바탕 으로 집합 실현Set
public class LinkedListSet<E> implements Set<E> {
private LinkedList<E> list;
public LinkedListSet() {
list = new LinkedList<E>();
}
@Override
public int getSize() {
return list.getSize();
}
@Override
public boolean isEmpty() {
return list.isEmpty();
}
@Override
public boolean contains(E e) {
return list.contains(e);
}
@Override
public void add(E e) {
if (!list.contains(e)) {
list.addFirst(e);
}
}
@Override
public void remove(E e) {
list.removeElement(e);
}
}
3.3 테스트:두 명작 의 어 휘 량 과 중복 되 지 않 는 어 휘 량
import java.util.ArrayList;
public class LinkedListSetTestDemo {
public static void main(String[] args) {
System.out.println("Pride and Prejudice");
// ArrayList
ArrayList<String> words1 = new ArrayList<>();
// word1
FileOperation.readFile("pride-and-prejudice.txt", words1);
System.out.println("Total words : " + words1.size());
LinkedListSet<String> set1 = new LinkedListSet<>();
// for , word words
// ArrayList words1 word
for (String word : words1)
set1.add(word);//
System.out.println("Total different words : " + set1.getSize());
System.out.println("-------------------");
System.out.println("Pride and Prejudice");
// ArrayList
ArrayList<String> words2 = new ArrayList<>();
// word1
FileOperation.readFile("a-tale-of-two-cities.txt", words2);
System.out.println("Total words : " + words2.size());
LinkedListSet<String> set2 = new LinkedListSet<>();
// for , word words
// ArrayList words1 word
for (String word : words2)
set2.add(word);//
System.out.println("Total different words : " + set2.getSize());
}
}
결과:여기 서 설명 할 필요 가 있 는 것 은 우리 가 통계 한 단어 수 는 각 단어 로 구 성 된 것 만 고려 하고 단어의 특수 한 형식 을 구분 하지 않 았 다 는 것 이다.
다음 절 에 본 절 과 관련 된 것 을 분석 할 것 이다[2 분 검색 트 리,링크 의 실현 을 바탕 으로 하 는 집합 Set 복잡 도 분석]
원본 주소 https://github.com/FelixBin/dataStructure/tree/master/src/SetPart
자바 알고리즘 과 관련 된 내용 에 관심 이 있 는 독자 들 은 본 사이트 의 주 제 를 볼 수 있 습 니 다.자바 데이터 구조 및 알고리즘 튜 토리 얼,자바 조작 DOM 노드 기술 총화,자바 파일 과 디 렉 터 리 작업 기법 집합과자바 캐 시 작업 기법 집합
본 고 에서 말 한 것 이 여러분 의 자바 프로 그래 밍 에 도움 이 되 기 를 바 랍 니 다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
JPA + QueryDSL 계층형 댓글, 대댓글 구현(2)이번엔 전편에 이어서 계층형 댓글, 대댓글을 다시 리팩토링해볼 예정이다. 이전 게시글에서는 계층형 댓글, 대댓글을 구현은 되었지만 N+1 문제가 있었다. 이번에는 그 N+1 문제를 해결해 볼 것이다. 위의 로직은 이...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.