Java SE

Optimized Character Escaper for Java

I wanted to write my own character escaper for requests going to Apache Lucent.
After writing my own, I also received feedback from programmers-stackexchange which resulted in the fastest implementation, also posted here. (Thanks to MrSmit42)

Below is my application that tests 6 different slightly different implementations over 30,000,000 executions each and outputs the time in MS.

The results in MS (on my computer) were the following:

Method 1 (Boolean Array)->    7574
Method 0 (Switch Stmt)->    9778
Method 4 (Switch Stmt Ugly – BreakPoints after evert case)->    12451
Method 6 (HashSet)->    17273
Method 5 (EnumMap)->    39516
Method 3 (Pattern/Regex)->    98229

This proves that for production systems that have many requests, jumping directly on the RegEx bandwagon for even trivial problems is not always the solution.
Even a 0,5 MS reduction per request leads to 50MS saved over 100 requests (per second) and 500MS over 1000 requests.

[polldaddy poll=7418939]

More

Why Java passes objects as references passed by value

I came across a question recently on stackoverflow that was very enlightening and proves how objects are actually passed in java. It was quite an interesting discovery!

I am quoting from here

 

Java is always pass-by-value. The difficult thing can be to understand that Java passes objects as references passed by value.

It goes like this:

public void foo(Dog d) {
  d.name.equals("Max"); // true
  d = new Dog("Fifi");
  d.name.equals("Fifi"); // true
}

Dog aDog = new Dog("Max");
foo(aDog);
aDog.name.equals("Max"); // true

In this example aDog.name will still be “Max”. “d” is not overwritten in the function as the object reference is passed by value.

Likewise:

public void foo(Dog d) {
  d.name.equals("Max"); // true
  d.setname("Fifi");
}

Dog aDog = new Dog("Max");
foo(aDog);
aDog.name.equals("Fifi"); // true

SO:Removing duplicate rows from data pulled from db in java

I’ve become an active member of stack overflow, both answering and making questions.
It’s amazing, there’s a whole community there that I hadn’t noticed before.
As I have not had time to post new items, I will be borrowing my answers to stackoverflow questions and re posting them here.


User Question:

So here’s my question. I have a giant text file of data and I need to input all of this data into a mySQL database fast through obviously using a java program. My only problem is that, the data is identified by a certain ID. Some of these ID’s have duplicates and contain all the same info as eachother. I would like to remove all of these for sorting purposes and clarity sake.

What would be the best way to go about this? If anyone could help I’d appreciate it

Answer:

clones/duplicates

clones/duplicates

While reading the data have a hashmap or hashset. check if the id exists in the hasmap/hashset and if so continue. otherwise enter in set/map and insert.

An aside: The difference between hashmap and hashset is hashset only takes values while hashmap takes key values. However, Hashset itself uses a hashmap within memory and just inserts a dummy object for values. See: Differences between HashMap and Hashtable?

More

Add new JSON document using plain text string with LightCouch

Hey,

Small post outlining a small modification to the 0.4 lightcouch source to allow direct plain text json insertion. A colleague and I had a JSON string which we wanted to insert into lightcouch without having to create a JSON object – only to have .toString() be called during the final POST request.

More

Data insertion to CouchDB using Lightcouch lib in Java (Sample)

Introduction

Hello, a small sample on how to insert JSON data to couchDB.

First off there are MANY java libraries for accessing couchdb. For information on which one to choose for you the 2 links below may help:
1) http://stackoverflow.com/questions/1105807/which-java-library-libraries-for-couchdb
2) http://wiki.apache.org/couchdb/Getting_started_with_Java

In this post we made use of LightCouch as we wanted something relatively simple without object mappings and other such features.

Aside: an also interesting tutorial utilizing couchdb4j with description of sessions is: http://www.drdobbs.com/jvm/223100116

More