Java 在Java中遍历Map(HashMap)

//HashMap<Key, Value> map = new HashMap<Key, Value>();
for (Key key : map.keySet()) {
    Value value = map.get(key);
}

Java 获取活动执行位置

try {
	File jarFile = new File(Main.class.getProtectionDomain().getCodeSource().getLocation().toURI());
} catch (URISyntaxException e) {
	//do some error handling here
}

Java Twitter直接消息的Twidroid公共意图

final Button button2 = (Button) findViewById(R.id.senddirect);
		button2.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				Intent intent = new Intent("com.twidroid.SendDirect");
				
				/* set recipient of direct message */
				intent.putExtra("com.twidroid.extra.TO",
				"androidev");
				
				/* set direct message content */
				intent.putExtra("com.twidroid.extra.MESSAGE",
						"This is a test from Twidroid public intent.");
				
				try {
					startActivityForResult(intent, 1);
				} catch (ActivityNotFoundException e) {
					/* Handle Exception if Twidroid is not installed */
					TextView text = (TextView) findViewById(R.id.message);
					text.setText("Twidroid Application not found.");
				    button2.setVisibility(View.GONE);
				}
			}
		});

Java 在Java中从classpath加载文本文件资源

final URL resource = this.getClass().getClassLoader().getResource(path);
List<String> lines=IOUtils.readLines((InputStream) resource.getContent());

Java 酷Haskell功能魔法(箭头)

-- Uasge
*Main> let antwords = words "the tan ant gets some fat"

*Main> clusterBy length antwords
[["the","tan","ant","fat"],["gets","some"]]

*Main> clusterBy head antwords
[["ant"],["fat"],["gets"],["some"],["the","tan"]]

*Main> clusterBy last antwords
[["the","some"],["tan"],["gets"],["ant","fat"]]

-- Source
import Control.Arrow ((&&&))
import qualified Data.Map as M

clusterBy :: Ord b => (a -> b) -> [a] -> [[a]]
clusterBy f = M.elems . M.map reverse . M.fromListWith (++) . map (f &&& return)

Java 为Wicket的AjaxButton提交脚本之前

new AjaxButton("save") {
    @Override
    protected IAjaxCallDecorator getAjaxCallDecorator() {
        return new DisableComponentCallDecorator(super.getAjaxCallDecorator(), this);
    }
}

class DisableComponentCallDecorator extends AjaxPreprocessingCallDecorator {
    private final String selector;
    private DisableComponentCallDecorator(IAjaxCallDecorator delegate, String jquerySelector) {
        super(delegate);
        this.selector = jquerySelector;
    }
    private DisableComponentCallDecorator(IAjaxCallDecorator delegate, Component component) {
        super(delegate);
        component.setOutputMarkupId(true);
        this.selector = "#" + component.getMarkupId();
    }
    @Override
    public CharSequence preDecorateScript(CharSequence script) {
        return "$('" + selector + "').addClass('disabled_while_waiting').attr('disabled','disabled'); " + super.preDecorateScript(script);
    }
    @Override
    public CharSequence preDecorateOnSuccessScript(CharSequence script) {
        return "$('" + selector + "').removeAttr('disabled'); " + super.preDecorateOnSuccessScript(script);
    }
    @Override
    public CharSequence preDecorateOnFailureScript(CharSequence script) {
        return "$('" + selector + "').removeAttr('disabled'); " + super.preDecorateOnFailureScript(script);
    }
}

Java Haskell静态调度重写规则

-- Enabling the extension
{-# OPTIONS -fglasgow-exts #-} 

--  Equal like class
class  MEEq a  where
   (=-=)   :: a -> a -> Bool

-- Instances with no implementation
instance MEEq ()
instance MEEq Bool
instance MEEq Int

-- The rewrite rule
{-# RULES
    "eq/Bool"   (=-=) = eq_Bool :: Bool -> Bool -> Bool
    "eq/Unit"   (=-=) = eq_Unit :: ()   -> ()   -> Bool
    #-}

main = do
       print $ True =-= False
       print $ ()   =-= ()
       -- This fails (at runtime!) since we didn't define a rule for it
       print $ 7    =-= (8 :: Int)

-- Makes sure to fail any instance that doesn't have an implementation on compile time
{-# RULES
    "Unable to resolve instance resolution" (=-=) = (=-=)
      #-}

Java 在Android上通过GMail或MMS发送图像

Intent i = new Intent(Intent.ACTION_SEND) ;
i.putExtra(Intent.EXTRA_STREAM,imageUri) ;
i.setType("image/jpeg") ;
startActivity(Intent.createChooser(i,"Send Image To:")) ;

Java MD5SUMS

public String md5sums(InputStream is) throws NoSuchAlgorithmException, FileNotFoundException{
		MessageDigest digest = MessageDigest.getInstance("MD5");
		byte[] buffer = new byte[8192];
		int read = 0;
		String md5sums = "";
		try {
			while( (read = is.read(buffer)) > 0) {
				digest.update(buffer, 0, read);
			}		
			byte[] md5sum = digest.digest();
			BigInteger bigInt = new BigInteger(1, md5sum);
			md5sums = bigInt.toString(16);
		}
		catch(IOException e) {
			throw new RuntimeException("Unable to process file for MD5", e);
		}
		finally {
			try {
				is.close();
			}
			catch(IOException e) {
				throw new RuntimeException("Unable to close input stream for MD5 calculation", e);
			}
		}
		return md5sums;
	}

Java 从jar /工作目录加载图像

private BufferedImage loadImage(String path) {
	try {
		return ImageIO.read(this.getClass().getResource(path));
	} catch (IOException e) {
		return null;
	}
}