January 30, 2018

Simple example for break and continue in java

Simple example for break and continue in java




/**
 *
 */
package com.belazy.generics;

/**
 * @author belazy
 *
 * How break and continue works in java
 *
 */
public class ControlStructures {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

int [] numbers = {1,2,3,4,5,6,7,8,9};

System.out.println("\n--- break ------------ ");

for(int number :numbers){

if(number ==5)
break;

System.out.print(number +" ");
}

System.out.println("\n--- continue ------------ ");

for(int number :numbers){

if(number ==5)
continue;

System.out.print(number +" ");
}


}

}

Output




--- break ------------
1 2 3 4
--- continue ------------
1 2 3 4 6 7 8 9




January 23, 2018

To remove a value from arraylist without iterating

/**
 * To remove a primitive data type for an Arraylist
 * without out iterating all the elements
 */
package com.belazy.generics;

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

/**
 * @author belazy
 *
 */
public class ArrayListRemoveSample {

/**
* @param args
*/
public static void main(String[] args) {

List<Integer> numberList = new ArrayList<Integer>();
numberList.add(1);
numberList.add(11);
numberList.add(2018);
numberList.add(911);
numberList.add(25);

System.out.println("--- before ---");
for(Integer thisNumber : numberList){
System.out.print(" "+thisNumber+" ");
}


numberList.remove(new Integer(911));

System.out.println("\n--- After ---");
for(Integer thisNumber : numberList){
System.out.print(" "+thisNumber+" ");
}
}

}

January 16, 2018

algorithm to split overlapping and merging consecutive dates in java

split overlapping and merging consecutive dates


/**
 * Conditions.java
 * Jan 14, 2018
 */
package com.belazy.algorithms;

/**
 * @author Steffi Thomas
 *
 */
public class Conditions {

private long startDate = 0;
private long endDate = 0;
private double amount = 0;

/**
*
*/
public Conditions(long startDate, long endDate, double amount) {
// TODO Auto-generated constructor stub
this.startDate = startDate;
this.endDate = endDate;
this.amount = amount;
}

public long getStartDate() {
return startDate;
}
public void setStartDate(long startDate) {
this.startDate = startDate;
}
public long getEndDate() {
return endDate;
}
public void setEndDate(long endDate) {
this.endDate = endDate;
}
public double getAmount() {
return amount;
}
public void setAmount(double amount) {
this.amount = amount;
}


}


DateSplit.java


/**
 * DateSplit.java
 * Jan 16, 2018
 */
package com.belazy.algorithms;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

/**
 * @author belazy
 *
 */
public class DateSplit {

/**
* TODO Jan 16, 2018
* @param args
*            void
*
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

List<Conditions> cancelConditions = new ArrayList<Conditions>();
cancelConditions.add(new Conditions(1, 8, 100));
cancelConditions.add(new Conditions(1, 8, 120));
cancelConditions.add(new Conditions(9, 12, 110));
cancelConditions.add(new Conditions(13, 20, 120));
cancelConditions.add(new Conditions(21, 30, 130));
cancelConditions.add(new Conditions(9, 18, 130));
cancelConditions.add(new Conditions(19, 30, 140));
cancelConditions.add(new Conditions(7, 18, 130));

Map<Long, Double> treeMap = new TreeMap<Long, Double>();
for(Conditions c :cancelConditions){
if(treeMap.containsKey(c.getEndDate())){
treeMap.put(c.getEndDate(), c.getAmount()+treeMap.get(c.getEndDate()));
}else{
treeMap.put(c.getEndDate(), c.getAmount());
}
}

System.out.println(" cancelCondition before : " + cancelConditions.size());

DateSplit d = new DateSplit();
d.splitDate(cancelConditions, treeMap);

}

/**
* TODO Jan 16, 2018
* @param cancelConditions
*            void
* @param treeMap2
*
*/
private void splitDate(List<Conditions> cancelConditions, Map<Long, Double> treeMap) {

for (long endDate : treeMap.keySet()) {
for (Conditions cond2 : cancelConditions) {
if (endDate > cond2.getStartDate() && endDate < cond2.getEndDate()) {
if (treeMap.containsKey(endDate)) {
double mapAmt = treeMap.get(endDate);
treeMap.put(endDate, cond2.getAmount() + mapAmt);
} else {
treeMap.put(endDate, cond2.getAmount());
}
}
}
}

System.out.println(" map : " + treeMap);

long startDate = 0;
long nextDate = 0;
for (Map.Entry<Long, Double> entry : treeMap.entrySet()) {
Long key = entry.getKey();
Double value = entry.getValue();
startDate = nextDate + 1;
nextDate = key;
System.out.println("startDate : " + startDate + " endDate : " + nextDate + " amount -->" + value);

}

}

}


Output



Thanks to @steffi for her support

Facebook comments