• Stackoverflow 最受关注的 10 个 Java 问题


    Stack Overflow 是一个大型的编程知识库。在 Stack Overflow 中已经有数以百万计的问题,并且很多答案有着很高的质量。这就是为什么 Stack Overflow 的答案经常位于 Google 搜索结果的顶部。


    尽管 Stack Overflow 上很多问题已经有了答案,每天还是会有很多问题被提出,很多问题仍然没有被回答或者没有获得满意的答案。那么当 Stack Overflow 不能解决问题时如何去寻找答案。


    随着成千上万的程序员使用 Java APIs 并在 Github 上分享他们的项目,这些项目能够给我们提供很好的例子来展示如何使用 Java APIs。Java API Example 是一个搜索门户,它提供了一些非常受欢迎的 Java APIs 的示例代码。


    在这篇文章中,将探讨使用开源代码(jExample)是否能够回答排序前列的 API 相关问题。API 相关问题指的是如何使用 APIs 解决任务的问题。我们对 Stack Overflow 排在前面的问题进行了分析。


    对于每一个问题,最佳答案显示在最前面,然后是从 Java API examples (jExample) 中给出的解决方法。


    1. 遍历 HashMap


    采纳的答案给出了这样的解决方法:


    Map<String, Object> map = ...; 

    for (String key : map.keySet()) { 

         // ... 

    }


    如果我们在 jExample 上搜索 “HashMap” 并跳转到 java.util.HashMap 的示例页面。然后点击最常见的一种方法 – entry(),我们可以快速得到如下的一个例子:




    这个例子向我们展示了如何使用HashMap HashMap.entrySet()、Entry.getKey() 和 Entry.getValue() 来遍历HashMap。


    链接:HashMap.entrySet()


     http://www.programcreek.com/java-api-examples/index.php?class=java.util.HashMap&method=entrySet 


    2. 由数组创建 ArrayList


    这个问题,答案中提供了多种方法。这里是排名前三的方法:


    // Method 1

    new ArrayList(Arrays.asList(array))


    // Method 2

    ImmutableList.of("string", "elements");


    // Method 3

    List l1 = Lists.newArrayList(anotherListOrCollection);


    上面的三个方法能够在 jExample 中找到:


    方法 1:




    方法 2:




    方法 3:




    如果我们不知道 ImmutableList 类就很难发现第二种方法。然而,如果我们去查看 ImmutableList 的示例页面,我们会学到这个类很多其它的用法。


    链接: Arrays.asList(), ImmutableList.of(), Lists.newArrayList()


    http://www.programcreek.com/java-api-examples/index.php?class=java.util.Arrays&method=asList


    http://www.programcreek.com/java-api-examples/index.php?%20api=com.google.common.collect.ImmutableList


    http://www.programcreek.com/java-api-examples/index.php?api=com.google.common.collect.Lists


    3. 怎样在一个范围内产生一个随机整数?


    采纳的答案给出的解决方法是:


    int randomNum = rand.nextInt((max - min) + 1) + min;


    如果我们查看 java.util,Random 类的页面,我们也能够找到一种类似的方法:




    链接: Random.nextInt()


    http://www.programcreek.com/java-api-examples/index.php?class=java.util.Random&method=nextInt


    4. 怎样将 String 转换为 int 类型?


    最优的答案:


    int foo = Integer.parseInt("1234");




    链接: Integer.parseInt()


    http://www.programcreek.com/java-api-examples/index.php?class=java.lang.Integer&method=parseInt


    5. 怎样将 InputStream 转换为 byte array?


    采纳的答案:


    InputStream is; 

    byte[] bytes = IOUtils.toByteArray(is);




    链接: ByteArrayOutputStream, IOUtils.toByteArray()


    http://www.programcreek.com/java-api-examples/index.php?api=java.io.ByteArrayOutputStream


    http://www.programcreek.com/java-api-examples/index.php?class=org.apache.commons.io.IOUtils&method=toByteArray


    6. 怎样产生一个 MD5 哈希?


    答案提到了 MessageDigest。




    链接: MessageDigest


    http://www.programcreek.com/java-api-examples/index.php?api=java.security.MessageDigest


    7. Java 如何创建一个文件和写入一个文件?


    创建一个文本文件-方法 1


    PrintWriter writer = new PrintWriter("the-file-name.txt", "UTF-8"); writer.println("The first line"); 

    writer.println("The second line"); 

    writer.close();


    创建一个文本文件-方法 2


    List 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"));


    jExample 中找到的例子:


    方法 1.




    方法 2.




    方法 3.




    链接: FileWriter, FileOutputStream, Files.write()


    http://www.programcreek.com/java-api-examples/index.php?api=java.io.FileWriter


    http://www.programcreek.com/java-api-examples/index.php?api=java.io.FileOutputStream


    http://www.programcreek.com/java-api-examples/index.php?class=java.nio.file.Files&method=write


    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(); 

    }


    jExample 中的例子: 




    链接: FileInputStream, FileReader, Files


    http://www.programcreek.com/java-api-examples/index.php?api=java.io.FileInputStream


    http://www.programcreek.com/java-api-examples/index.php?api=java.io.FileReader


    http://www.programcreek.com/java-api-examples/index.php?api=java.nio.file.Files


    9. 怎样从 java.util.Date转换为XMLGregorianCalendar?


    采纳的答案:


    GregorianCalendar c = new GregorianCalendar(); 

    c.setTime(yourDate); XMLGregorianCalendar date2 = DatatypeFactory.newInstance().newXMLGregorianCalendar(c);




    链接: DatatypeFactory.newXMLGregorianCalendar()


    http://www.programcreek.com/java-api-examples/index.php?class=javax.xml.datatype.DatatypeFactory&method=newXMLGregorianCalendar


    10. 如何检查一个字符串是否为数字?


    采纳的答案建议使用 Apache Commons Lang 中的 StringUtils.isNumeric。


    这个答案的示例代码可能没必要给出,因为这段代码只有一行。而且,如果查看 StringUtils 的页面,可以看到 StringUtils 最常用的方法列表。这个列表按照流行的程度排序。下面是这份列表的一个快照:



    链接: StringUtils


    http://www.programcreek.com/java-api-examples/index.php?class=org.apache.commons.lang.StringUtils&method=isNumeric


    总结


    我找到了 Stack Overflow 上最热门 10 个问题的全部示例代码。然而,jExample 要求用户拥有一定的知识水平并且能够想到大概是什么 API 类。有些答案可能并不明显,不过 jExample 为使用目标 API 类提供了补充信息。侧边栏的相关类和常用方法对于查看相关 API 类和目标 API 类的重要方法是很有用处的。


  • 相关阅读:
    数据仓库010
    R语言- 实验报告
    数据仓库006
    数据仓库009
    多台Linux 7.x服务器具有相同的UUID网络链接参数,肿么办?
    数据仓库005
    数据仓库004
    我的编程竞赛生涯
    我的建模竞赛生涯
    再见了,亲爱的博客园
  • 原文地址:https://www.cnblogs.com/zhangboyu/p/7452584.html
Copyright © 2020-2023  润新知