Common API (Note 22)

Common API

1. Enhance for

public static void main(String[] args) {
Set <String>set =new HashSet<String>();
set.add("70");
set.add("40");
set.add("80");
set.add("50");
/* Iterator<String> it=set.iterator();
while(it.hasNext()){
System.out.println(it.next());
}*/
for(String s:  set){
s="90"; //This operation will not affect the data in the original collection
System.out.println(s);
}
}

It can also iterate over arrays
int [] x={2,4,6};
for(int i=0;i<x.length;i++){
System.out.println(x[i]);
}
is equivalent to the following
for(int n : x){
System.out.println(n);
}

//Example, use enhanced for to traverse the map collection
for(Map.Entry<String, String> item : map.entrySet()){
System.out.println(item.getKey()+":"+item.getValue());
}

Second, the Collections class

It is specially used to operate the Collection collection, and the methods inside are all static
//Example sort a list collection (natural order)
static void sortDemo(){
List<String> list=new ArrayList<String>();
list.add("b");
list.add("a");
list.add("a");
list.add("a");
list.add("a");
list.add("c");
list.add("f");
list.add("e");
Collections.sort(list);
for(String s:list){
System.out.println(s);
}
}

//Example 2 If you want to reverse the order in the above example, how to deal with it? You can flip the comparator and pass it in
Collections.sort(list, Collections.reverseOrder()); 

//Example 3 for the above example, sort by the length of the string, use a custom comparator
Collections.sort(list,new Comparator<String>() {
public int compare(String o1, String o2) {
return o1.length()-o2.length(); //Because it is the data in the list collection, so don't worry about deduplication
}
}); 

//Example 4 find the maximum value in the set
max(Collection<? extends T> coll) 
max(Collection<? extends T> coll, Comparator<? super T> comp) //You can pass a custom comparator
public class Test {
public static void main(String[] args) {
List<Person> list=new ArrayList<Person>();
list.add(new Person(20, "student one"));
list.add(new Person(90, "student two"));
list.add(new Person(50, "student three"));
list.add(new Person(30, "student four"));
Person p= Collections.max(list);
System.out.println(p);
}
}

class Person implements Comparable<Person> {
@Override
public String toString() {
return "Person [age=" + age + ", name=" + name + "]";
}
Person(int age,String name){
this.age=age;
this.name=name;
}
int age;
String name;
public int compareTo(Person o) {
return this.age-o.age;
}
}

//Example 5 find
static void searchDemo(){
List<String> list = new ArrayList<String>();
list.add("b");
list.add("c");
list.add("f");
list.add("a");
list.add("d");
list.add("e");
Collections.sort(list); //Before binary query, be sure to sort
int index=Collections.binarySearch(list, "c"); //Returns the index of the location of the content
System.out.println(index);
}

//Example 6, fill
List<String> list = new ArrayList<String>();
list.add("b");
list.add("c");
list.add("f");
Collections.fill(list, "bad");
System.out.println(list); //[bad, bad, bad]

//Example 7 replace
List<String> list = new ArrayList<String>();
list.add("b");
list.add("c");
list.add("f");
list.add("f");
Collections.replaceAll(list, "f", "嗷");
System.out.println(list); //[b, c, ow, ow]

//shuffle
static void shuffdemo(){
List<String> list = new ArrayList<String>();
list.add("a");
list.add("b");
list.add("c");
list.add("d");
list.add("e");
Collections.shuffle(list);
System.out.println(list);
}

A small exercise to randomly generate 100 random numbers between 1=100 and shuffle the order
List <Integer> list=new ArrayList<Integer>();
for(int i=0;i<100;i++){
list.add(i);
}
Collections.shuffle(list);
System.out.println(list);

Three, Arrays class

Mainly used to operate on arrays, all composed of static methods
--Arrays.sort();
--Arrays.binarySearch(...) //Binary search
--Arrays.copyOf() //Copy of array
--Arrays.fill() //fill
--Arrays.asList(T... a) //The array is converted into a collection
    
//Example, array to collection
public static void main(String[] args) {
String [] array={"001","002","009"};
List<String> list=Arrays.asList(array); //Convert the array into a collection
list.add("new data"); //UnsupportedOperationException Serious attention, do not add or delete class operations for the collections transferred from asList
list.add("new data");
test(list);
}
static void test(List <?>list){
for(int i=0;i<list.size();i++){
System.out.println(list.get(i));
}
}

//Example collection to array
public static void main(String[] args) {
List<String> list=new ArrayList<String>();
list.add("a");
list.add("a");
list.add("x");
list.add("a");
list.add("sssa");
String []  array=new String [list.size()];
list.toArray(array);
for (int i = 0; i < array.length; i++) {
System.out.println(array[i]);
}
}

Fourth, the System class

It is declared with final, this class cannot be instantiated
-- static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length) 
-- static void exit(int status) //Exit the system
-- static void gc() // invoke the garbage collector
-- static long currentTimeMillis() //Get the current time in milliseconds
-- static Map<String,String> getenv() //Get information about the current environment (get all environment variables)
-- static String getenv(String name); //According to the name of the environment variable, get the value of the environment variable

//Example Get the value of an environment variable based on the name of the environment variable
String result=System.getenv("path");

//Example take out all environment variables
Map<String,String> map= System.getenv();
for(Map.Entry<String, String> item: map.entrySet()){
System.out.println(item.getKey()+":"+item.getValue());
}
--static String setProperty(String key, String value) //Set system properties
--static String getProperty(String key) //Get system properties 
--static Properties getProperties() //Get all system properties
The Properties collection inherits from HashTable 
Represents a persistent set of properties. Properties can be saved in or loaded from a stream.
Each key and its corresponding value in the property list is a string
         
//Example Get all system properties
Properties prop = System.getProperties();
Set<Object> set = prop.keySet();
for (Object key : set) {
System.out.println(key+"    :   "+prop.get(key));
}   

Five, Runtime class

Every Java application has an instance of the Runtime class that enables the application to interface with the environment in which it runs.
The current runtime can be obtained through the getRuntime method. 
Runtime represents the current virtual machine process, not new 
 
//Example, open the drawing program, close after 5 seconds
Runtime r= Runtime.getRuntime(); 
Process p= r.exec("mspaint.exe c:\\chick.bmp");  
System.out.println("Please look at the chicken I drew, 5 seconds");
Thread.sleep(5000); //Thread stops for 5 seconds
p.destroy();

Six, Date and SimpleDateFormat

// Example 1 
Date d=new Date(); //Get the Date object corresponding to the current date
System.out.println(d);  //Tue Apr 19 14:21:22 CST 2016
Note: This Date is under java.util, not under java.sql
  
//Example 2 format the date according to the custom format and output
Date d=new Date();
SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd E hh:mm:ss");
String str= df.format( d) ;
System.out.println(str); //2016-04-19 Tuesday 02:25:39

//Example 3 convert the string to date format
String str="2016-04-19";
SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd");
Date d= df.parse(str); // Convert the string to a date
If the format string does not match the target string, an Unparseable date: "2016-04-19" exception occurs
  

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325537917&siteId=291194637