삽질하기/JAVA2010.07.16 11:03
public class UserObject implements Comparable<UserObject> {
	
	private int priority;
	
	public UserObject(int priority) {
		this.priority = priority;
	}
	
	public int compareTo(UserObject o) {
		if(this.priority < o.priority) {
			return -1;
		} else if (this.priority > o.priority) {
			return 1;
		} else {
			return 0;
		}
	}
	
	public String toString() {
		return "priority : " + this.priority;
	}
}
public class CompareTest {
	public static void main(String[] args) {
		UserObject[] arr = {
			new UserObject(5),
			new UserObject(2),
			new UserObject(4),
			new UserObject(6),
			new UserObject(1),
			new UserObject(7),
			new UserObject(3),
		};
		
		System.out.println("정렬 전 출력");
		print(arr);
		
		System.out.println("정렬후 출력");
		Arrays.sort(arr);
		print(arr);
		
	}
	
	public static void print(UserObject[] arr) {
		for(int i=0; i<arr.length; i++) {
			System.out.println(arr[i].toString());
		}
	}
}

출력결과
정렬 전 출력
priority : 5
priority : 2
priority : 4
priority : 6
priority : 1
priority : 7
priority : 3
정렬후 출력
priority : 1
priority : 2
priority : 3
priority : 4
priority : 5
priority : 6
priority : 7
신고
Posted by mysnyc
삽질하기/JAVA2009.05.29 13:50
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
import java.io.IOException;

public class StringZipper {
	
	//GZIPOutputStream을 이용하여 문자열 압축하기
	public byte[] zipStringToBytes(String input) throws IOException {
		
		ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
		GZIPOutputStream gzipOutputStream = new GZIPOutputStream(byteArrayOutputStream);
		BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(gzipOutputStream);
		bufferedOutputStream.write(input.getBytes());
		
		bufferedOutputStream.close();
		byteArrayOutputStream.close();
		
		return byteArrayOutputStream.toByteArray();
	}

	//GZIPInputStream을 이용하여 byte배열 압축해제하기
	public String unzipStringFromBytes(byte[] bytes) throws IOException {
		
		ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);
		GZIPInputStream gzipInputStream = new GZIPInputStream(byteArrayInputStream);
		BufferedInputStream bufferedInputStream = new BufferedInputStream(gzipInputStream);
		ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
		
		byte[] buffer = new byte[100];
		
		int length;
		while((length = bufferedInputStream.read(buffer)) > 0) {
			byteArrayOutputStream.write(buffer, 0, length);
		}
		
		bufferedInputStream.close();
		gzipInputStream.close();
		byteArrayInputStream.close();
		byteArrayOutputStream.close();
		
		return byteArrayOutputStream.toString();
	}
	
	//테스트
	public static void main(String[] args) throws IOException {
		
		StringZipper sz = new StringZipper();
		
		byte[] tmp = sz.zipStringToBytes("TEST");
		String str = sz.unzipStringFromBytes(tmp);
		
		System.out.println(str);
	}
}

신고
Posted by mysnyc
TAG gzip, Java

티스토리 툴바