Skip to main content

Posts

Showing posts with the label Code

Best Way to use ArrayList in Java

                      I n the following post we are goning to discuss a little variation in use of ArrayList in java. We will see how we can improve the performance by just a small change. First of all run the below java code on your machine. import java.util.ArrayList; import java.util.List; public class Test{ public static void main(String... args) { int s = 10000000; String v = "PROGRAMMING"; long t1,t2; //Segment-1 t1 = System.currentTimeMillis(); List<String> l1 = new ArrayList<>(); for(int i=0; i<s; ++i){ l1.add(v); } t2 = System.currentTimeMillis(); System.out.println("Time for above segment-1 : "+(t2-t1)+" ms"); //Segment-2 t1 = System.currentTimeMillis(); List<String> l2 = new ArrayList<>(s); for(int i=0; i<s; ++i){ l2.add(v); } t2 = System.currentTimeMillis(); System.out.println("Time for abo...

Java Code Example For Aerospike Simple Operations [Read, Write, Delete]

                      I n the following post we will see sample java code to perform simple operation (read, write and delete Records) from Aerospike Database System. The code are self explanatory, follow the comments in code for better understanding. You only need a jar for java client which you can download from aerospike official website. Main Class import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import com.aerospike.client.AerospikeClient; import com.aerospike.client.AerospikeException; import com.aerospike.client.Bin; import com.aerospike.client.Host; import com.aerospike.client.Key; import com.aerospike.client.Record; import com.aerospike.client.Value; import com.aerospike.client.policy.ClientPolicy; import com.aerospike.client.policy.WritePolicy; public class Aerospike { public static void main(String... args){ ...

Observer Pattern : Real Life Example and Java Code Example

Observer Pattern The observer pattern defines a one to many relation between objects so that when one object changes state, all of its dependent or related objects get notified automatically Real Life Example: Group Chat is a real life example of observer pattern. Whenever someone type some text in text button and hit send button. Every member get notified that someone texted in group. Java Code Example: public interface Subject { void register(Observer observer); void remove(Observer observer); void notifyRespectedObserver(String msg); } public interface Observer { void update(String msg); String write(String msg); String getName(); } public class Console implements Subject { public List memberList; public Console(){ memberList = new LinkedList (); } public void register(Observer observer){ memberList.add(observer); } public void remove(Observer observer){ memberList.remove(observer); } public void notifyRespectedObserver(String msg){ for...

Design Pattern : Real Life Example and Java Code Example

                      I n software engineering, a design pattern is a general reusable solution to a commonly occurring problem within a given context in software design. A design pattern is not a finished design that can be transformed directly into the source or machine code. It is a description or template for how to solve a problem that can be used in many different situations. - Wiki To explore the various kind of Design Patterns follow the below links. ----------------------------------------------------------------------------------------------------------------- Observer Pattern ----------------------------------------------------------------------------------------------------------------- Proxy Pattern ----------------------------------------------------------------------------------------------------------------- Builder Pattern ------------------------...

Template for C++ Program

C++ is a very important language in competitive programming because C++ include flexibility of C and have extended STL template library.You can use C++ easy and and quick way if you have a template file. A template file contains a format and necessary code which is required frequently in competitive programming.You can save a lot of time by using template file because it takes some time if you have to include another header and define some more macros. Here I am giving a simple template file according to my convenience.You can edit it or generate another file according to yours. #include<iostream> #include<cstdio> #include<bitset> #include<vector> #include<cmath> #include<queue> #include<map> #include<set> #include<list> #include<stack> #include<string> #include<cstdlib> #include<cstring> #include<algorithm> using namespace std; #define fori(i,n) for(i=0;i<(n);++i) #define fo...