stackoverflow上Java相关回答技巧 测试demo

来源:互联网 发布:初中课本同步软件 编辑:程序博客网 时间:2024/05/19 12:17
import java.io.FileInputStream;import java.io.StringWriter;import java.util.ArrayList;import java.util.Arrays;import java.util.Collections;import java.util.Comparator;import java.util.HashMap;import java.util.Iterator;import java.util.List;import java.util.Map;import java.util.Map.Entry;import java.util.TreeMap;import org.apache.commons.io.IOUtils;import org.apache.commons.lang.ArrayUtils;//https://github.com/giantray/stackoverflow-java-top-qa//stackoverflow-java-top-qapublic class Stackoverflow_java_top_qa {static String convertStreamToString(java.io.InputStream is) {java.util.Scanner s = new java.util.Scanner(is).useDelimiter("\\A");return s.hasNext() ? s.next() : "";}public static void main(String[] args) throws Exception {String[] strs = { "123", "456", "789" };List<String> list = new ArrayList<String>();// array=>listCollections.addAll(list, strs);list.add("hehe");System.out.println(list);list = Arrays.asList(strs);System.out.println(list);// java.lang.UnsupportedOperationException// https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/create-arraylist-arraylistt-from-array-t.md// list.add("test");System.out.println(list);// list=>arraystrs = (String[]) list.toArray();System.out.println(Arrays.toString(strs));String content = convertStreamToString(new FileInputStream("pom.xml"));System.out.println(content);Map<String, String> myMap = new HashMap<String, String>();myMap.put("001", "test1024");myMap.put("002", "test002");// 支持jdk4以下Iterator entries = myMap.entrySet().iterator();while (entries.hasNext()) {Entry thisEntry = (Entry) entries.next();Object key = thisEntry.getKey();Object value = thisEntry.getValue();System.out.println(key + "=" + value + ",");}testarr001();testsortMap();testConcat();teststack();testList();//inputstream =>stringStringWriter writer = new StringWriter();IOUtils.copy(new FileInputStream("pom.xml"), writer, "utf-8");String theString = writer.toString();System.out.println(theString);//inputstream =>stringtheString=IOUtils.toString(new FileInputStream("pom.xml"), "utf-8");System.out.println(theString);}//通过一行代码初始化ArrayListprivate static void testList() {ArrayList<String> places = new ArrayList<String>(Arrays.asList("Buenos Aires", "Córdoba", "La Plata"));System.out.println(places);}//获取完整的堆栈信息private static void teststack() {String fullStackTrace = org.apache.commons.lang.exception.ExceptionUtils.getFullStackTrace(new Throwable());System.out.println(fullStackTrace);}//便捷地将两个数组合到一起private static void testConcat() {String[] a = { "ab", "cd", "ef" };String[] b = { "12", "23", "354" };int aLen = a.length;int bLen = b.length;String[] c = new String[aLen + bLen];System.arraycopy(a, 0, c, 0, aLen);System.arraycopy(b, 0, c, aLen, bLen);System.out.println(Arrays.toString(c));String[] both = (String[]) ArrayUtils.addAll(a, b);System.out.println(Arrays.toString(both));}// https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/how-to-sort-a-mapkey-value-on-the-values-in-java.md// Map<Key,Value>基于Value值排序private static void testsortMap() throws Exception {final HashMap<String, Double> map = new HashMap<String, Double>();TreeMap<String, Double> sorted_map = new TreeMap<String, Double>(new Comparator<String>() {public int compare(String a, String b) {if (map.get(a) >= map.get(b)) {return -1;} else {return 1;} //}});map.put("A", 99.5);map.put("B", 67.4);map.put("C", 67.6);map.put("D", 167.3);System.out.println("unsorted map: " + map);sorted_map.putAll(map);System.out.println("results: " + sorted_map);}// 测试一个数组是否包含指定的值private static void testarr001() throws Exception {String[] fieldsToInclude = { "id", "name", "location" };if (ArrayUtils.contains(fieldsToInclude, "id")) {System.out.println("true");} else {System.out.println("false");}System.out.println(Arrays.asList(fieldsToInclude).contains("id"));}}

0 0
原创粉丝点击