Stack Overflow是一個龐大的編程知識倉庫,在Stack Overflow 上,數百萬的提問被回答,並且這些回答都是高質量的。這就是為什麼在Google搜索結果的排行榜上,Stack Overflow 總是位居首位。
雖然Stack Overflow上有非常多的提問,但是仍然每天都有大量的問題被提出,其中的很多都等待解答或者沒有得到好的解答。因此,問題是如何找到答案的,通過Stack Overflow是不夠的。
隨著成千上萬的開發者使用Java的API並且在Github上分享他們的項目,這些項目可以提供很多很好的例子來展示如何使用Java的API。Java API Example是一個提供常用Java API代碼示例搜索的入口
在這篇文章中,我將會探索只通過開源的代碼(jExample)能否解決投票前幾名的API相關問題。“API相關的問題”指的是如何通過一些API來解決一個任務的問題。Stack Overflow上投票靠前的問題在http://stackoverflow.com/questions/tagged/java可以找到
對於每一個問題,最好的回答首先會被展示,隨後通過Java API examples(jExample)的解決方案也會圖文並茂的展示。
被接受的回答:
Map<String, Object> map = ...;
for (String key : map.keySet()) {
// ...
}
如果我們在jExample搜索“HashMap”,前往java.util.HashMap示例頁面。然後點擊其中一個最常用的方法-entrySet(),我們就能快速的如下的示例:
HashMap<BigInteger,R> subMap = rowie.getValue();
for( Entry<BigInteger, R> colie : subMap.entrySet() )
{
BigInteger col = colie.getKey();
R vali = colie.getValue();
ret.setVal(row, col, mutr.mutate( vali ) );
}
這個例子展示了如何通過使用HashMap.entrySet(),Entry.getKey()和Entry.getValue()去迭代循環去遍歷一個HashMap
Links: HashMap.entrySet()
ArrayList
對於這個問題,有多個回答提供了很多方式。這裡是一些排名前三的方法:
// Method 1
new ArrayList<Element>(Arrays.asList(array))
// Method 2
ImmutableList.of("string", "elements");
// Method 3
List<String> l1 = Lists.newArrayList(anotherListOrCollection);
以上的三個方法可以通過`jExample1找到
Method 1:
List<String> updatedLikedAddresses = new ArrayList<>(Arrays.asLi(likedAddresses));
Method 2:
List<String> portions = ImmutableList.of(serviceName,version,callStyle.name())
Method 3:
List<String> portions = ImmutableList.of(serviceName,version,callStyle.name())
被接受的來自回答的解決方法:
int randomNum = rand.nextInt((max - min) + 1) + min;
最好的答案
int foo = Integer.parseInt("1234");
被采納的回答
InputStream is;
byte[] bytes = IOUtils.toByteArray(is);
MD5散列
可以使用MessageDigest
public static String getMD5Digest(String str) {
try {
byte[] buffer = str.getBytes();
byte[] result = null;
StringBuffer buf = null;
MessageDigest md5 = MessageDigest.getInstance("MD5");
// allocate room for the hash
result = new byte[md5.getDigestLength()];
// calculate hash
md5.reset();
md5.update(buffer);
result = md5.digest();
// System.out.println(result);
// create hex string from the 16-byte hash
buf = new StringBuffer(result.length * 2);
for (int i = 0; i < result.length; i++) {
int intVal = result[i] & 0xff;
if (intVal < 0x10) {
buf.append("0");
}
buf.append(Integer.toHexString(intVal).toUpperCase());
}
return buf.toString();
} catch (NoSuchAlgorithmException e) {
System.err.println("Exception caught: " + e);
e.printStackTrace();
}
return null;
}
方法一
PrintWriter writer = new PrintWriter("the-file-name.txt", "UTF-8");
writer.println("The first line");
writer.println("The second line");
writer.close();
方法 2
List<String> lines = Arrays.asList("The first line", "The second line");
Path file = Paths.get("the-file-name.txt");
Files.write(file, lines, Charset.forName("UTF-8"));
BufferedReader br = new BufferedReader(new FileReader("file.txt"));
try {
StringBuilder sb = new StringBuilder();
String line = br.readLine();
while (line != null) {
sb.append(line);
sb.append(System.lineSeparator());
line = br.readLine();
}
String everything = sb.toString();
} finally {
br.close();
}
被接受的回答:
GregorianCalendar c = new GregorianCalendar();
c.setTime(yourDate);
XMLGregorianCalendar date2 = DatatypeFactory.newInstance().newXMLGregorianCalendar(c);
被接受的回答是使用Apache Commons Lang包中的 StringUtils.isNumeric
StringUtils.isNumeric("23432")