• Home
  • About
    • 코드좀비 photo

      코드좀비

      An amazing website.

    • Learn More
    • Github
  • Posts
    • All Posts
    • All Tags
  • Projects

자바 코테 대비 코테 유용 스킬

28 Mar 2020

Reading time ~2 minutes

자바 코테 대비…

아무래도 c++만 주력 으로 해 왔는데… 프로그래머스 코테가

c++은 사용 못하던…

java 사용은 거의 실질적인 것들만 해 왔어서

알고리즘 풀이나 데이터 타입 등 익숙지가 않다..

그래서 필요한것들 추려서 준비 해야겠다..

Queue / Stack

Queue<Integer> q = new LinkedList<Integer>();
		q.add(1);
		q.add(2);
		while(!q.isEmpty())
			System.out.println(q.poll());
		
		Stack<Integer> s = new Stack<Integer>();
		s.push(1);
		s.push(2);
		while(!s.isEmpty()) {
			System.out.println(s.pop());
		}

Priority Queue

import java.util.PriorityQueue;

PriorityQueue<String> pq = new PriorityQueue<String>();

pq.add("ab");
pq.add("bb");
pq.add("aaa");

while(!pq.isEmpty()){
  System.out.println(pq.poll());
}

- 역순 - 

improt java.util.Collections;
PriorityQueue<String> rpq = new PriorityQueue<String>(Collections.reverseOrder());

Set

package test;
import java.util.HashSet;
import java.util.Iterator;

public class test {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		HashSet<Integer> hs = new HashSet<Integer>();
		hs.add(1);
		hs.add(2);
		hs.add(10);
		hs.add(3);
		hs.add(4);
		hs.add(-3);
		// renove : 삭제, 있으면 true, 없으면 false 반환
		if(hs.remove(10))
			System.out.println("yes it was");
		else
			System.out.println("No");
		if(hs.remove(10))
			System.out.println("yes it was");
		else
			System.out.println("No");
		

		// contains : 존재하는지 
		if(hs.contains(1))
			System.out.println("Yes");
		else
			System.out.println("No");
		
		System.out.println("셋 갯수 : " + hs.size());
		// 자동 변환형
		for( int a : hs) {
			System.out.println(a);
		}
		System.out.println();
		// iterator 형
		
		Iterator<Integer> iter = hs.iterator();
		while(iter.hasNext()) {
			System.out.println(iter.next());
		}	
	}
}

MAP

        HashMap<Integer,Integer> m = new HashMap<Integer,Integer>();
		m.put(1,5);
		System.out.println(m.remove(1));
		m.put(1,3);
		m.put(2,4);
		System.out.println("key 1 : value : " + m.get(1));
		if(m.containsKey(1))
			System.out.println("1 yes");
		if(m.containsValue(3))
			System.out.println("v3 yes");
		System.out.println(m);
		for(int key : m.keySet()) {
			System.out.println("key : " + key + " value : " + m.get(key));
		}
		for(int value : m.values()) {
			System.out.println("value : " + value);
		}
		

Pair ( 자작)

static class Pair implements Comparable<Pair>{
		int first;
		int second;

		public Pair(){}
		public Pair(int a, int b) {
			this.first = a; 
			this.second = b;
		}
		public void Set(int a, int b) {
			this.first = a; 
			this.second = b;
		}

		@Override
		public int compareTo(Pair o) {
			if(this.first > o.first)
				return -1;
			else if( this.first == o.first) {
				if(this.second > o.second)
					return -1;
				else
					return 1;
			}
			else
				return 1;
		}
	}

소트

Arrays.sort(배열);
Arrays.sort(배열,collections.reverseOrder());


java Share Tweet +1