Stack Overflow 上排名前十的與API相關的問題,stackoverflow
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)的解決方案也會圖文並茂的展示。
遍曆一個HashMap
被接受的回答:
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 hashresult = new byte[md5.getDigestLength()];// calculate hashmd5.reset();md5.update(buffer);result = md5.digest();// System.out.println(result);// create hex string from the 16-byte hashbuf = 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; }
在java中如何建立一個檔案並向檔案中寫入內容
方法一
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"));
在java中從文字檔讀取內容的最好方法
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(); }
如何將java.util.Date轉換成XMLGregorianCalendar
被接受的回答:
GregorianCalendar c = new GregorianCalendar(); c.setTime(yourDate); XMLGregorianCalendar date2 = DatatypeFactory.newInstance().newXMLGregorianCalendar(c);
如何檢查一個字串是否為數值型的字串
被接受的回答是使用Apache Commons Lang包中的 StringUtils.isNumeric
StringUtils.isNumeric("23432")