Popular Posts

May 28, 2012

HASHMAP VS HASHTABLE VS HASHSET

I wanted to know the different between hashtable and hashmap. And search from Google i  found another thing, that is hashset.
Here is the some basic diffences between them.


Hashtable:
Hashtable is basically a datastructure to retain values of key-value pair.

-> It didn't allow null for both key and value. You will get NullPointerException if you add null value.
-> It is synchronized. So it comes with its cost. Only one thread can access in one time.

Hashtable<Integer, String> cityTable = new HashTable<Integer,String>();
cityTable.put(1, "Dhaka");
cityTable.put(2, "Manikgonj");
cityTable.put(3, null); /* NullPointerException at runtime */

System.out.println(cityTable.get(1));
System.out.println(cityTable.get(2));
System.out.println(cityTable.get(3));

HashMap:
Like HashTable it also accepts key value pair.
->  It allows null for both key and value
->  It is unsynchronized. So come up with better performance

HashMap<Integer,String> productMap = new HashMap<Integer,String>();
productMap.put(1, "Keys");
productMap.put(2, null);



HashSet:
HashSet does not allow duplicate values.
-> It provides add method rather put method.
-> You also use its contain method to check whether the object is already available in HashSet.
HashSet can be used where you want to maintain a unique list.

HashSet<String> stateSet = new HashSet<String>();
stateSet.add ("CA");
stateSet.add ("WI");
stateSet.add ("NY");

if (stateSet.contains("PB")) /* if CA, it will not add but shows following message*/
     System.out.println("Already found");
else
    stateSet.add("PB");

May 6, 2012

Restart App(Android)


Intent i = getBaseContext().getPackageManager().getLaunchIntentForPackage(getBaseContext().getPackageName());
                    i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    startActivity(i);

Using Vibrator in android



-Add permission to android Manifest file
 <uses-permission android:name="android.permission.VIBRATE" />

- In java file:
 Vibrator vibrator;
- In Oncreage:
 vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);

- Use it:
 vibrator.vibrate(30);

Using Custom Dialog(ANdroid)


Java Code:

private void hintMethod(){

final Dialog dialog = new Dialog(GMTActivity.this);
            dialog.setContentView(R.layout.thinking_ui);
            dialog.setTitle(" Guess My THink!! ");
            dialog.setCancelable(false);
            dialog.setCanceledOnTouchOutside(false);
       
            thinkImageView = (ImageView) dialog.findViewById(R.id.thinkImageView);
            buttonTK = (Button) dialog.findViewById(R.id.buttonTK);
            thinkingTextView = (TextView) dialog.findViewById(R.id.thinkingTextView);
           
            thinkingTextView.setSingleLine(false);
            thinkingTextView.setText("I am thinking between \n"+minNumber+" and "+maxNumber);
       
           
            thinkImageView.setOnClickListener(new OnClickListener() {
               
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    dialog.dismiss();
                }
            });

           
            buttonTK.setOnClickListener(new OnClickListener() {
               
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    dialog.dismiss();
                }
            });
           
            dialog.show();

}

Xml file:

<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >


    <ImageView
        android:id="@+id/thinkImageView"
        android:layout_width="69dp"
        android:layout_height="58dp"
        android:layout_x="10dp"
        android:layout_y="1dp"
        android:src="@drawable/icon" />

    <TextView
        android:id="@+id/thinkingTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_x="82dp"
        android:layout_y="18dp"
        android:text="thinking 1 to 12" />

    <Button
        android:id="@+id/buttonTK"
        android:layout_width="96dp"
        android:layout_height="wrap_content"
        android:layout_x="76dp"
        android:layout_y="63dp"
        android:text=" Ok " />

</AbsoluteLayout>

Open website from your apps(Android)


              private final static String WEBSITE = "http://itsjubayer.blogspot.com";


               final Intent visit = new Intent(Intent.ACTION_VIEW);
                visit.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                visit.setData(android.net.Uri.parse(WEBSITE));
               
                // Use a thread so that the menu is responsive when clicked
                new Thread(new Runnable() {
                    public void run() {
                        startActivity(visit);
                    }
                }).start();

                return true;