Core java - Advance Topics
  • Welcome
  • Schedule
  • 1) Exception Handling
    • 1) Introduction to Exception Handling
    • 2) Categories of Exceptions
    • 3) Creating a method that throws an exception
    • 4) Creating Custom Exception Classes
    • 5)What happens when an exception is thrown?
      • 5.1) Creating try-catch-finally blocks
      • 5.2) Using a method that throws a checked exception
      • 5.3) Using a method that throws a runtime exception
      • 5.4) Using a method that throws an error
      • 5.5) Will a finally block execute even if the catch block defines a return statement?
      • 5.6) What happens if both a catch and a finally block define return statement?
      • 5.7) What happens if a finally block modifies the value returned from a catch block?
      • 5.8) Can a try block be followed only by a finally block?
      • 5.9) Does the order of the exceptions caught in the catch blocks matter?
      • 5.10) Can I rethrow an exception or the error I catch?
      • 5.11) Can I declare my methods to throw a checked exception instead of handling it?
      • 5.12) I can create nested loops, so can I create nested try-catch blocks too?
      • 5.13) Should I handle errors?
    • 6) Best Practices
    • 7) Cheat Sheet
    • 8) Problems
  • 2) Wrapper Classes and Enums
    • 2.1) Creating objects of the wrapper classes
    • Enums
  • 3) Inner Classes
    • 3.1) Static nested class (also called static inner class)
    • 3.2) Inner class (also called member class)
    • 3.3) Anonymous inner class
    • 3.4) Method local inner classes
    • CheatSheet
  • 4) Generics
    • Multiple Type parameters in Generic classes
    • Inheritance using Generics
    • Generic interfaces
    • Generic Methods
    • Bounded type parameters
    • Applications
  • 5) Equals and Hashcode
    • Problems
  • CompareTo method overview
  • Basic DS
    • 1) Simple Array List
    • 2) Simple HashMap
  • 5) Collections Framework - Part 1
    • Introducing the collections framework
    • Working with the Collection interface
      • The core Collection interface
      • Methods of the Collection interface
    • Creating and using List, Set, and Deque implementations
      • List interface and its implementations
      • Iterators
      • Sorting List using custom sorting technique
      • Comparable Interface
      • Custom Sorting using comparator
      • ArrayList - Examples and practice problems
    • Stack
    • Linked List
    • LinkedList Operations
  • 6) Collections Framework - Part 2
    • Sets
      • Set Types
      • Array to Set (vice versa)
    • Maps
    • TreeMap
    • Autoboxing And Unboxing
  • Collections Framework - Part 3
    • Basics : DS , Number System
    • Internal Working
      • HashMap
      • HashSet
  • 7) Reflection API
  • 8) Annotations
  • 9) Reading Input From Various Sources
    • File Handling
    • Reading From Xml
    • Reading From JSON
  • 10) Multi-threading (Concurrency)
    • Protect shared data
    • Thread-safe access to shared data
  • 11) Design Patterns
    • Singleton
    • DI
  • 12) Internal Working of JVM
  • 13) Garbage Collection
  • 14) More on Strings (Buffer and Builder)
  • 15) Cloning and Immutable Class
    • 16) Serialization And Deserialization
    • Untitled
  • JAVA 8
    • Interface Changes
    • Lambda
    • Method Ref
    • Optional
    • Streams
    • Predicates
  • Practice Tests
    • Test - Collections
    • OOPS
    • S-OOPS
Powered by GitBook
On this page

Was this helpful?

  1. 4) Generics

Applications

LL

package com.gs.corejava.datastructures;

public class MySinglyLinkedList<T> {

	private class MyNode<E extends Object> {
		E data;
		MyNode<E> next = null;

		public MyNode() {
		}

		public MyNode(E data) {
			this.data = data;
		}
	}

	private MyNode<T> start;
	private int size = 0;
	private MyNode<T> currentNode;

	public MySinglyLinkedList() {
		start = new MyNode<T>();
		start.next = null;
	}

	public void add(T data) {
		MyNode<T> myNode = new MyNode<T>(data);
		MyNode<T> head = start;
		MyNode<T> prev = start;
		if (null == currentNode) {
			while (null != head) {
				prev = head;
				head = head.next;
			}
			currentNode = myNode;
			prev.next = currentNode;
		} else {
			currentNode.next = myNode;
			currentNode = myNode;
		}
		size++;
	}

	public void addFirst(T data) {
		MyNode<T> myNode = new MyNode<T>(data);
		MyNode<T> head = start;
		myNode.next = head.next;
		head.next = myNode;
		size++;
	}

	public void addLast(T data) {
		add(data);
	}

	public void add(T data, int index) {
		MyNode<T> myNode = new MyNode<T>(data);
		MyNode<T> head = start;
		MyNode<T> prev = start;
		int currentIndex = 0;
		while (null != head && currentIndex <= index) {
			prev = head;
			head = head.next;
			currentIndex++;
		}
		myNode.next = prev.next;
		prev.next = myNode;
		size++;
	}

	public MyNode<T> remove(T data) {
		MyNode<T> node = new MyNode<T>(data);
		MyNode<T> head = start;
		MyNode<T> prev = start;
		while (null != head) {
			if (null != node.data && node.data.equals(head.data)) {
				break;
			}
			prev = head;
			head = head.next;
		}
		prev.next = head.next;
		head = null;
		size--;
		return node;

	}

	public MyNode<T> remove(int index) {
		MyNode<T> head = start;
		MyNode<T> prev = start;
		int currentIndex = 0;
		while (null != head && currentIndex <= index) {
			prev = head;
			head = head.next;
			currentIndex++;
		}
		MyNode<T> myNodeToReturn = head;
		prev.next = head.next;
		head = null;
		size--;

		return myNodeToReturn;

	}

	public Object[] toArray() {
		Object[] array = new Object[size];
		int k = 0;
		MyNode<T> head = start.next;
		while (null != head) {
			array[k] = head.data;
			k++;
			head = head.next;
		}
		return array;

	}

}
package com.gs.corejava.datastructures;

public class TestLinkedList {

	public static void main(String[] args) {

		testIntegerLinkedListAdd();

		System.out.println("---for String objects---");

		testStringLinkedListAddMethod();

		System.out.println("---for Custom objects like Teacher----");

		testCustomObjLinkedListAdd();
	}

	/**
	 * 
	 */
	private static void testIntegerLinkedListAdd() {
		MySinglyLinkedList<Integer> linkedList = new MySinglyLinkedList<Integer>();

		linkedList.add(123);
		linkedList.add(456);
		linkedList.add(454);
		linkedList.add(224);

		linkedList.add(-80, 2);

		linkedList.addFirst(12);

		linkedList.remove(new Integer(454));
		linkedList.remove(2);

		Object[] arr = linkedList.toArray();
		for (int i = 0; i < arr.length; i++) {
			System.out.println(arr[i]);
		}
	}

	/**
	 * 
	 */
	private static void testCustomObjLinkedListAdd() {
		// ---------for Custom objects like Teacher-----------
		MySinglyLinkedList<Teacher> listOfTeacher = new MySinglyLinkedList<Teacher>();
		Teacher teacher1 = new Teacher(1, "Mohit");
		Teacher teacher2 = new Teacher(2, "Sumit");
		Teacher teacher3 = new Teacher(3, "Sahil");
		Teacher teacher4 = new Teacher(4, "Naveen");
		listOfTeacher.add(teacher1);
		listOfTeacher.add(teacher2);
		listOfTeacher.add(teacher3);
		listOfTeacher.add(teacher4);
		Object[] arrayOfTeacher = listOfTeacher.toArray();
		Teacher teacherObj;
		for (int i = 0; i < arrayOfTeacher.length; i++) {
			if (arrayOfTeacher[i] instanceof Teacher) {
				teacherObj = (Teacher) arrayOfTeacher[i];
				System.out.println(teacherObj);
			}
		}
	}

	/**
	 * 
	 */
	private static void testStringLinkedListAddMethod() {
		// -------for String objects-------
		MySinglyLinkedList<String> listOfNames = new MySinglyLinkedList<String>();
		listOfNames.add("Mohit");
		listOfNames.add("Rohit");
		listOfNames.add("Sumit");
		listOfNames.add("Amit");
		Object[] arrayOfNames = listOfNames.toArray();
		for (int i = 0; i < arrayOfNames.length; i++) {
			System.out.println(arrayOfNames[i]);
		}
	}

}

class Teacher {
	private int id;
	private String name;

	/**
	 * @param id
	 * @param name
	 */
	public Teacher(int id, String name) {
		super();
		this.id = id;
		this.name = name;
	}

	@Override
	public String toString() {
		return "Teacher [id=" + id + ", name=" + name + "]";
	}

}
PreviousBounded type parametersNext5) Equals and Hashcode

Last updated 6 years ago

Was this helpful?