Home » Posts tagged 'daily-learning'
Tag Archives: daily-learning
Java Puzzler : They just find me !!
Couple of days back I wrote a piece of code which was behaving in an unexpected manner. I was confused what was happening. Take a look at the sample code below and predict its behavior
package com.shekhar;
public class JavaPuzzler {
public static void main(String[] args) {
JavaPuzzler javaPuzzler = new JavaPuzzler();
javaPuzzler.doSth();
}
public void doSth() {
float f = 1.2f;
if (f >= 1.0) {
f = 0.9999999999999f;
}
InnerClass innerClass = new InnerClass(f);
System.out.println(innerClass.getValue());
}
private class InnerClass {
private float value;
public InnerClass(float value) {
if (value >= 1.0f) {
throw new IllegalArgumentException(
"Value can't be greater than 1.0f");
}
this.value = value;
}
public float getValue() {
return value;
}
}
}
My initial expectation was that I would get value 0.9999999999999f as answer.Try it and find the answer. Share your answer and reasoning in comments.
Finding all the indexes of a whole word in a given string using java
Intent
To find all the indexes of a whole word in a given searchable string.
Motivation
Today, I had to write a piece of code in which I had to find all the indexes of a particular keyword in a searchable string. Most of the times, when we have to find index of a keyword in a searchable string we use indexOf method of String class.
For example,
String searchableString = “Don’t be evil. Being evil is bad”; String keyword = “be”;
So, we can find the index of keyword as
int index = searchableString.indexOf(keyword);
This will give us the index of first occurrence of keyword (“be”). Now suppose, we have to find all the indexes of keyword (“be”), we will have to loop over searchableString and find all the indexes of keyword “be”.
This can be done as follows
public static void findIndexes(){
String searchableString = "don’t be evil.being evil is bad";
String keyword = "be";
int index = searchableString.indexOf(keyword);
while (index >=0){
System.out.println("Index : "+index);
index = searchableString.indexOf(keyword, index+keyword.length()) ;
}
}
public static void main(String[] args) {
findIndexes();
}
This program will print :
Index : 6
Index : 14
There is a problem in this code as we should not get “Index : 14” because we are looking for a keyword “be” and the index it has found is from word “being” . This just looks for any occurrence of keyword in the string it does not have to be a whole word. To solve this problem, we will be writing a solution using Regular expressions.
Solution
We have to find out the indexes which correspond to the whole word. For example, we should only get “Index : 6” as that corresponds to the keyword “be” not “Index : 14″ as it corresponds to the word “being” .
This can be done as follows :-
WholeWordIndexFinder.java
package org.dailywtf.string;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class WholeWordIndexFinder {
private String searchString;
public WholeWordIndexFinder(String searchString) {
this.searchString = searchString;
}
public List<IndexWrapper> findIndexesForKeyword(String keyword) {
String regex = "\\b"+keyword+"\\b";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(searchString);
List<IndexWrapper> wrappers = new ArrayList<IndexWrapper>();
while(matcher.find() == true){
int end = matcher.end();
int start = matcher.start();
IndexWrapper wrapper = new IndexWrapper(start, end);
wrappers.add(wrapper);
}
return wrappers;
}
public static void main(String[] args) {
WholeWordIndexFinder finder = new WholeWordIndexFinder("don’t be evil.being evil is bad");
List<IndexWrapper> indexes = finder.findIndexesForKeyword("be");
System.out.println("Indexes found "+indexes.size() +" keyword found at index : "+indexes.get(0).getStart());
}
}
IndexWrapper.java
package org.dailywtf.string;
public class IndexWrapper {
private int start;
private int end;
public IndexWrapper(int start, int end) {
this.start = start;
this.end = end;
}
public int getEnd() {
return end;
}
public int getStart() {
return start;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + end;
result = prime * result + start;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
IndexWrapper other = (IndexWrapper) obj;
if (end != other.end)
return false;
if (start != other.start)
return false;
return true;
}
}
Explanation
First of all a regular expression is built using the keyword. Regex \b defines a word boundary. \b allows you to perform a “whole words only” search using a regular expression in the form of \bword\b.
More information on regex could be found at this link. Next, we just iterate over the matcher till matcher can find any match. Whenever a match is found an wrapper object is created which contains the start and end position of the keyword.
Using the above code, we can find all the indexes of a keyword in a searchable string.
HQL which gives number of hours between two timestamps
Intent
To write down a hql (hibernate query language) which gives the difference between two timestamps.
Motivation
Today, i had to write down a hql which should return me the difference between two timestamps. It took me quite a long time to write down the hql query which gives me the correct result. So, i am writing down hql in this blog in order to help any developer who might face this problem.To explain the problem, suppose we have to find out the number of hours before which a user was last updated. The problem seemed quite easy when I first thought about the solution. The most obvious solution i thought was to use hour() function of hql. So, I wrote down the hql
select hour(current_timestamp – user.lastUpdatedTimeStamp) from com.test.User as user where user.id=1;
It worked but only when both the timestamps are of the same date i.e. if the current_timestamp and lastUpdateTimeStamp are of same date (18th April 2010). This solution does not work if the timestamp are on different date as hour function gives number of hours based on time only, it does not consider dates. So, if current_timestamp is 19-04-2010 11:00:00 and lastUpdatedTimeStamp is 18-04-2010 5:00:00 , it will give answer as 6 which is wrong as number of hour between these timestamps is 30.
Solution
HQL which works is
select
(days(current_timestamp) *24 + hour(current_timestamp)) -( days(user.lastUpdatedTimeStamp)*24 + hour( user.lastUpdatedTimeStamp))
from com.test.User as user where user.id=1;
This hql will give the correct answer as it will add the number of hours corresponding to the date to the number of hours on that particular date.
Mathematically, this will be like this
((19*24 + 11) -(18*24 +5)) == 467 – 437 = 30
So, we will get the correct answer 30.