Data Structures in Java
Data Structures in Java
2D ARRAYS
board[i][j]='-';
board[0][0]='0';
board[1][0]='0';
board[2][0]='0';
new char[]{'0','x','-'},
new char[]{'-','0','-'},
new char[]{'-','x','0'}
};
System.out.println(Arrays.deepToString(board));
System.out.println(Arrays.deepToString(boardTwo));
LISTS
//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
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);
colors.stream().forEach(System.out::println);
colors.forEach(System.out::println);
System.out.println(color);
System.out.println(colors.get(i));
STACKS
//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
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());
System.out.println(stack.empty());
}
QUEUES
//the queue collection is straight forward and has few methods: peek,pop,size,search,empty
supermarket.add(new Person("manu",22));
supermarket.add(new Person("bagu",23));
supermarket.add(new Person("bob",21));
System.out.println(supermarket.size());
System.out.println(supermarket.peek());
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());
//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.add(new Person("manu",22));
linkedList.add(new Person("bagu",23));
linkedList.add(new Person("bob",21));
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
SETS
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);
// have to override the hashcode,equals and the toString as well(as the constructors)
}
MAPS
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());
map.entrySet()
map.remove(3);
System.out.println("*** after its removed ***");
map.entrySet()
// if you are using classes you need to override the hashcode,toSting and equals(hashcode is must)