자바 바 텀 은 링크 를 바탕 으로 집합 과 맵-집합 set 작업 에 대한 상세 한 설명 을 실현 합 니 다.

본 논문 의 사례 는 자바 바 텀 이 링크 를 바탕 으로 집합 과 맵-집합 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 노드 기술 총화,자바 파일 과 디 렉 터 리 작업 기법 집합자바 캐 시 작업 기법 집합
본 고 에서 말 한 것 이 여러분 의 자바 프로 그래 밍 에 도움 이 되 기 를 바 랍 니 다.

좋은 웹페이지 즐겨찾기