Recepty, články, nápady, programování. Dříve dum-zahrada, finance, internet a know-how.okhelp.cz Pro lepší výsledky hledání používejte i diakritiku.

Map TreeMap key value pair sort by key, using of iterator Java Android example


Map TreeMap key value pair, Map sort by key, Iterator for Map Java Android example.


import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

public class MainClass {
	public static void main(String[] arg) {
		// english;germany dictionary
		String[] arrayOfString = { "one;eine", "two;zwei", "three;drei", "four;vier" };

		Map<String, String> map = new TreeMap<String, String>();
		for(String s: arrayOfString){
	    	String[] array = s.split(";");
	    	String sKey ="", sValue="";
	    	if(array.length > 1){
	    	sKey = array[0]; sValue = array[1];
	    		map.put(sKey, sValue);
		List sortedByKeys=new ArrayList(map.keySet());
            // iterate map
	    Set references = map.keySet();
	    Iterator it = references.iterator();
	    while (it.hasNext()) {
	      String key = (String) it.next();
	      String value = map.get(key);
	      System.out.println(key + " = " + value);
            // or other example how iterate map
	    TreeSet<String> keys = new TreeSet<String>(map.keySet());
	    for (String key : keys) { 
	       String value = map.get(key);
	       System.out.println(key + " = " + value);

       // check if key exists 	    
//	   if( map.containsKey("two")){
//		System.out.print("two = " + map.get("two"));
//	   }
four = vier
one = eine
three = drei
two = zwei

397LW NO topic_id


Další témata ....(Topics)


Eclipse Escape text when pasting into a string literal | eclipse-escape-text-when-pasting-into-a-string-literal

Window>Preference>Java>Editor>Typing and check the "Escape text when pasting into a string literal".

Get Context Java Android example | get-context-java-android-example

getContext() getApplicationContext() method Java Android example source

Context myContext_1 = ThisClassName.this; // to open a Dialog
Context myContext_2 = getContext();
Context myContext_3 = this.getContext();
Context myContext_4 = this;
Context myContext_5 = this.getApplicationContext ();

OnClickListener getImageBtnOnClick = new OnClickListener() {
public void onClick(View view) {
Context context = view.getContext();

// Toast
Toast.makeText(getApplicationContext(), "Context == getApplicationContext "
, Toast.LENGTH_SHORT).show();

// store Context in public class
public class MyActivity extends Activity {
public static Context myCnt = null;
protected void onCreate(Bundle icicle) {
myCnt = this;
// or
// MyStorage.setContext(this);

// cntxFromStorage == this
Context cntxFromStorage = MyStorage.getContext();

public class MyStorage
private static Context cntStorageContext = null;

public static Context getContext() {
return cntStorageContext;

public static void setContext(Context context) {
MyStorage.cntStorageContext = context;


class DataBaseHelper extends SQLiteOpenHelper {
// get MyActivity context
Context cnt = MyStorage.getContext();



Delete Bitmap Android Example | delete-bitmap-android-example

If some memory leak - problem try release of memory used of a big Bitmap what already not to need;
Bitmpap bmp; // not null
bmp = null;

final boolean bmpIsRecycled = bmp.isRecycled()
// Returns true if this bitmap has been recycled.

Free the native object associated with this bitmap, and clear the reference to the pixel data. This will not free the pixel data synchronously; it simply allows it to be garbage collected if there are no other references. The bitmap is marked as "dead”, meaning it will throw an exception if getPixels() or setPixels() is called, and will draw nothing. This operation cannot be reversed, so it should only be called if you are sure there are no further uses for the bitmap. This is an advanced call, and normally need not be called, since the normal GC process will free up this memory when there are no more references to this bitmap.

Set Custom Font to View Android Example | set-custom-font-to-view-android-example

You have to put your font fonts/samplefont.ttf into assets/fonts folder in your project!

// set own custom font from assets
TextView txt = (TextView) findViewById(R.id.custom_font);
Typeface mFace = Typeface.createFromAsset(getContext().getAssets(),

Screen Android Landscape Portrait programmatically | screen-android

Screen Android example source code for developers.
Get orientation of screen.

 public int getScreenOrientation() {
  if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT)                {
       return 1; // Portrait Mode
  }else if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
       return 2;   // Landscape mode
  return 0;

Editace: 2013-12-09 13:29:38
Počet článků v kategorii: 397

11 Share