Skip to content

Instantly share code, notes, and snippets.

@yomusu
Created June 19, 2014 06:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yomusu/fb35989b889fb10495d4 to your computer and use it in GitHub Desktop.
Save yomusu/fb35989b889fb10495d4 to your computer and use it in GitHub Desktop.
join iterable
public class Hoge {
/***
*
* sepaで連結して文字列を作成します
*
* @param sepa
* @return
*/
static public String join( Iterable iterable, String sepa ) {
Iterator it = iterable.iterator();
if( it.hasNext() ) {
StringBuilder buf = new StringBuilder();
buf.append( itemToString(it.next()) );
while( it.hasNext() ) {
buf.append(sepa);
buf.append( itemToString(it.next()) );
}
return buf.toString();
} else {
// nothing
return "";
}
}
/** 項目をString化するヤツ(本当はdelegateぽくしたい) */
static protected String itemToString( Object obj ) {
if( obj !=null ) {
return obj.toString();
}
return "";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment