1D Array
// Examples of native arrays for each primitive type and String
public static void nativeArrayExamples() {
int[] oneDArray = new int[0];
int[][] twoDArray = new int[0][0];
// byte array
byte[] byteArray = {1, 2, 3, 4, 5};
System.out.println("Byte array: " + Arrays.toString(byteArray));
// short array
short[] shortArray = {100, 200, 300, 400, 500};
System.out.println("Short array: " + Arrays.toString(shortArray));
// int array
int[] intArray = {1000, 2000, 3000, 4000, 5000};
System.out.println("Int array: " + Arrays.toString(intArray));
// long array
long[] longArray = {10000L, 20000L, 30000L, 40000L, 50000L};
System.out.println("Long array: " + Arrays.toString(longArray));
// float array
float[] floatArray = {1.1f, 2.2f, 3.3f, 4.4f, 5.5f};
System.out.println("Float array: " + Arrays.toString(floatArray));
// double array
double[] doubleArray = {1.11, 2.22, 3.33, 4.44, 5.55};
System.out.println("Double array: " + Arrays.toString(doubleArray));
// char array
char[] charArray = {'a', 'b', 'c', 'd', 'e'};
System.out.println("Char array: " + Arrays.toString(charArray));
// boolean array
boolean[] booleanArray = {true, false, true, true, false};
System.out.println("Boolean array: " + Arrays.toString(booleanArray));
// String array
String[] stringArray = {"apple", "banana", "cherry", "date", "elderberry"};
System.out.println("String array: " + Arrays.toString(stringArray));
}
2D Array
// Examples of native 2D arrays
public static void native2DArrayExamples() {
// 2D int array
int[][] int2DArray = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
System.out.println("2D int array:");
print2DArray(int2DArray);
// 2D double array
double[][] double2DArray = {
{1.1, 2.2, 3.3},
{4.4, 5.5, 6.6},
{7.7, 8.8, 9.9}
};
System.out.println("2D double array:");
print2DArray(double2DArray);
// 2D String array
String[][] string2DArray = {
{"apple", "banana", "cherry"},
{"dog", "elephant", "fox"},
{"red", "green", "blue"}
};
System.out.println("2D String array:");
print2DArray(string2DArray);
}
// Helper method to print 2D arrays
private static void print2DArray(Object[][] array) {
for (Object[] row : array) {
System.out.println(Arrays.toString(row));
}
System.out.println();
}
ArrayList
// Initialize list with elements using List.of (immutable)
List<String> immutableList = List.of("Apple", "Banana", "Cherry");
System.out.println("Immutable list: " + immutableList);
// Create a mutable list from the immutable list
List<String> list = new ArrayList<>(immutableList);
System.out.println("Mutable list: " + list);
// Adding element at specific index
list.add(1, "Blueberry");
System.out.println("List after adding Blueberry at index 1: " + list);
// Accessing elements
System.out.println("Element at index 2: " + list.get(2));
// Updating elements
list.set(3, "Dragonfruit");
System.out.println("List after updating Cherry to Dragonfruit: " + list);
// Removing elements
list.remove("Banana");
System.out.println("List after removing Banana: " + list);
// Removing element at specific index
list.remove(2);
System.out.println("List after removing element at index 2: " + list);
// Checking if list contains an element
System.out.println("Does list contain Apple? " + list.contains("Apple"));
// Finding index of an element
System.out.println("Index of Blueberry: " + list.indexOf("Blueberry"));
// Sublist
List<String> subList = list.subList(0, 2);
System.out.println("Sublist from index 0 to 2: " + subList);
// Initialize a new immutable list for sorting and reversing
List<String> immutableSortList = List.of("Grape", "Fig", "Elderberry", "Date");
System.out.println("Original immutable list for sorting: " + immutableSortList);
// Create a mutable list from the immutable list
List<String> sortList = new ArrayList<>(immutableSortList);
// Sorting
Collections.sort(sortList);
System.out.println("Sorted list: " + sortList);
// Reversing
Collections.reverse(sortList);
System.out.println("Reversed list: " + sortList);
// Clear the list
list.clear();
System.out.println("List after clearing: " + list);
Set
HashSet
: 순서보장 X, 정렬 X
LinkedHashSet
: 삽입 순 순서보장 O
TreeSet
: BST 로 저장 고로 정렬됨
멤버 메소드
add
: 삽입remove
: 제거retainAll
: 교집합addAll
: 전부 삽입removeAll
: 전부 제거contains
: 포함 여부containsAll
// Initialize sets using Set.of() for immutable sets
Set<Integer> set1 = Set.of(1, 2, 3, 4, 5);
Set<Integer> set2 = Set.of(4, 5, 6, 7, 8);
System.out.println("Set 1: " + set1);
System.out.println("Set 2: " + set2);
// Union
Set<Integer> union = new HashSet<>(set1);
union.addAll(set2);
System.out.println("Union: " + union);
// Intersection
Set<Integer> intersection = new HashSet<>(set1);
intersection.retainAll(set2);
System.out.println("Intersection: " + intersection);
// Difference (set1 - set2)
Set<Integer> difference = new HashSet<>(set1);
difference.removeAll(set2);
System.out.println("Difference (set1 - set2): " + difference);
// Symmetric Difference
Set<Integer> symmetricDifference = new HashSet<>();
symmetricDifference.addAll(set1);
symmetricDifference.addAll(set2);
Set<Integer> temp = new HashSet<>(set1);
temp.retainAll(set2);
symmetricDifference.removeAll(temp);
System.out.println("Symmetric Difference: " + symmetricDifference);
// Subset check
Set<Integer> subset = Set.of(1, 2);
System.out.println("Is " + subset + " a subset of " + set1 + "? " + set1.containsAll(subset));
Map<String, Integer>
HashMap
: 순서보장 X, 정렬 X
LinkedHashMap
: 삽입 순 순서보장 O, 정렬 X
TreeMap
: 정렬 O
put(key, value)
: 삽입get
: 반환 (미존재 시 뭘까..)remove(key)
: 제거containsKey(key)
: 키 존재 여부containsValue(value)
: 밸류 존재 여부isEmpty()
, keySet()
, values()
Map<String, Integer> map = new HashMap<>();
map.put("One", 1);
map.put("Two", 2);
map.put("Three", 3);
System.out.println("Map: " + map);
System.out.println("Value for key 'One': " + map.get("One"));
System.out.println("Removed from map: " + map.remove("Two"));
System.out.println("Map after removal: " + map);
System.out.println("Map contains key 'Three'? " + map.containsKey("Three"));
System.out.println("Map contains value 3? " + map.containsValue(3));
System.out.println("Map size: " + map.size());
System.out.println("Map is empty? " + map.isEmpty());
System.out.println("Map keys: " + map.keySet());
System.out.println("Map values: " + map.values());
Queue
offer(value)
: 뒤에 삽입poll()
: 앞에꺼 반환isEmpty()
Queue<String> queue = new LinkedList<>();
queue.offer("First");
queue.offer("Second");
queue.offer("Third");
System.out.println("Queue: " + queue);
System.out.println("Polled from queue: " + queue.poll());
System.out.println("Queue after poll: " + queue);
Stack
push()
pop()
peek()
Stack<Integer> stack = new Stack<>();
stack.push(1);
stack.push(2);
stack.push(3);
System.out.println("Stack: " + stack);
System.out.println("Popped from stack: " + stack.pop());
System.out.println("Stack after pop: " + stack);
PriorityQueue
offer(val)
poll()
peek()
PriorityQueue<Integer> pq = new PriorityQueue<>();
pq.offer(3);
pq.offer(1);
pq.offer(2);
System.out.println("PriorityQueue: " + pq);
System.out.println("Polled from PriorityQueue: " + pq.poll());
System.out.println("PriorityQueue after poll: " + pq);
Deque
queue 종류는 offer로 명명하다 여기선 add를 쓰는구나
addFirst
addLast
pollFirst
pollLast
// Useful Collections methods examples
public static void usefulCollectionsMethods() {
List<Integer> numbers = new ArrayList<>(Arrays.asList(3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5));
// Sort a list
Collections.sort(numbers);
System.out.println("Sorted list: " + numbers);
// Reverse a list
Collections.reverse(numbers);
System.out.println("Reversed list: " + numbers);
// Shuffle a list
Collections.shuffle(numbers);
System.out.println("Shuffled list: " + numbers);
// Find min and max elements
int min = Collections.min(numbers);
int max = Collections.max(numbers);
System.out.println("Min: " + min + ", Max: " + max);
// Count occurrences of an element
int frequency = Collections.frequency(numbers, 5);
System.out.println("Frequency of 5: " + frequency);
// Replace all occurrences of an element
Collections.replaceAll(numbers, 5, 10);
System.out.println("After replacing 5 with 10: " + numbers);
// Fill a list with a specific element
Collections.fill(numbers, 0);
System.out.println("After filling with 0: " + numbers);
// Create an unmodifiable list
List<String> immutableList = Collections.unmodifiableList(Arrays.asList("a", "b", "c"));
System.out.println("Immutable list: " + immutableList);
// Create a synchronized list
List<Double> syncList = Collections.synchronizedList(new ArrayList<>());
syncList.add(1.1);
syncList.add(2.2);
System.out.println("Synchronized list: " + syncList);
// Binary search (on a sorted list)
numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
int index = Collections.binarySearch(numbers, 7);
System.out.println("Index of 7: " + index);
}