[Day25] Java 25 [9/14]

 

PriorityQueue

PriorityQueue ์ปฌ๋ ‰์…˜ ํด๋ž˜์Šค 

ใ„ด FIFO X , ์ €์žฅ์ˆœ์„œ์— ์ƒ๊ด€์—†์ด Priority(์šฐ์„ ๊ถŒ) ๋†’์€ ์š”์†Œ๊ฐ€ ๋จผ์ € ๋‚˜์˜จ๋‹ค. 

// <๊ธฐ๋ณธํ˜• X, ์ฐธ์กฐํ˜• O> ์ œ๋„ค๋ฆญ
// Queue<int>   q = new PriorityQueue<int>();
Queue<Integer>   q = new PriorityQueue<Integer>();
// ์ •์ˆ˜์ธ ๊ฒฝ์šฐ์—๋Š” ๋‚ฎ์€ ์ •์ˆ˜๊ฐ€ ์šฐ์„ ์ˆœ์œ„๊ฐ€ ๋†’๊ตฌ๋‚˜..(ํ™•์ธ)
q.offer(3);  // int -> Integer  ์˜คํ†  ๋ฐ•์‹ฑ
q.offer(5);
q.offer(2);
q.offer(4);
q.offer(1);
		
// ํ  FIFO
while ( ! q.isEmpty() ) {
	  int n = q.poll();
	  System.out.println( n ); // 1,2,3,4,5
} //

Deque( [D]ouble-[E]nded   [Que]ue )            FIFO ๋””ํ, ๋ฑ

์–‘์ชฝ ๋์—์„œ ๋ชจ๋‘ ์ถ”๊ฐ€/์ถ”์ถœ์ด ๊ฐ€๋Šฅํ•œ ์ปฌ๋ ‰์…˜ ํด๋ž˜์Šค 

LinkedList , ArrayDeque ์ปฌ๋ ‰์…˜ ํด๋ž˜์Šค

 

↑ ↓ offer()               offerFirst()         pollFirst()

|        |

|        |

|        |

|        |

↑ ↓ poll()                   pollLast()             offerLast()

 

public static void main(String[] args) {
	Deque<String> dq = new LinkedList<String>();
} // main

 

Iterator,  ListIterator, Enumeration  ์ธํ„ฐํŽ˜์ด์Šค

๋ชจ๋‘ ์ปฌ๋ ‰์…˜์—์„œ ์ €์žฅ๋œ ์š”์†Œ๋ฅผ ์ ‘๊ทผํ•˜๋Š”๋ฐ ์‚ฌ์šฉ๋˜๋Š” ์ธํ„ฐํŽ˜์ด์Šค    

Enumeration(๊ตฌ๋ฒ„์ ผ)  Iterator(์‹ ๋ฒ„์ ผ) < ListIterator ๊ธฐ๋Šฅํ™•์žฅ

fail-fast  ๊ธฐ๋Šฅ  

Vector<String> v = new Vector<>();
v.add("A"); v.add("B"); v.add("C"); v.add("D"); v.add("E");
		
// ์—ด๊ฑฐ์ž
// Iterator               -> ๋‹จ๋ฐฉํ–ฅ ์š”์†Œ ์ ‘๊ทผ
// ListIterator        ->  <- ์–‘๋ฐฉํ–ฅ ์š”์†Œ ์ ‘๊ทผ

ListIterator<String> ir = v.listIterator();

while (ir.hasNext()) {
	String str =  ir.next();
	System.out.println( str );
}
	   
while (ir.hasPrevious() ) { // ์ด์ „ ์š”์†Œ ์žˆ๋ƒ? 
	String str = ir.previous();
	System.out.println( str );
}

Arrays ๋ฉ”์„œ๋“œ

1) copyOf(), copyOfRange() - ๋ฐฐ์—ด ๋ณต์‚ฌ

2) fill(), setAll()                    - ๋ฐฐ์—ด ์ฑ„์šฐ๊ธฐ

3) sort(), binarySearch()     - ์ •๋ ฌ ๊ณผ ๊ฒ€์ƒ‰

4) equals(), toString()  ๋ฐฐ์—ด ๋น„๊ต, ์ถœ๋ ฅ

5) asList()                               - ๋ฐฐ์—ด -> List ์ปฌ๋ ‰์…˜ํด๋ž˜์Šค ๋ณ€ํ™˜

6) parallelXXX() , Spliterator() , stream()

- ์ฒ˜๋ฆฌ ์†๋„(์„ฑ๋Šฅ)์„ ์œ„ํ•ด์„œ

- ์—ฌ๋Ÿฌ ์Šค๋ ˆ๋“œ๊ฐ€ ๋™์‹œ์— ์ฒ˜๋ฆฌํ•˜๋Š” ๋ฉ”์„œ๋“œ

 

 

Comparator ์ธํ„ฐํŽ˜์ด์Šค์™€  Comparable

 

- ๋ฐฐ์—ด ์ฑ„์šฐ๊ธฐ

Arrays.setAll(m,   (x) -> (int)(Math.random()*101)  );
System.out.println( Arrays.toString(m) );
Arrays.sort(m);  // ๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌ
System.out.println( Arrays.toString(m) );

 

- ์ •๋ ฌ

// ์˜ค๋ฆ„์ฐจ์ˆœ(asc) ์ •๋ ฌ
Arrays.sort( m );
		
// ๋‚ด๋ฆผ์ฐจ์ˆœ( desc ) ์ •๋ ฌ + Comparator ์ง์ ‘๊ตฌํ˜„ , ๋ฌด๋ช…ํด๋ž˜์Šค, ๋žŒ๋‹ค์‹
Arrays.sort(m, new DesendingComparator());

// ๋Œ€์†Œ๋ฌธ์ž ๊ตฌ๋ถ„ํ•˜์ง€ ์•Š๊ณ  ์ •๋ ฌ( ๊ธฐ์–ต )
Arrays.sort(m,  String.CASE_INSENSITIVE_ORDER );
System.out.println( Arrays.toString(m) );

 

Comparator ์ธํ„ฐํŽ˜์ด์Šค

ํด๋ž˜์Šค ์ž์ฒด์—์„œ implements Comparable ํ•ด์„œ @Override

public class Ex06_02 {
	public static void main(String[] args) {
		// 12:05 ์ˆ˜์—… ์‹œ์ž‘~ 
		Person [] m = {
				new Person("๋ฌธํ˜œ๋นˆ", 23),				
				new Person("๊น€๊ฒฝ์—ฐ", 27),
				new Person("์กฐ๋ฏผ๊ฒฝ", 25)
		}; 
		
		System.out.println(  Arrays.toString( m ) );
		
		// ์ •๋ ฌ
		// java.lang.ClassCastException: [days25.Person] cannot be cast to java.lang.[Comparable]
		// Person ํด๋ž˜์Šค๋Š” Comparable์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ ํ•˜์ง€ ์•Š์•˜๊ธฐ ๋•Œ๋ฌธ์— ํ˜•๋ณ€ํ™˜ X -> ์ •๋ ฌ( ๋น„๊ต  X )
		//  Person ํด๋ž˜์Šค   ๋ฌด์—‡์„ ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌ ์ •ํ•ด์ ธ ์žˆ์ง€ ์•Š๋‹ค. 
		/*
		Arrays.sort( m , new Comparator<Person>() {

			@Override
			public int compare(Person o1, Person o2) {
				 
				return o1.age      -       o2.age;
				// return o1.name.compareTo(     o2.name  );
			}
		});
		*/
		
		// Arrays.sort( m ,   ( o1,  o2) -> o1.name.compareTo(     o2.name  ) );		
		Arrays.sort( m );  // ์ด๋ฆ„, ๋‚˜์ด ์ •๋ ฌ		
		
		System.out.println(  Arrays.toString( m ) );	
	} // main
} // class

class Person implements Comparable<Person>{
	
	// ํ•„๋“œ
	String rrn; // ์ฃผ๋ฏผ๋“ฑ๋ก๋ฒˆํ˜ธ
	String name;
	int age;	
	
	// ์ƒ์„ฑ์ž
	public Person() {
		super(); 
	}
	public Person(String name, int age) {
		super();
		this.name = name;
		this.age = age;
	}  
	public Person(String rrn, String name, int age) {
		super();
		this.rrn = rrn;
		this.name = name;
		this.age = age;
	}
	
	// 
	@Override
	public String toString() {
		return "Person [rrn=" + rrn + ", name=" + name + ", age=" + age + "]";
	}
	
	@Override
	public int compareTo(Person o) { 
		return this.name.compareTo(o.name);
	}
	
	// Student                   hashCode() , equals() ์˜ค๋ฒ„๋ผ์ด๋”ฉ~
	// HashSet ์ถ”๊ฐ€ํ• ๋•Œ   ํ•™๋ฒˆ ๊ฐ™์œผ๋ฉด ์ถ”๊ฐ€๋˜์ง€ ์•Š๋„๋ก ๊ตฌํ˜„ํ•˜์„ธ์š”. 
	//                                 
	@Override
	public int hashCode() { 
		return  this.rrn.hashCode();
	}
	@Override
	public boolean equals(Object obj) {		
		
		if( obj instanceof Person) { 
			Person p = (Person)obj;
			return this.rrn.equals(p.rrn);  // true, false 
		}	
		return false;
	}	
} // class

 

 

HashSet ์ปฌ๋ ‰์…˜ ํด๋ž˜์Šค

ใ„ด Set ๊ตฌํ˜„ํ•œ ๋Œ€ํ‘œ์ ์ธ ์ปฌ๋ ‰์…˜ ํด๋ž˜์Šค

ใ„ด ์ˆœ์„œ ์œ ์ง€ X, ์ค‘๋ณต ํ—ˆ์šฉ X

HashSet<Integer>  hs = new HashSet<>();
// Collecion <? extends Integer> c ๋งค๊ฐœ๋ณ€์ˆ˜   
hs.add( 3 );
hs.add( 5 );
hs.add( 2 );
hs.add( 4 );
hs.add( 1 );
		
// ๋ฉ”์„œ๋“œ 3๊ฐ€์ง€ - 1)๊ธฐ๋Šฅ 2) ๋งค๊ฐœ๋ณ€์ˆ˜ 3)๋ฆฌํ„ด๊ฐ’(๋ฆฌํ„ด์ž๋ฃŒํ˜•)
System.out.println(   hs.add( 1 )   );  // ์ค‘๋ณต ํ—ˆ์šฉ X    false
hs.remove(5);  // Object o,   int index X

ํ™œ์šฉ (์ค‘์š” : HashSet -> LinkedList ๋ณ€ํ™˜, ์ •๋ ฌํ•˜๊ธฐ ์œ„ํ•ด)

public static void main(String[] args) {
	HashSet<Integer>  lotto = new HashSet<Integer>();
		
	fillLotto( lotto ); 
	dispLotto( lotto ); 

} // main

public static void fillLotto( HashSet<Integer> lotto) { 
	Random rnd =  new Random();  
	while( lotto.size() < 6) {
		    int temp = rnd.nextInt(45) + 1;   
			lotto.add( temp );  
	} // while  
}

public  static void dispLotto(HashSet<Integer> lotto) {
	// ์ปฌ๋ ‰์…˜๊ฐ์ฒด -> [] ๋ฐฐ์—ด๋กœ ๋ณ€ํ™˜  : toArray()
	// Object [] =  lotto.toArray(); 
	
	/*
	List<Integer> list = new LinkedList<>(lotto);  // ์•”๊ธฐ( ์‹œํ—˜ ) 
	Collections.sort(list);
	*/
	
	Iterator<Integer> ir = lotto.iterator();
	// Iterator<Integer> ir =  list.iterator();
	while (ir.hasNext()) {
		int n =  ir.next();  // ์˜คํ†  ์–ธ๋ฐ•์‹ฑ  ( Integer -> int ํ˜•๋ณ€ํ™˜)
		System.out.printf("[%d]", n);
	}
	System.out.println();
} // dispLotto

LinkedHashSet

LinkedHashSet<Integer> lotto = new LinkedHashSet<>();
		
lotto.add(19);
lotto.add(7);
lotto.add(27);

System.out.println(  lotto );  //์ˆœ์„œ์œ ์ง€
//  ๋กœ๋˜๊ฒŒ์ž„ : 3๋ฒˆ         
int gameNumber = 1;
		
Scanner scanner = new Scanner(System.in);
System.out.print("> ๋กœ๋˜ ๊ฒŒ์ž„ ํšŸ์ˆ˜ ์ž…๋ ฅ ? ");
gameNumber = scanner.nextInt();
		
// int [][] lottos = new int[gameNumber][6];
// LinkedHashSet<Integer> [] lottos = new LinkedHashSet[gameNumber]; 		
ArrayList< LinkedHashSet<Integer> > lottos = new ArrayList<LinkedHashSet<Integer>>();
		
// [ ๋ฐฐ์—ด์˜ ๋ฐฐ์—ด = ๋‹ค์ฐจ์› ]
// ๋กœ๋˜ ์ฑ„์›Œ๋„ฃ๊ณ .
for (int i = 0; i <  gameNumber  ; i++) {  // == gameNumber
	LinkedHashSet<Integer>   lotto = new LinkedHashSet<Integer>();			
	Ex07_02.fillLotto(lotto);				
	lottos.add(lotto);
}

// ๋กœ๋˜ ์ถœ๋ ฅ
Iterator<LinkedHashSet<Integer>> ir = lottos.iterator();
while (ir.hasNext()) {
	LinkedHashSet<Integer> lotto = ir.next();
	Ex07_02.dispLotto(lotto);
}

* ArrayList์•ˆ์— LinkedHashSet ๋„ฃ์„ ์ˆ˜ ์žˆ์Œ!!

HashSet<Person>  hs = new HashSet<Person>();
		
// ์ฃผ๋ฏผ๋“ฑ๋ก๋ฒˆํ˜ธ๊ฐ€ ๊ฐ™๋‹ค๋ฉด ๋™์ผํ•œ ์‚ฌ๋žŒ( Person) ์ด๋‹ค.
Person p1 = new Person(  "1111", "ํ™๊ธธ๋™", 20  );
Person p2 = new Person(  "1111", "ํ™๊ธธ๋™", 20  );
		 
System.out.println( p1.hashCode() );  // 366712642
System.out.println( p2.hashCode() ); // 1829164700		
System.out.println(  p1.equals(p2) );  // false
	 
		
// ์ฃผ๋ฏผ๋“ฑ๋ก๋ฒˆํ˜ธ๊ฐ€ ๊ฐ™์€ Person์€ hs ์— ์ค‘๋ณต์ฒดํฌ์— ๊ฑธ๋ ค์„œ ์ถ”๊ฐ€๋˜์ง€ ์•Š๋„๋ก ํ•˜๊ณ  ์‹ถ๋‹ค. 
// ๋ฐฉ๋ฒ• ?  Object.hashCode(), equals() ๋ฉ”์„œ๋“œ ๋ฅผ ์˜ค๋ฒ„๋ผ์ด๋”ฉ.
hs.add(  p1 );
hs.add(  p2 );
		
System.out.println(  hs ); // [Person [rrn=1111, name=ํ™๊ธธ๋™, age=20]]

์˜ˆ์ œ) ๋น™๊ณ ํŒ ์•ˆ์— 25๊ฐœ์˜ ์ˆซ์ž ์ค‘๋ณต์ด ๋˜์ง€ ์•Š๊ฒŒ ๋„ฃ๊ณ  ๋‹ค์‹œ 2์ฐจ์› ๋ฐฐ์—ด๋กœ ์ฑ„์›Œ๋„ฃ๊ธฐ ์œ„ํ•ด LinkedHashSet ์‚ฌ์šฉ

int [][] board = new int[5][5]; //
//  1~25 ์ˆซ์ž๋ฅผ  ์ค‘๋ณด๋˜์ง€ ์•Š๊ฒŒ ์ž„์˜๋กœ ๋‹ค ์ฑ„์›Œ ๋„ฃ์–ด์•ผ ๋˜๋„ค์š”.
Random rnd = new Random();		
		
// 	์™œ ? LinkedHashSet ์ปฌ๋ ‰์…˜ ํด๋ž˜์Šค ์‚ฌ์šฉ
LinkedHashSet<Integer>  hs = new LinkedHashSet<Integer>();		
// 3:06 ์ˆ˜์—…์‹œ์ž‘
while(  hs.size() < 25  ) {
	 hs.add(       rnd.nextInt(25) +1        );  // 1~25 
} // while
		
// ์ถœ๋ ฅ.
// [25, 23, 2, 16, 22, 21, 6, 20, 7, 12, 15, 10, 1, 3, 13, 18, 14, 9, 24, 5, 11, 8, 4, 19, 17]
System.out.println(  hs );
		
// hs -> [][] board
Iterator<Integer>  ir = hs.iterator();
// []  -> [][]
int i = 0;
while (ir.hasNext()) {
	 int n =  ir.next();
	 board[i/5][i%5] = n;
	  i++;
}

์ค‘๋ณต์ฒดํฌ ( HashSet์— new ์—ฐ์‚ฐ์ž๋กœ ์„œ๋กœ ๋‹ค๋ฅธ ์ธ์Šคํ„ด์Šค ์ €์žฅํ•˜๋ฉด ๋‹ค๋ฅธ ๊ฐ์ฒด๋กœ ์ธ์‹, ๋งŒ์•ฝ ๊ฐ์ฒด์˜ ๋‚ด์šฉ์ด ๋™์ผํ•œ ๊ฒƒ์œผ๋กœ ์ทจ๊ธ‰ํ•˜๊ธฐ ์œ„ํ•ด์„  Person ๊ฐ์ฒด ๋‚ด์—์„œ equals ๋ฉ”์„œ๋“œ๋ฅผ ์˜ค๋ฒ„๋ผ์ด๋”ฉ ํ•ด์ค˜์•ผํ•จ

// ์ค‘๋ณต ์ฒดํฌ ๋ฐฉ๋ฒ• ? 
HashSet<Person>  hs = new HashSet<Person>();
		
// ์ฃผ๋ฏผ๋“ฑ๋ก๋ฒˆํ˜ธ๊ฐ€ ๊ฐ™๋‹ค๋ฉด ๋™์ผํ•œ ์‚ฌ๋žŒ( Person) ์ด๋‹ค.
Person p1 = new Person(  "1111", "ํ™๊ธธ๋™", 20  );
Person p2 = new Person(  "1111", "ํ™๊ธธ๋™", 20  );
		
System.out.println( p1.hashCode() );  // 366712642
System.out.println( p2.hashCode() ); // 1829164700		
 
System.out.println(  p1.equals(p2) );  // false
	 		
// ์ฃผ๋ฏผ๋“ฑ๋ก๋ฒˆํ˜ธ๊ฐ€ ๊ฐ™์€ Person์€ hs ์— ์ค‘๋ณต์ฒดํฌ์— ๊ฑธ๋ ค์„œ ์ถ”๊ฐ€๋˜์ง€ ์•Š๋„๋ก ํ•˜๊ณ  ์‹ถ๋‹ค. 
// ๋ฐฉ๋ฒ• ?  Object.hashCode(), equals() ๋ฉ”์„œ๋“œ ๋ฅผ ์˜ค๋ฒ„๋ผ์ด๋”ฉ.
hs.add(  p1 );
hs.add(  p2 );
		
System.out.println(  hs ); // [Person [rrn=1111, name=ํ™๊ธธ๋™, age=20]]

Person๊ฐ์ฒด

@Override
public int hashCode() {
  return this.rrn.hashCode();
}

@Override
public boolean equals(Object obj) {
  if(obj instanceof Person) {
    Person p = (Person)obj;
    return this.rrn.equals(p.rrn);
  }
  return false;
}

 

ํ•ฉ์ง‘ํ•ฉ / ๊ต์ง‘ํ•ฉ / ์ฐจ์ง‘ํ•ฉ

HashSet<Integer> a = new HashSet<Integer>();
a.add(1);a.add(2);a.add(3);a.add(4);a.add(5);
		
HashSet<Integer> b = new HashSet<Integer>();
b.add(4);b.add(5);b.add(6);b.add(7);b.add(8);

// 1.  ํ•ฉ์ง‘ํ•ฉ      a  U   b     [1, 2, 3, 4, 5, 6, 7, 8]
HashSet<Integer> hab = new HashSet<Integer>();
		
/*
Iterator<Integer>  ir = a.iterator();
while (ir.hasNext()) {
	Integer n = (Integer) ir.next();
	hab.add(n);
}
		
ir = b.iterator();
while (ir.hasNext()) {
	Integer n = (Integer) ir.next();
	hab.add(n);
}
*/
		
hab.addAll(a);
hab.addAll(b);

// 2. ๊ต์ง‘ํ•ฉ  a  ∩  b   [4, 5]
HashSet<Integer> kyo = new HashSet<Integer>();
				
Iterator<Integer>  ir = a.iterator();
while (ir.hasNext()) {
	Integer n = (Integer) ir.next();
	if(  b.contains( n )) {  // ๊ต์ง‘ํ•ฉ
		kyo.add(n);
	} else{                        // ์ฐจ์ง‘ํ•ฉ
		//cha.add(n);
	}
} //  while
				
// 3. ์ฐจ์ง‘ํ•ฉ  a - b  [1,2,3]
HashSet<Integer> cha = new HashSet<Integer>( a  );  // [1, 2, 3, 4, 5]
// cha.addAll(a);
cha.removeAll(kyo);

 

 

TreeSet 

[TreeSet ํŠน์ง•]

1) ์ด์ง„ ๊ฒ€์ƒ‰ ํŠธ๋ฆฌ (binarySearch Tree) -> ๋ฐ์ดํ„ฐ ์ €์žฅ

  ๊ฒ€์ƒ‰ ์„ฑ๋Šฅ ๋›ฐ์–ด๋‚˜๋‹ค. 

 

2) Set = ์ค‘๋ณต ํ—ˆ์šฉ X, ์ˆœ์„œ ์œ ์ง€ X

 

3)  ๊ฒ€์ƒ‰ + ์ •๋ ฌ  + ๋ฒ”์œ„ ๊ฒ€์ƒ‰ ์„ฑ๋Šฅ ๋›ฐ์–ด๋‚˜๋‹ค. 

 

4) ๋งํฌ๋“œ๋ฆฌ์ŠคํŠธ ์ฒ˜๋Ÿผ    ๋…ธ๋“œ(Node)๊ฐ€  ์—ฐ๊ฒฐ๋œ ๊ตฌ์กฐ.

  next  ๋‹ค์Œ ์š”์†Œ ์ฃผ์†Œ

  Prev  ์ด์ „ ์š”์†Œ ์ฃผ์†Œ   

 

5)  ์ตœ์ƒ์œ„ ๋…ธ๋“œ๋ฅผ  "root" ๋…ธ๋“œ

  ๋ถ€๋ชจ-์ž์‹  ๋…ธ๋“œ ๊ด€๊ณ„,

  ํ˜•์ œ ๋…ธ๋“œ ๊ด€๊ณ„     

 

6)    class TreeNode{

  TreeNode  ์™ผ์ชฝ ์ž์‹๋…ธ๋“œ;

  TreeNode  ์˜ค๋ฅธ์ชฝ ์ž์‹๋…ธ๋“œ;

  Object value;

}  

TreeSet<Integer>  ts = new TreeSet<Integer>();
ts.add( 7 );
ts.add( 4 );
ts.add( 9 );
ts.add( 1 );
ts.add( 5 );
ts.add( 6 ); 
        
System.out.println( ts ); //[1, 4, 5, 6, 7, 9]
        
System.out.println(  ts.first() );  // ์ •๋ ฌ๋œ ์ˆœ์„œ์—์„œ ์ฒซ ๋ฒˆ์งธ ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.   1
System.out.println(  ts.last() );  // ์ •๋ ฌ๋œ ์ˆœ์„œ์—์„œ ๋งˆ์ง€๋ง‰ ๋ฒˆ์งธ ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.  9
        
SortedSet<Integer>  ss =  ts.subSet(1, 7);
System.out.println( ss ); //[1, 4, 5, 6]
        
// ์ง€์ •๋œ ๊ฐ์ฒด(1) ๋ณด๋‹ค ํฐ ๊ฐ’์„ ๊ฐ€์ง„ ๊ฐ์ฒด ์ค‘์— ์ œ์ผ ๊ฐ€๊นŒ์šด ๊ฐ’์˜ ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜, ์—†์œผ๋ฉด null
System.out.println(  ts.higher(1) );  // 4
// ts.lower(1)        // null
        
// ์ง€์ •๋œ ๊ฐ์ฒด(3)์™€ ๊ฐ™์€ ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜, ์—†์œผ๋ฉด ์ž‘์€ ๊ฐ’์„ ๊ฐ€์ง„ ๊ฐ์ฒด ์ค‘์— ์ œ์ผ ๊ฐ€๊นŒ์šด ๊ฐ’์„ ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜
//  ์—†์œผ๋ฉด null ๋ฐ˜ํ™˜
System.out.println(  ts.floor(3) );  //  1
        
// ์ง€์ •๋œ ๊ฐ์ฒด(3)์™€ ๊ฐ™์€ ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜, ์—†์œผ๋ฉด ํฐ๊ฐ’์„ ๊ฐ€์ง„ ๊ฐ์ฒด ์ค‘์— ์ œ์ผ ๊ฐ€๊นŒ์šด ๊ฐ’์„ ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜
//  ์—†์œผ๋ฉด null ๋ฐ˜ํ™˜
System.out.println(  ts.ceiling(3) );  //  4
        
// TreeSet์€ ์ •๋ ฌ๋œ ๊ตฌ์กฐ ( ๋”ฐ๋กœ ์ •๋ ฌ X )

์˜ˆ์ œ

TreeSet<Integer> set = new TreeSet<Integer>();
int [] score = { 80, 95, 50, 35, 45, 65, 10, 100};
for (int i = 0; i < score.length; i++) {
	 set.add(  score[i] );
}
System.out.println( set );  // ์ž๋™ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ
			
// headSet() : ์ง€์ •๋œ ๊ฐ์ฒด(50)๋ณด๋‹ค ์ž‘์€ ๊ฐ’์˜ ๊ฐ์ฒด๋“ค์„ ๋ฐ˜ํ™˜.
SortedSet<Integer>  r1 = set.headSet(50, true);   // 50 ํฌํ•จ๋˜์ง€ ์•Š๋Š”๋‹ค. 
		
// tailSet() : ์ง€์ •๋œ ๊ฐ์ฒด(50)๋ณด๋‹ค ํฐ ๊ฐ’์˜ ๊ฐ์ฒด๋“ค์„ ๋ฐ˜ํ™˜.
SortedSet<Integer>  r1 = set.tailSet(50);   // 50 ํฌํ•จ๋˜์ง€ ์•Š๋Š”๋‹ค. 
set.headSet(50, true);   // 50 ํฌํ•จํ•œ๋‹ค.

 

 

  • ๋„ค์ด๋ฒ„ ๋ธ”๋Ÿฌ๊ทธ ๊ณต์œ ํ•˜๊ธฐ
  • ๋„ค์ด๋ฒ„ ๋ฐด๋“œ์— ๊ณต์œ ํ•˜๊ธฐ
  • ํŽ˜์ด์Šค๋ถ ๊ณต์œ ํ•˜๊ธฐ
  • ์นด์นด์˜ค์Šคํ† ๋ฆฌ ๊ณต์œ ํ•˜๊ธฐ