Skip to content

Latest commit

 

History

History
198 lines (156 loc) · 3.47 KB

File metadata and controls

198 lines (156 loc) · 3.47 KB

Advanced Data Structure

Collections

Questions:

  1. What is Collections in java?

Dynamic Array

Questions:

  1. What is a dynamic array?
import java.util.ArrayList;
import java.util.List;

public class Demo {
	public static void main(String[] args) {
		List<Integer> a = new ArrayList<>();
		a.add(1);
		a.add(2);
		for (int i : a){
			System.out.println(i);
		}
	}
}

Dimensional Array

public class Demo{
	public static void main(String[] args) {
		int[][] a = new int[2][2];
		int[][] b = {{1,2,3},{4,5,6},{7,8,9}};
	}
}

Set

import java.util.Set;
import java.util.HashSet;

public class Demo{
	public static void main(String[] args) {
		Set<Integer> a = new HashSet<>();
		a.add(1);
		a.add(2);
		a.add(1);
	}
}

Questions:

  1. Can a Set contain duplicate elements?

Queue and Stack

import java.util.Queue;
import java.util.LinkedList;

public class Demo{
	public static void main(String[] args) {
		Queue<Integer> a = new LinkedList();
		a.offer(1);
		a.offer(2);
		a.offer(3);
		a.poll();
	}
}
import java.util.Stack;

public class Demo{
	public static void main(String[] args) {
		Stack<Integer> a = new Stack<>();
		a.push(1);
		a.push(2);
		a.push(3);
		a.pop();
	}
}

Questions:

  1. Is Queue First-In-First-Out or Last-In-First-Out? How about Stack?
  2. Describe some examples of the implementations on queue and stack?

Map

import java.util.Map;
import java.util.HashMap;

public class Demo{
	public static void main(String[] args) {
		Map<Integer, Integer> a = new HashMap<>();
		a.put(1, 1);
		a.put(1, 2);
	}
}

Questions:

  1. What's stored in a Map?
  2. Can Map have duplicate key or value?

Comparable and Comparator

import java.util.List;
import java.util.ArrayList;
import java.util.Collections;

public class Demo{
	public static void main(String[] args) {
		Person a = new Person(19);
		Person b = new Person(20);
		Person c = new Person(18);

		List<Person> list = new ArrayList<Person>();

		list.add(a);
		list.add(b);
		list.add(c);

		System.out.println(list.get(0).age);

		Collections.sort(list);

		System.out.println(list.get(0).age);
	}
}

class Person implements Comparable<Person>{
	int age;

	Person(int i){
		this.age = i;
	}

	public int compareTo(Person a){
		return this.age - a.age;
	}
}

Questions:

  1. How do you understand sort?
  2. What does public int compareTo() stand for?
import java.util.List;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

public class Demo{
	public static void main(String[] args) {
		Person a = new Person(19);
		Person b = new Person(20);
		Person c = new Person(18);

		List<Person> list = new ArrayList<Person>();

		list.add(a);
		list.add(b);
		list.add(c);

		System.out.println(list.get(0).age);

		AgeCompare ageCompare = new AgeCompare();
		Collections.sort(list, ageCompare);

		System.out.println(list.get(0).age);		
	}
}

class Person{
	int age;

	Person(int i){
		this.age = i;
	}
}

class AgeCompare implements Comparator<Person>{
	public int compare(Person a, Person b){
		return a.age - b.age;
	}
}

Questions:

  1. What's the difference between Comparable and Comparator?
  2. For method public int compare(), what does positive, zero and negative return value stand for?