Lunski's Clutter

This is a place to put my clutters, no matter you like it or not, welcome here.

0%

Remainder

補些遺漏的部分。

HashSet

只需要存儲不重複的key,像map,但並不需要value,可以使用Set。

1
2
3
4
5
6
7
8
9
10
11
12
13
import java.util.*;
public class Main {
public static void main(String[] args) {
Set<String> set = new HashSet<>();
System.out.println(set.add("abc")); //true
System.out.println(set.add("xyz")); //true
System.out.println(set.add("xyz")); //false
System.out.println(set.contains("xyz")); //true
System.out.println(set.contains("XYZ")); //false
System.out.println(set.remove("hello")); //false
System.out.println(set.size()); //2
}
}

TreeSet

有序的Set

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import java.util.*;
public class Main {
public static void main(String[] args) {
TreeSet ts = new TreeSet();
ts.add("apple");
ts.add("banana");
ts.add("pear");
ts.add("orange");
System.out.println(ts);
}
}
>
java -cp /tmp/hdkJZZyPLb Main
[apple, banana, orange, pear]

TreeMap

不像HashMap是無序的key,內部會對Key進行排序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import java.util.*;
public class Main {
public static void main(String[] args) {
Map<Integer,String> map = new TreeMap<>();
map.put(2,"apple");
map.put(3,"pear");
map.put(1,"orange");
for (Integer key : map.keySet()) {
System.out.println(key);
}
}
}
>
1
2
3

// reverse
TreeMap<Integer, String> map =
new TreeMap<>(Comparator.reverseOrder());
map.put(3, "val");
map.put(2, "val");
map.put(1, "val");
map.put(5, "val");
map.put(4, "val");

assertEquals("[5, 4, 3, 2, 1]", map.keySet().toString());

Binary Tree

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import java.util.*;
class Node {
int key;
Node left, right;

public Node(int item) {
key = item;
left = right = null;
}
}
class BinaryTree {
Node root;

BinaryTree(){ root = null;}
BinaryTree(int key){ root = new Node(key);}

public void traverseInOrder(Node node) {
if (node != null) {
traverseInOrder(node.left);
System.out.print(node.key+" ");
traverseInOrder(node.right);
}
}

public static void main(String[] args) {
BinaryTree tree = new BinaryTree();

tree.root = new Node(1);
tree.root.left = new Node(2);
tree.root.right = new Node(3);
tree.root.left.left = new Node(4);

System.out.print("\nIn order Traversal: ");
tree.traverseInOrder(tree.root);
}
}
>
java -cp /tmp/hdkJZZyPLb BinaryTree
In order Traversal: 4 2 1 3

Bubble sort && Inserting sort

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
public class Sort {  
static void bubbleSort(int[] arr) {
int temp = 0, len = arr.length;
for(int i=0; i < len; i++){
for(int j=1; j < (len-i); j++){
//swap
if(arr[j-1] > arr[j]){
temp = arr[j-1];
arr[j-1] = arr[j];
arr[j] = temp;
}
}
}
}
public static void selectionSort(int[] arr){
for (int i = 0; i < arr.length - 1; i++) {
int index = i;
for (int j = i + 1; j < arr.length; j++){
if (arr[j] < arr[index]){
index = j;
}
}
int smallerNumber = arr[index];
arr[index] = arr[i];
arr[i] = smallerNumber;
}
}
public static void main(String[] args) {
int arr[] ={3,60,35,2,45,320,5};

//System.out.println("Bubble Sort");
//bubbleSort(arr);
System.out.println("Selection Sort");
selectionSort(arr);

for(int i=0; i < arr.length; i++){
System.out.print(arr[i] + " ");
}

}
}

如果你覺得這篇文章很棒,請你不吝點讚 (゚∀゚)

Welcome to my other publishing channels