Posts

Can we override static method in java ?

No ! we cannot override the static methods. Since static method belongs to class level not object level. class StaticExe1 { public static void run() { System.out.println("run of StaticExe1"); } public  void fly() { System.out.println("fly of non-static Exe1"); } } class StaticExe2 extends StaticExe1 { public static void run() { System.out.println("run of StaticExe2"); } public  void fly() { System.out.println("fly of non-static Exe2"); } } class StaticExe3 extends StaticExe2 { public static void run() { System.out.println("run of StaticExe3"); } public  void fly() { System.out.println("fly of non-static Exe3"); } } public class StaticMethod { public static void main(String[] args) { StaticExe2 ex = new StaticExe3 (); ex.run(); ex.fly(); } }

Sort a Map by value !!

Sorting a Map by value in Java: import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.Set; public class SortMapByValue { public static void main(String[] args) { Map<String, Integer> map = new HashMap<String, Integer>(); map.put("z", 2); map.put("q", 0); map.put("p", 5); map.put("s", 8); map.put("o", 0); map.put("r", 9); map.put("v", 8); System.out.println(map); Set<Entry<String, Integer>> entrySet = map.entrySet(); List<Entry<String, Integer>> lists = new ArrayList<Entry<String, Integer>>(entrySet); Collections.sort(lists, new Comparator<Entry<String, Integer>>() { @Override public int compare(Entry<String, Integer> o1, Entry<String, Integer> o...

Java single field sort using Comparator.

import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.List; class SortBySalary implements Comparator<Human> { @Override public int compare(Human o1, Human o2) { if (o1.salary > o2.salary) return 1; if (o1.salary < o2.salary) return -1; return 0; } } class Human { String name; int age; double salary; public Human(String n, int a, double s) { this.name = n; this.age = a; this.salary = s; } public Human() { } public String toString() { return this.name + " " + this.age + " " + this.salary; } } public class ComparatorExe { public static void main(String[] args) { List<Human> l = new ArrayList(); Human h1 = new Human("ritesh", 12, 6000); Human h2 = new Human("raju", 18, 5000); Human h3 = new Human("ishaan", 10, 7000); Human h4 = new Human("yuyu", 15, 333); Human h5 = new Hu...

Java multiple way sorting - Comparator example .

Compare multiple fields using Comparator: import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.List; class SortByAnimalByName implements Comparator<Animal> { @Override public int compare(Animal o1, Animal o2) { return o1.name.compareTo(o2.name); } } class SortByAnimalByColor implements Comparator<Animal> { @Override public int compare(Animal o1, Animal o2) { return o1.color.compareTo(o2.color); } } class SortByAnimalByWeight implements Comparator<Animal> { @Override public int compare(Animal o1, Animal o2) { if (o1.weight > o2.weight) { return 1; } if (o1.weight < o2.weight) { return -1; } return 0; } } class SortByAllAnimal implements Comparator<Animal> { List<Comparator<Animal>> com; SortByAllAnimal(Comparator<Animal>... all) { this.com = Arrays.asList(all); } @Override public...

What is the output of the program ?

class SampleExec1  { void run() { System.out.println("SampleExec1 run"); } } class SampleExec2 extends SampleExec1 { void run() { System.out.println("SampleExec2 run"); } } public class SampleExecution { public static void main(String[] args) { SampleExec2 ex = new SampleExec1(); ex.run(); } } Output : compile time error at line 15. Type mismatch: cannot convert from SampleExec1 to SampleExec2

Write a program to find maximum profit

public class MaxProfit {   public static void main(final String[] args) {     int a[] = { 4, 1, 10, 2, 3, 5, 6, 25, 48, 8, 26 };     int max = a[a.length - 1];     int min = a[a.length - 1];     int indexOfMax = a.length - 1;     int indexOfMin = a.length - 1;     int dif = -1;     for (int i = a.length - 2; i >= 0; i--) {       if (max < a[i]) {         max = a[i];         indexOfMax = i;       }       if (max > a[i]) {         min = a[i];         indexOfMin = i;       }       if ((indexOfMax > indexOfMin)) {         if (dif < (max - min)) {  ...

Write a program to count each unique words in a given string.

public class WordCountByMapAlgoritham { public static void main(String[] args) { String s = "this is java code. this java code is highly recommended for java developer !!"; String a[] = s.split(" "); Map<String, Integer> map = new HashMap<String, Integer>(); for (String word : a) { if (map.get(word) != null) { map.put(word, map.get(word) + 1); } else map.put(word, 1); } System.out.println(map); } } Output:  {!!=1, java=3, code=1, code.=1, this=2, for=1, is=2, developer=1, highly=1, recommended=1}