Top Ten API-related issues on Stack Overflow

Source: Internet
Author: User

Stack Overflow is a huge repository of programming knowledge, and on stack Overflow, millions of of questions are answered, and the answers are high-quality. That's why Stack Overflow is always the first in the rankings for Google search results.

Although there are a lot of questions on stack overflow, there are still a lot of questions raised every day, many of them waiting to be answered or not getting a good answer. Therefore, the question is how to find the answer, through the stack overflow is not enough.

With thousands of developers using Java APIs and sharing their projects on GitHub, these projects can provide a number of good examples of how to use Java's APIs. Java API example is a portal that provides sample searches for common Java API Code

In this article, I will explore the ability to solve the pre-polling API-related issues only through open source code (jexample). "API-related issues" refers to the issue of how to solve a task through some API. Stack overflow to vote on the front of the problem in Http://stackoverflow.com/questions/tagged/java can find

For each question, the best answer will be shown first, and then the solution via Java API Examples (jexample) will be presented in an illustrated way.

Traverse a HashMap

Accepted Answer:

    map<string, object> map = ...;     For (String Key:map.keySet ()) {      //...     }

  

If we search for "HashMap" in Jexample, go to the Java.util.HashMap sample page. Then click on one of the most commonly used methods-entryset () and we'll be able to quickly follow the example below:

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

  

This example shows how to iterate through HashMap.entrySet(),Entry.getKey() a hashmap by using and iterating through Entry.getValue() loops.

Links:HashMap.entrySet ()

Creates an array with an ArrayList

There are several answers to this question that provide many ways. Here are some of the top three methods:

    Method 1    New arraylist<element> (arrays.aslist (array))    //Method 2    Immutablelist.of ("string", " Elements ");    Method 3    list<string> l1 = lists.newarraylist (anotherlistorcollection);

  

The above three methods can be found by ' 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 ())

  

How do I generate a broken random number within a range?

The solution received from the answer:

    int randomnum = Rand.nextint ((max-min) + 1) + min;

  

How to convert a string to an integer type

The best answer

    int foo = integer.parseint ("1234");

  

How to convert byte streams to byte arrays

Accepted answers

    InputStream is;     byte[] bytes = Ioutils.tobytearray (IS);

  

How to generate a MD5Hash column

can useMessageDigest

public static string Getmd5digest (String str) {try {byte[] buffer = str.getbytes (); byte[] result = NULL; StringBuffer buf = null; MessageDigest MD5 = messagedigest.getinstance ("MD5");//Allocate box 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 Intv Al = 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;    }    

  

How to create a file in Java and write content to a file

Method One

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

  

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

  

The best way to read content from a text file in 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 ();     }

  

How to convert Java.util.Date to Xmlgregoriancalendar

Accepted Answer:

    GregorianCalendar C = new GregorianCalendar ();     C.settime (yourdate);     Xmlgregoriancalendar date2 = Datatypefactory.newinstance (). Newxmlgregoriancalendar (c);

  

How to check whether a string is a numeric string

The accepted answer is to use the Apache Commons LangStringUtils.isNumeric

    Stringutils.isnumeric ("23432")

  

Top Ten API-related issues on Stack Overflow

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.