기본 콘텐츠로 건너뛰기

자바 Set, Map

1.Set
HashSet<Integer> abc = new HashSet<Integer>();
abc.add(1);
abc.add(2);
abc.add(3);

Iterator it = abc.iterator();
Integer temp = 0;
while(it.hasNext()) {
temp = (Integer) it.next();
System.out.println(temp);
}

for(Integer item : abc) {
System.out.println(item);
}

2.Map
HashMap<String,Integer> abc = new HashMap<String,Integer>();
abc.put("a", 0);
abc.put("b", 1);
abc.put("c", 2);

for(String temp:abc.keySet()) {
System.out.println(temp);
}

if(abc.containsKey("a")) {
System.out.println(abc.get("a"));
}

abc.clear();
System.out.println(abc.size());

댓글

이 블로그의 인기 게시물

프로그래머스 - 시저 암호

public class Caesar { String caesar ( String s , int n ) { n %= 26 ; String result = "" ; char [] temp = s . toCharArray (); for ( int i = 0 ; i < temp . length ; i ++) { if ( temp [ i ]>= 'a' && temp [ i ]<= 'z' ) temp [ i ] = ( char ) (( temp [ i ]+ n )> 'z' ? ( temp [ i ]+ n - 1 )% 'z' + 'a' : ( temp [ i ]+ n )); else if ( temp [ i ]>= 'A' && temp [ i ]<= 'Z' ) temp [ i ] = ( char ) (( temp [ i ]+ n )> 'Z' ? ( temp [ i ]+ n - 1 )% 'Z' + 'A' : ( temp [ i ]+ n )); result += temp [ i ]; } return result ; } public static void main ( String [] args ) { Caesar c = new Caesar (); System . out . println ( "s는 'a B z', n은 4인 경우: " + c . caesar ( ...