Thursday, 20 October 2022

Chapter 1 , Core Java by Cay Horstmann

An Introduction To Java

Java is not just a programming language, but an entire platform of libraries, execution environment - security, portability, automatic garbage collection.

Brief history:

1991 : Java founders at Sun Microsystems want to create a language suitable for consumer devices(cable) - so it has to be small and also platform neutral.

1994 : Mosaic browser - need for a language like java. Sun creates HotJava browser to show off java and the craze begins.

1996: Java 1.0 - no print

1997: 1.1 - refelection, GUI event model, inner classes

1998: 1.2 - SE, ME(embedded devices), EE

2000-2002: 1.3-1.4 - libraries, performance

2004: 1.5-> 5 - for each, loops, autoboxing, annotations, enums, static import

2006: 6 - libraries, performance

2009: Oracle buys Sun

2011: 7 - switch string, diamond op

2014: 8 - functional style prog, lambda expressions, streams, interfaces with default methods

2017: 9 - modules

2018: 11- var

2021: 17-records


Buzz words:

Simple : cleaned up C++, small(embedded devices)

Object oriented : data=objects, interfaces to objects

Distributed: access objects across net(HTTP/FTP)

Robust: compile time and run time checks, memory cant be overwritten and corrupted

Secure: secure over a network

Architecture neutral : code compiled to bytecode that can run anywhere

Portable: No implementation dependency, datatypes size doesnt vary as in C++

Interpreted : executes bytecode - fast

High performance: frequently executed bytecode to machine code - hotspots - Just In Time Compiler

Multithreaded: fast and real time

Dynamic: changing libraries without impacting dependent code, adding code to running programs

~

Friday, 9 September 2022

Comparable and Comparator

Java supports sorting on collection in two ways - through Comparable and Comparator(both are functional interfaces with one single abstract method).

Comparable:

To sort collection based on Comparable:

  • Elements of the collection must implement Comparable interface and implement it's method "int compareTo(Object o1)".
  • Then sorting can be done using Collections.sort(List)

Cons:

  • Can't support multiple types of sorting, as one compareTo method can have only one implementation.
  • May not have access to the element class, in which case, subclassing the element and implementing compareTo would be the only option.

Code:

public class Dog implements Comparable<Dog>{
      String name;
      String breed;

      public Dog(String name,String breed){
            this.name = name;
            this.breed = breed;
      }
        @Override
public int compareTo(Dog o) {
return name.compareTo(o.name);
}
}
...

List<Dog> dogList = new ArrayList<Dog>();
Collections.sort(dogList);
...


Comparator:

To sort collection based on Comparator:

  • A class/anonymous class implements Comparator interface and implements it's method "int compare(Object o1, Object o2). 
  • Then sorting can be done by passing the object of the above implmented Comparator - using Collection.sort(List, Comparator).

Pros:

  • Can support multiple sorting implementations.
  • Don't need to have access to elements of the collection.

Code:

public class DogBreedComparator implements Comparator<Dog> {
@Override
public int compare(Dog o1, Dog o2) {
return o1.breed.compareTo(o2.breed);
}
}
..

Collections.sort(dogList, new DogBreedComparator());
..
Lambda expression:
Collections.sort(dogList, (o1,o2)->o1.breed.compareTo(o2.breed));
..

Both compareTo and compare methods return integer - 
1  : if passed in object is greater than current object
-1: if passed in object is lesser than current object
0: if both are equal

~

Monday, 24 August 2020

Array Rotation - Three Solutions

Array rotation by k places can be done by using extra space of k sized array, by shifting array by one position in loop of k, by shifting array by gcd(array.length, k) in gcd number of sets. Following program has all the implementations:



Saturday, 1 August 2020

Top K Frequent Elements - Hashing Problem

Problem : Get top K frequent elements.
Implementation:



Sort By Frequency - Hashing Problem

Problem : Sort elements by their frequency.
Following is the implementation where we store the frequency in hashmap:



Count Distinct Elements In Every Window - Hashing Problem

Problem: Count distinct elements in every window of given K size.
Following is the implementation, which rotates the array window:





Print Non-repeated Elements - Hashing Problems

Problem : Print the non-repeated elements of an array.
Following is the implementation: