# JAVA - Data Structures

kimkevin90·2022년 1월 9일
0

### 1. Arrays

public class TheArrays {
public static void main(String[] args) {
String[] colors = new String[5];
colors[0] = "purple";
colors[1] = "blue";

System.out.println(Arrays.toString(colors));

int [] numbers = {100, 200};

// Array loop
for (int i = 0; i < colors.length ; i++) {
System.out.println(colors[i]);
}

for (int i = colors.length -1; i >= 0 ; i--) {
System.out.println(colors[i]);
}

for (String color : colors) {
System.out.println(color);
}

Arrays.stream(colors).forEach(System.out::println);

}
}

### 2. 2D Arrays

public class GridArrays {
public static void main(String[] args) {
char[][] board = new char[3][3];
for(int i = 0; i<3; i++) {
for (int j = 0; j<3; j++) {
board[i][j] = '-';
}
}

char[][] boardTwo = new char[][] {
new char[] {'O', '-', '-'},
new char[] {'O', '-', '-'},
new char[] {'O', '-', '-'}
};

board[0][0] = 'O';
board[1][0] = 'O';
board[2][0] = 'O';
System.out.println(Arrays.deepToString(board));
System.out.println(Arrays.deepToString(boardTwo));
}
}

### 3. Lists and ArrayList

public class Lists {
public static void main(String[] args) {
// unmodifiable
List<String> colorsUnmodifiable = List.of(
"blue",
"yellow"
);

// 타입지정
List<String> colors =  new ArrayList<>();
System.out.println(colors.size());
System.out.println(colors.contains("yellow"));
System.out.println(colors.contains("pink"));
System.out.println(colors);

for(String color : colors) {
System.out.println(color);
}

colors.forEach(System.out::println);

for (int i = 0; i < colors.size(); i++) {
System.out.println(colors.get(i));
}
}
}

### 4. Stack

public class Stacks {
public static void main(String[] args) {
Stack<Integer> stack = new Stack<>();
stack.push(1);
stack.push(2);
stack.push(3);
System.out.println(stack.peek());
System.out.println(stack.size());
System.out.println(stack.pop());
System.out.println(stack.size());
System.out.println(stack.empty());
}
}

### 5. Queue

public class WorkingWithQueues {

public static void main(String[] args) {

Queue<Person> supermarket = new LinkedList<>();

System.out.println(supermarket.size());
System.out.println(supermarket.peek());
System.out.println(supermarket.poll());
System.out.println(supermarket.size());
}
}

class Person {
private String name;
private Integer age;

public Person(String name, int age) {
this.name = name;
this.age = age;
};
}

### 6. Linked List

public class WorkingWithLinkedList {
public static void main(String[] args) {
ListIterator<Person> personListIterator = linkedList.listIterator();
while (personListIterator.hasNext()) {
System.out.println(personListIterator.next());
}

while (personListIterator.hasPrevious()) {
System.out.println(personListIterator.previous());
}
}
}

class Person {
private String name;
private Integer age;

public Person(String name, int age) {
this.name = name;
this.age = age;
};
}

### 7. Sets

(1) A collection that contains no duplicate elements.
(2) sets contain no pair of elements e1 and e2 such that e1.equals(e2), and at most one null element.
(3) As implied by its name, this interface models the mathematical set abstraction.

public class Sets {
public static void main(String[] args) {
Set<Ball> balls = new HashSet<>();
balls.remove(new Ball("red"));
System.out.println(balls.size());
balls.forEach(System.out::println);
}

static class Ball {
String color;
public Ball(String color) {
this.color = color;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Ball ball = (Ball) o;
return Objects.equals(color, ball.color);
}

@Override
public int hashCode() {
return Objects.hash(color);
}

@Override
public String toString() {
return "Ball{" +
"color='" + color + '\'' +
'}';
}
}
}

### 8. Map Interface

(1) A map cannot contain duplicate keys.
(2) Each key can map to at most one value
(3) As implied by its name, this interface models the mathematical set abstraction.

public class WorkingWithMaps {
public static void main(String[] args) {
Map<Integer, Person2> map = new HashMap<>();
map.put(1, new Person2("Dohee"));
map.put(2, new Person2("Jisub"));
map.put(3, new Person2("Daban"));
// 오버라이딩
map.put(3, new Person2("Daban 2"));
System.out.println(map);
System.out.println(map.size());
System.out.println(map.get(1));
System.out.println(map.containsKey(4));
System.out.println(map.entrySet());
System.out.println(map.keySet());

map.entrySet().forEach(System.out::println);

map.remove(3);

System.out.println(map.getOrDefault(3, new Person2("default")));
System.out.println(map.values());

}

static class Person2 {
String name;

public Person2(String name) {
this.name = name;
}

@Override
public String toString() {
return "Person2{" +
"name='" + name + '\'' +
'}';
}
}
}

### 9. Hash Functions and HashCode

(1) Hash Function is that produces something known as the hash code
(2) Hash code is mapping an object to its integer value

public class WorkingWithMaps {
public static void main(String[] args) {
Map<Person2, Diamond> map = new HashMap<>();
map.put(new Person2("Dohee"), new Diamond("Afican Diamond"));

System.out.println((new Person2("Dohee").hashCode()));
System.out.println((new Person2("Dohee").hashCode()));
System.out.println(map.get(new Person2("Dohee")));
}

static class Person2 {
String name;

public Person2(String name) {
this.name = name;
}

@Override
public String toString() {
return "Person2{" +
"name='" + name + '\'' +
'}';
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Person2 person2 = (Person2) o;
return Objects.equals(name, person2.name);
}

@Override
public int hashCode() {
return Objects.hash(name);
}
}

static class Diamond {
String name;

public Diamond(String name) {
this.name = name;
}

@Override
public String toString() {
return "Diamond{" +
"name='" + name + '\'' +
'}';
}
}
}

참고 : Amigoscode - Java Data Structures Tutorial

Pay it forward