miin29na

[알고리즘] Java Collection (TODO) 본문

IT_Algorithm/algorithm

[알고리즘] Java Collection (TODO)

miin29na 2020. 6. 13. 15:24

Java Collection

- Java Collection

  List
  순서 존재 / 데이터 중복 허용
Index 사용
  ArrayList LinkedList Stack
성능      
정렬      
선언      
초기화 new ArrayList<Integer>(Arrays.asList(1,2,3));    
  codevang.tistory.com/129?category=827592    

 

  Map
  Key,Value / 순서X / Key중복 X
  HashMap TreeMap HashTable
성능 hashMap > treeMap 트리구조 만들어 저장
범위검색, 정렬 시에는 HashMap 보다 좋음
HashMap 구버전
정렬 가장 처음 넣은 데이터가 index=0 key 값 자동 정렬  
선언 HashMap map = new HashMap() Map tm = new TreeMap<>(map);  
초기화      
  // 키가 존재한다면 찾는 키의 값을 반환하고, 
없다면 0을 반환
map.put(key, map.g
etOrDefault(key, 0)) + 1);;
//내림차순 정렬
tm.descendingMap()
 
    codevang.tistory.com/134  

 

  Set
  순서 X / 중복X
  HashSet TreeSet
성능    
정렬    
선언    
초기화    
  List<Integer> a = new ArrayList<>();
HashSet<Integer> map = new HashSet<Integer>(a);
 

example

 

-String 역순 출력

public class ReverseBuffer {

   public static void main(String []args) {

      StringBuffer strBuffer = new StringBuffer(); 

      String str = "Reverse this strings";

      strBuffer.append(str);

      // 버퍼안에 reverse()를 이용해 거꾸로 출력

      System.out.print(strBuffer.reverse());

   }

}

Colored by Color Scripter

-String 자르기

//사용법

String.substring(start) //문자열  start위치부터 끝까지 문자열 자르기

String.substring(start,end) //문자열  start위치 부터 end전까지 문자열 발췌

        

 

//예제

String str = "ABCDEFG"//대상 문자열

/*A=0 B=1 C=2 D=3 E=4 F=5 G=6의 index를 가진다.*/

        

str.substring(3); 

/*substring(시작위치) 결과값 = DEFG*/

 

str.substring(36); 

/*substring(시작위치,끝위치) 결과값 = DEF*/

'IT_Algorithm > algorithm' 카테고리의 다른 글

[알고리즘15. 3Sum  (0) 2020.06.17
[알고리즘]backtracking(TODO)  (0) 2020.06.14
[알고리즘] Tree, DFS, BFS (TODO)  (0) 2020.06.13
Comments