Java - Collections

원종서·2023년 3월 6일
0

java

목록 보기
7/9

List Collection

ArrayList, Vector, LinkedList

Vector 는 ArrayList 와 비슷한데, 동기화에 적합함.

  1. boolean add(E e)
  2. void add(int index, E e)
    3 set(int index, E e)
  3. boolean contains(Object o)
  4. E get(int index)
  5. boolean isEmpty()
  6. int size
  7. void clean
  8. E remove(int index)
  9. boolean remove(Object o)

Set Collecion

HashSet, LinkdedHashSet, TreeSet

boolean add(E e)
boolean contains(Object o)
isEmpty()
Iterator iterator()
int size
void clear();
boolean remove(Object o)

  // Set Collecion 객체 가져오기.
  
  Set<E> set = new Set<>();
  //1 
    
  for(E e : set) {
  	...
  }
  
  //2 
  Iterator<E> iterator = set.iterator();
  
  Method of Iterator
  // boolean hasNext();
  // E next();
  // void remove();
  
  while(iterator.hasNext()){
  	E e = iterator.next();
  }
  
  

Map Collection

HashMap, Hashtable, LinkedHashMap, Properties, TreeMap

V put(K key, V value)
boolean containsKey(Object key)
boolean containsValue(Object value)
Set<Map.entry<K,V>> entrySet();
V get(Object key)
boolean isEmpty()
Set keySet()
int size
Collection values()
void clear()
V remove(Object Key)


public static void main(String[] args){
	Map<String, Integer> map = new HashMap<>();

	map.put("a",1);
	map.put("B", 2);
	map.put("C",3);
	map.put("D",4);

	String key= "A";
	int value = map.get(key);
	
	Set<String> ketSet = map.keySet();
	Iterator<String> keyIterator = ketSet.iterator();
  
	while(keyIterator.hasNext()){
	 String k = keyIterator.next();
    Integer value = map.get(k);
	}

  Set<Entry<String,Integer>> entrySet = map.entrySet();

  Iterator<Entry<String,Integer> entryInterator = entrySet.iterator();
  
	while(entryInterator.hasNext()){	
		Entry<String,Integer> entry = entryInterator.next();
		String key = entry.getKey();
		Integer value = entry.getValue();
		
	}

	map.remove("D");

}

TreeSet

검색 기능을 강화시킨 TreeSet and TreeMap

E first // 제일 낮은 객체 리턴
E last() 제일 높은 객체 리턴
E lower(E e) 주어진 객채보다 바로 아래 객체 리턴
E higher(E e)
E floor(E e) 주어진 객체와 동등한 객체가 있으면 리턴 , 없으면 lower
E ceiling(E e)
E pollFirst() 제일 낮은 객체를 꺼내오고 컬렉션에서 제거
E pollLast()
Iterator descendingIterator
NavigableSet descendingSet()
NavigableSet headSet(E toElement, boolean inclusive) 주어진 객체보다 낮은객체들을 리턴, 주어진 객체 포함 여부는 두번째 매개값에 따라 달라짐
NavigableSet tailSet(E e, boolean i) // 주어진 객체보다 높은 객체들
NavigableSet subSet(E fromElement, boolean fromInclusive, E toElement, boolean toInclusive)

Comparable and Comparator

TreeSet 과 TreeMap 에 저장되는 키 객체는 저장과 동시에 오름차순으로 정렬되는데,
타입 파라미터가 Comparable 인터페이스ㅡㄹ 구현하고 있어야함.

// Example

public class Person implements Comparable<Person>{
	public String name;
	public int age;
	 
	// constructor

	@Override
	public int compareTo(Person o){	
		return age == o.age ? 0 : age > 0.age ? 1 : -1;
	}
}


public static void main(String[] args){
	TreeSet<Person> treeSet = new TreeSet<>();
	
	treeSet.add(new Person("A", 45));
	treeSet.add(new Person("B" , 55));
	treeSet.add(new Person("C", 66));
}

// Comparable 을 구현 하지 않은 비구현객체를 TreeSet or TreeMap 과 사용하고 싶다면
비교자 (Comparator 인터페이스를 구현한 객체) 를 사용하면 된다.

public class Fruit(){	
	public String name;
	public int price;
	
	public Fruit(String name, int price){
		...//
	}
}

public class FruitComparator implements Comparator<Fruit> {
	@Override
	public int compare(Fruit o1 , Fruit o2){
		return o1.price == o2.price ? 0 : o1.price < o2.price ? -1 : 1;
	}
}

불변 컬랙션 생성방법

                                                         ```java
                                                         	List<String> immutableList1 = List.of("A","B", "C");

// Set.of("A");
// Map.of(1,"B", 2,"C");

List list = new ArrayList<>();
list.add("1");
list.add("2");
list.add("3");

List immutableList2= List.copyOf(list);

String[] arrr = {"A","B"};

List immutableList3 = Arrays.asList(arrr);

                                                         ```
                                                         

0개의 댓글