ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 컬렉션 예시들
    언어/JAVA 2023. 7. 9. 22:49

    1. Thread - Vector, HashTable, HashMap

     

    Vector  

    package Collection;
    
    import java.util.List;
    import java.util.Vector;
    
    public class Vector1 {
    
        public static void main (String[] args){
    
            List<Board> list = new Vector<>();
            Thread th1 = new Thread(new BoardTask(list));
            Thread th2 = new Thread(new BoardTask2(list));
    
            th1.start();
            th2.start();
    
            try{
                th1.join();
                th2.join();
            }catch (Exception e){}
    
    
            int size = list.size();
            System.out.println("총 객체 수:" +size);
            System.out.println();
        }
    
    }
    
    class Board {
        String title;
        String content;
        String Auth;
    
        Board(String title, String content, String Auth){
            this.title = title;
            this.content = content;
            this.Auth = Auth;
        }
    }
    
    class BoardTask implements Runnable{
        private List<Board> list;
        BoardTask(List<Board> list){
            this.list = list;
        }
        @Override
        public void run() {
            if(this.list != null)
                for(int i=1; i<=1000;i++)
                    list.add(new Board("제목"+i,"내용"+i,"글쓴이"+i));
        }
    }
    
    class BoardTask2 implements Runnable{
        private List<Board> list;
        BoardTask2(List<Board> list){
            this.list = list;
        }
        @Override
        public void run() {
            if(this.list != null)
                for(int i=1001; i<=2000;i++)
                    list.add(new Board("제목"+i,"내용"+i,"글쓴이"+i));
        }
    }

     

    HashTable

    public class HashTable1 {
        public static void main(String[] args){
    
            Map<String, Integer> map = new Hashtable<>();
    
            Thread threadA = new Thread(){
                @Override
                public void run(){
                  for(int i=1; i<=1000;i++)
                      map.put(String.valueOf(i),i);
                }
            };
    
            Thread threadB = new Thread(){
                @Override
                public void run(){
                    for(int i=1001; i<=2000;i++)
                        map.put(String.valueOf(i),i);
                }
            };
    
            threadA.start();
            threadB.start();
    
            try{
                threadA.join();
                threadB.join();
            }catch (Exception e){}
    
            int size = map.size();
            System.out.println("총 엔트리 수:"+size);
            System.out.println();
    
        }
    
    }

     

     

     

     

    2. Hash set

    public class HashSet1 {
        public static void main(String[] args){
    
            Set<String> set = new HashSet<>();
    
            set.add("Java");
            set.add("JDBC");
            set.add("JSP");
            set.add("Java");
            set.add("Spring");
    
            int size = set.size();
            System.out.println(size);
        }
    
    }

     

    3 Hashmap

    public class HashMapEx {
    
        public static void main(String[] args){
    
            Map<String,Integer> map = new HashMap<>();
    
            //객체 저장
            map.put("신용권",85);
            map.put("홍길동",95);
            map.put("동장군",87);
            map.put("홍길동",85);
    
            //키로 값 얻기
            String key = "홍길동";
            int value = map.get(key);
            System.out.println(key +" : "+value);
            System.out.println();
    
            //키 Set 컬렉션 얻고, 키와 값 얻기
            Set<String> keySet = map.keySet();
            Iterator<String> keyIterator = keySet.iterator();
    
            while(keyIterator.hasNext()){
              String k = keyIterator.next();
              Integer v = map.get(k);
              System.out.println(k + ":" + v);
            }
            System.out.println();
    
            //엔트리 set 컬렉션을 얻고, 반복헤서 키와 값 얻기
    
            Set<Map.Entry<String,Integer>> entrySet = map.entrySet();
            Iterator<Map.Entry<String,Integer>> entryIterator = entrySet.iterator();
            while (entryIterator.hasNext()){
                Map.Entry<String,Integer> ent= entryIterator.next();
                String k = ent.getKey();
                Integer v = ent.getValue();
                System.out.println(k + " : "+ v);
            }
            System.out.println();
    
            //키로 엔트리 삭제
            map.remove("홍길동");
            System.out.println("총 Entey 수:" + map.size());
            System.out.println();
            
    
        }
    
    }

     

    4 properties

    public class PropertiesEx {
        public static void main(String[] args) {
            Properties pro = new Properties();
    
            try {
                pro.load(PropertiesEx.class.getResourceAsStream("Properties.properties"));
            } catch (IOException ex) {
                throw new RuntimeException(ex);
            }
    
            //주어진 키에 대한 값 읽기
            String driver = pro.getProperty("diver");
            String url = pro.getProperty("url");
            String username = pro.getProperty("username");
            String password = pro.getProperty("password");
            String admin = pro.getProperty("admin");
    
            //값 출력
            System.out.println("driver: "+ driver);
            System.out.println("url :" + url);
            System.out.println("usrname : "+username);
            System.out.println("password : "+password);
            System.out.println("admin : "+admin);
    
        }
    
    }
    driver=oracle.jdbc.OracleDriver
    url=jdbc:oravle:thin:@localhost:1521:orcl
    username=scott
    password=tiger
    admin=\uD64D\uAe38\uB3D9

     

    5.Treeset

    public class TreeSetEx {
    
        public static void main(String[] args){
         TreeSet<Integer> scores = new TreeSet<>();
    
         //Integer 객체 저장
            scores.add(87);
            scores.add(98);
            scores.add(75);
            scores.add(95);
            scores.add(80);
    
          //정렬된 Integer 객체를 하나씩 가져오기
            for(Integer s : scores){
                System.out.println(s +" ");
            }
            System.out.println("\n");
    
            //특정 Integer 객체 가져오기
            System.out.println("가장 낮은 점수:" + scores.first());
            System.out.println("가장 높은 점수:" + scores.last());
            System.out.println("95점 아래:" + scores.lower(95));
            System.out.println("95점 위:" + scores.higher(95));
            System.out.println("95점이거나 바로 아래 점수:" + scores.floor(95));
            System.out.println("85점이거나 바로 위의 점수:" + scores.ceiling(85)+"\n");
    
            //내림차순 정렬하기
            NavigableSet<Integer> descendingScore = scores.descendingSet();
            for(Integer s: descendingScore){
                System.out.println(s +" ");
            }
            System.out.println("\n");
    
            //범위검색 (80 <=)
            NavigableSet<Integer>rangeSet = scores.tailSet(80,true);
            for(Integer s : rangeSet){
                System.out.println(s +" ");
            }
            System.out.println("\n");
    
            //범위 검색(80 <= score < 90)
            rangeSet = scores.subSet(80,true,90,false);
            for(Integer s : rangeSet){
                System.out.println(s +" ");
            }
        }
    }

     

    6. TreeMap

     

    public class TreeMapEx {
        public static void main(String[] args){
    
            TreeMap<String,Integer> treeMap = new TreeMap<>();
    
            //엔트리 저장
            treeMap.put("apple",10);
            treeMap.put("forever",60);
            treeMap.put("description",40);
            treeMap.put("ever",50);
            treeMap.put("zoo",80);
            treeMap.put("base",20);
            treeMap.put("guess",70);
            treeMap.put("cherry",30);
    
            //정렬된 엔트리 하나씩 가져오기
            Set<Map.Entry<String,Integer>> entrySet = treeMap.entrySet();
            for(Map.Entry<String,Integer> entry : entrySet){
                System.out.println(entry.getKey() + "-" + entry.getValue());
            }
            System.out.println();
    
            //특정 키에 대한 값 가져오기
            Map.Entry<String,Integer> entry = null;
            entry = treeMap.firstEntry();
            System.out.println("제일 앞 단어" + entry.getKey() + "-" + entry.getValue());
            entry = treeMap.lastEntry();
            System.out.println("제일 뒤 단어" + entry.getKey() + "-" + entry.getValue());
            entry = treeMap.lowerEntry("ever");
            System.out.println("ever 앞 단어" + entry.getKey() + "-" + entry.getValue()+"\n");
    
    
            //내림차순으로 정렬하기
            NavigableMap<String,Integer> descendingMap = treeMap.descendingMap();
            Set<Map.Entry<String,Integer>> descendingEntrySet = descendingMap.entrySet();
            for(Map.Entry<String,Integer> e : descendingEntrySet){
                System.out.println(e.getKey() +"-" + e.getValue());
            }
            System.out.println();
    
            //범위 검색
            System.out.println("[c~h 사이의 단어 검색하기]");
            NavigableMap<String,Integer> rangeMap = treeMap.subMap("c",true,"h",false);
            for(Map.Entry<String,Integer> e : rangeMap.entrySet()){
                System.out.println(e.getKey() +" - " + e.getValue());
            }
    
        }
    
    }

     

    '언어 > JAVA' 카테고리의 다른 글

    - JavaNextStep - 숫자야구게임(1) - 단위테스트  (0) 2023.07.10
    -JavaNextStep (1) - Junit  (0) 2023.07.10
    컬렉션  (0) 2023.07.09
    어노테이션  (0) 2023.07.08
    중첩 선언과 익명 객체  (0) 2023.07.07
Designed by Tistory.