DATA STRUCTURES IN JAVA
implementation of the collections api
author: emmanuel kipruto
youtube tutoriol: baguscripts
2D ARRAYS
public class _2DArrays {
public static void main(String[] args) {
// 2d arrays are seen when creating a tic-tac-toe game
// nested arrays in this case it's a 2d array
char[][] board = new char[3][3];
for (int i=0; i<3; i++){
for (int j = 0; j<3; j++){
board[i][j]='-';
board[0][0]='0';
board[1][0]='0';
board[2][0]='0';
//this is unmodifiable array
char[][] boardTwo =new char[][]{
new char[]{'0','x','-'},
new char[]{'-','0','-'},
new char[]{'-','x','0'}
};
// we use Arrays.deepToString since it's a nested array(2-dimensional array)
System.out.println(Arrays.deepToString(board));
System.out.println(Arrays.deepToString(boardTwo));
LISTS
public class TheLists {
public static void main(String[] args) {
//unlike the array collection the list does not have a limit, you can add to the list as long as you want
// there are a couple implementation of lists and in this tutorial well be looking at arraylist
// you can switch the implementation to any of these any time, and it will work just as fine
//vector, abstract list, stacks ,LinkedLists
List<String> colors = new ArrayList<>();
colors.add("black");
colors.add("green");
colors.add("maroon");
System.out.println(colors.contains("black"));
System.out.println(colors.contains("blue"));
System.out.println(colors);
System.out.println("**** using stream api***");
colors.stream().forEach(System.out::println);
System.out.println("**** using for each ***");
colors.forEach(System.out::println);
System.out.println("**** using enhanced for loop ***");
for (String color : colors){
System.out.println(color);
System.out.println("**** using for loop ***");
for (int i=0;i< colors.size();i++){
System.out.println(colors.get(i));
STACKS
public static void main(String[] args) {
//the stack collection is straight forward and has few methods: peek,pop,size,search,empty
// stacks works on the LIFO(last in first out)
//example of the in real life can be seen when you pile plates
//the last in (the one on top) will be the first out
Stack<Integer> stack = new Stack<>();
stack.push(1);
stack.push(2);
stack.push(3);
stack.push(4);
//the peek method will pick the one on top but won't remove it
System.out.println(stack.peek());
System.out.println(stack.size());
//the pop method will pick the one on top and remove it
System.out.println(stack.pop());
System.out.println(stack.size());
System.out.println(stack.peek());
// the empty method checks if its empty and returns a boolean
System.out.println(stack.empty());
}
QUEUES
public class Queues {
public static void main(String[] args) {
//the queue collection is straight forward and has few methods: peek,pop,size,search,empty
// queue works on the FIFO(last in first out)
//example of the in real life can be seen on a supermarket line
//the fast in will be the first out
Queue<Person> supermarket = new LinkedList<>();
supermarket.add(new Person("manu",22));
supermarket.add(new Person("bagu",23));
supermarket.add(new Person("bob",21));
System.out.println(supermarket.size());
// the peek method returns the top value
System.out.println(supermarket.peek());
// the poll method removes the top value
System.out.println(supermarket.poll());
System.out.println(supermarket.size());
System.out.println(supermarket.peek());
System.out.println(supermarket.size());
System.out.println(supermarket.poll());
System.out.println(supermarket.size());
//working with linked list
System.out.println("*** linked list ***");
//this is a doubly linked list and hence can be more resourceful since it has pointers pointing to the
next and the previous value
LinkedList<Person> linkedList = new LinkedList<>();
linkedList.add(new Person("manu",22));
linkedList.add(new Person("bagu",23));
linkedList.add(new Person("bob",21));
ListIterator<Person> personListIterator = linkedList.listIterator();
while (personListIterator.hasNext()){
System.out.println(personListIterator.next());
System.out.println();
while (personListIterator.hasPrevious()){
System.out.println(personListIterator.previous());
// remember to override the hashcode when working with a class instead of a record
record Person(String name, int age){}
SETS
public class Sets {
public static void main(String[] args) {
// sets does not contain duplicates
// if you put a duplicate value it does not have an effect
Set<Balls> balls= new HashSet<>();
balls.add(new Balls("blue"));
balls.add(new Balls("green"));
// here we add another blue color but the size still remains the same since it doesn't allow
duplicates hence no effect
balls.add(new Balls("blue"));
balls.add(new Balls("yellow"));
System.out.println(balls.size());
balls.forEach(System.out::println);
balls.remove(new Balls("green"));
System.out.println(balls.size());
// hash set will print the values at random whereas a tree set will print in the order they were added
balls.forEach(System.out::println);
// if you use a class here instead of a record then you
// have to override the hashcode,equals and the toString as well(as the constructors)
record Balls (String color){}
}
MAPS
public class Maps {
public static void main(String[] args) {
// maps cannot contain duplicates
// if you put a duplicate value it overrides the previous one
Map<Integer, Person> map = new HashMap<>();
map.put(1, new Person("manu") );
map.put(2, new Person("bagu") );
map.put(3, new Person("bob") );
//this key will override the previous person 'bob'
map.put(3, new Person("wallace") );
System.out.println(map.size());
System.out.println(map.get(1));
System.out.println(map.containsKey(4));
System.out.println(map.keySet());
System.out.println(map.entrySet());
System.out.println("*** loop through the map ***");
map.entrySet()
.forEach(x-> System.out.println(x.getKey()+ " " + x.getValue()));
//Can be replaced with 'Map.forEach()'
map.remove(3);
System.out.println("*** after its removed ***");
map.entrySet()
.forEach(x-> System.out.println(x.getKey()+ " " + x.getValue()));
// if you are using classes you need to override the hashcode,toSting and equals(hashcode is must)
//hashcode is used internally in the hashmaps same as hashset
//equals is used to compare is there are duplicates
//all this comes by default when working with records
record Person(String name){}