메뉴 건너뛰기

조회 수 1486 추천 수 0 댓글 0
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄
import java.io.*;







// Collection.sort를 사용하기 위해서 필요하다.  

class MyComp implements Comparator<result> {
     public int compare(result o1, result o2) {
          if (o1.a > o2.b) return -1;
          else if (o1.a < o2.b) return 1;
          else return 0;
     }
}




class result{
     int a; 
     int b;
}




public class Test_java {
     public static void main(String[] ar) {
          File a = new File("./aaa.txt");
          Scanner sc = null;
          try {
               sc = new Scanner(a);
          }catch(IOException e) {
               System.err.println(e);
               System.out.println("파일 스캔 에러");
               System.exit(1);
            }
  
  ArrayList<String> al = new ArrayList<String>();
  ArrayList<result> a2 = new ArrayList<result>();
  
  while(sc.hasNext()) {
       al.add(sc.nextLine());
   
       result re = new result(); 
       re.a = 1; 
       re.b = 2;
 

       a2.add(re);
   
  }
    
  Collections.sort(al);
  Collections.sort(a2, new MyComp());
  
  
  // TreeMap 입력 및 출력 부분. 
  TreeMap<String, Integer> tm = new TreeMap<String, Integer>();
  
  for(int i = 0; i < al.size(); i++) {
   tm.put(al.get(i), 1); 
  }
  
  // TreeMap index값 가져오기.
  Set cla = tm.keySet();
  String[] strArr = new String[cla.size()];
  strArr = (String[]) cla.toArray(strArr);
  
  for(int i = 0 ; i < strArr.length ; i++){
       System.out.println("TreeMap index = " + strArr[i]);
  }
  
  // 파일 출력부분 입니다.
  PrintWriter pw = null;
  try {
   pw = new PrintWriter(new BufferedWriter(new FileWriter(new File("./bbb.txt"))));
  }catch(IOException e) {
       System.out.println("파일 쓰기 에러");
       System.exit(1);
  }
  
  for(int i = 0; i < al.size(); i++) 
   pw.println(al.get(i));
  
  pw.close();
    }
}


TreeMap의 경우, 자동으로 key 값을 기준으로 정렬이 된다. 그러나, key 값이 아닌 value 값으로 정렬을 하고자 한다면, TreeMap의 값들을 ArrayList에 저장후, Collections.sort를 이용하면 된다. 이것은 HashMap의 경우도 동일하다. 


List of Articles
번호 제목 날짜 조회 수
71 enum 2016.09.13 3404
70 문자 기반 스트림 2016.09.13 3440
69 쓰레드의 우선순위 2016.09.13 3503
68 파일I/O 개요 file 2016.09.13 3507
67 Jadclipse 플러그인 설치 file 2016.09.19 3512
66 인터페이스와 다형성 2016.09.13 3535
65 File 클래스 file 2016.09.13 3603
64 원하는 패턴의 날짜 구하기 : JAVA 2016.12.09 3608
63 새로 만든 모듈의 iBatis 쿼리를 새로운 xml 파일에 만들고 싶다면 이렇게 하자. file 2016.08.29 3650
62 set get 파라미터 2016.08.18 3658
61 컬렉션 프레임워크 file 2016.09.13 3705
60 JAR 파일 2016.09.19 3713
59 BigDecimal타입의 사칙연산 2016.12.22 3713
58 클래스 메서드와 인스턴스 메서드 2016.09.13 3720
57 [자바(스프링&mybatis&jsp) 프로젝트 & 아파치 &톰켓 연동]3. 이클립스 설치 file 2016.08.18 3756
56 바이트 기반의 스트림 file 2016.09.13 3763
55 static멤버와 관련된 예제 2016.09.21 3768
54 새로 만든 모듈의 iBatis 쿼리를 새로운 xml 파일에 만들고 싶다면 이렇게 하자. file 2016.08.29 3773
53 [자바(스프링&mybatis&jsp) 프로젝트 & 아파치 &톰켓 연동 ] 이클립스 프로젝트 생성 순서07.commons-collection 설치 file 2016.08.18 3845
52 Select statements cannot be executed as an update. 에러 해결방법 file 2016.08.29 3846
Board Pagination Prev 1 2 3 4 5 6 7 8 Next
/ 8

하단 정보를 입력할 수 있습니다

© k2s0o1d4e0s2i1g5n. All Rights Reserved