Java

[Java] 컬렉션 프레임워크

블로그 주인장 2023. 8. 7.

목차


    📢 컬렉션 프레임워크

    • 여러 데이터를 편리하게 관리할 수 있도록 만들어 놓은 것이다.
    • 자료 구조 및 알고리즘을 구조화 하는 것이다.

    📰 List 인터페이스

    • 순서가 있는 데이터의 집합
    • 데이터의 중복을 허용
    • 대표 구현 클래스 (ex. ArrayList, LinkedList, Vector)

    💻 ArrayList 인터페이스

    public class Main {
    
        public static void main(String[] args) {
        
            ArrayList list1 = new ArrayList();
            list1.add(1);
            list1.add(2);
            list1.add(3);
            System.out.println("list1 = " + list1);
            list1.remove(Integer.valueOf(2));   //데이터 2라는 정수를 삭제한다.
            System.out.println("list1 = " + list1);
            list1.add(0, 10);       //인덱스 0번에 10이라는 정수를 더한다.
            System.out.println("list1.size() = " + list1.size());			//리스트 안의 인덱스 파악
            System.out.println("list1.contains(1) = " + list1.contains(1)); //데이터 유/무
            System.out.println("list1.indexOf(10) = " + list1.indexOf(10)); //10의 인덱스 위치 파악
        }
      }

    💻 LinkedList 인터페이스

    public class Main {
    
        public static void main(String[] args) {
    
            LinkedList list2 = new LinkedList();
            list2.add(1);
            list2.add(2);
            list2.add(3);
            System.out.println("list2 = " + list2);
    
            list2.addFirst(0);	//첫번째 위치에 '0'을 삽입
            list2.addLast(4);	//마지막 위치에 '4'을 삽입
            System.out.println("list2 = " + list2);
    
            list2.remove(Integer.valueOf(1));	//리스트에서 1이라는 값을 지운다.
            System.out.println("list2 = " + list2);
    
            list2.removeFirst(); //첫번째 위치에 값을 삭제
            list2.removeLast();	 //마지막 위치에 값을 삭제
            System.out.println("list2 = " + list2);
    
            System.out.println(list2.size());	//배열 안에 있는 인덱스의 갯수 추출
          }
    }
     

    📰 Set 인터페이스

    • 순서가 없는 데이터의 집합
    • 데이터의 중복 허용 X
    • 대표 구현 클래스 (ex. HashSet, TreeSet)

    💻 HashSet 인터페이스

    public class Main {
    
        public static void main(String[] args) {
    
            HashSet set1 = new HashSet();
            set1.add(1);
            set1.add(2);
            set1.add(3);
            System.out.println("set1 = " + set1);
    
            set1.remove(1); //값으로 인식하여 해당 데이터 1을 삭제
    
            set1.add(2);
            set1.add(3);
            System.out.println("set1 = " + set1);
            System.out.println(set1.size());
            System.out.println(set1.contains(2));
    
          }
    }

    💻 TreeSet 인터페이스

    public class Main {
    
        public static void main(String[] args) {
    
            TreeSet set2 = new TreeSet();
            set2.add(1);
            set2.add(2);
            set2.add(3);
    
            System.out.println("set2 = " + set2);
            set2.remove(1); //값으로 인식하여 해당 데이터 1을 삭제
    
            set2.clear();   //모든 데이터 삭제
    
            set2.add(10);
            set2.add(20);
            set2.add(30);
            set2.add(10);
            System.out.println("set2 = " + set2);
            System.out.println(set2.first());
            System.out.println(set2.last());
            System.out.println(set2.lower(10)); //10보다 작은 수를 구한다.
            System.out.println(set2.higher(10)); //10보다 작은 수를 구한다.
    
    
          }
    }

    📰 Map 인터페이스

    • 키와 값의 쌍으로 이루어진 데이터 집합
    • 순서를 유지하지 않는다.
    • 대표 구현 클래스 (ex. HashMap, TreeMap)

    💻 HashMap 인터페이스

    public class Main {
    
        public static void main(String[] args) {
    
            HashMap map1 = new HashMap();
            map1.put(1, "kiwi");	//put으로 Key,Value를 넣는다.
            map1.put(2, "apple");
            map1.put(3, "mango");
            System.out.println("map1 = " + map1);
            
            map1.remove(2); //2번 키워드의 값을 삭제
            System.out.println("map1 = " + map1);
            System.out.println("map1.get(1) = " + map1.get(1)); //get으로 Key,Value를 얻는다.
          }
    }

    💻 TreeMap 인터페이스

    public class Main {
    
        public static void main(String[] args) {
    
            TreeMap map2 = new TreeMap();
            map2.put(1, "kiwi");
            map2.put(2, "apple");
            map2.put(3, "mango");
            System.out.println("map2 = " + map2);
    
            System.out.println(map2.firstEntry());  //첫번째 데이터
            System.out.println(map2.firstKey());    //첫번째 키값
    
            System.out.println(map2.lastEntry());  //첫번째 데이터
            System.out.println(map2.lastKey());    //첫번째 키값
    
            System.out.println(map2.lowerEntry(10)); //10보다 작은 수를 구한다.
            System.out.println(map2.higherKey(10)); //10보다 작은 수를 구한다.
    
          }
    }

    반응형

    'Java' 카테고리의 다른 글

    [Java] 클래스와 객체(1)  (0) 2023.08.08
    [Java] 파일의 입출력  (0) 2023.08.07
    [Java] 콘솔 입출력  (0) 2023.08.06

    댓글