hello world

public class HelloWorld {

    public static void main(String[] args) {
        // Prints "Hello, World" to the terminal window.
        System.out.println("Hello, World");
    }

}

maven

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>bjut</groupId>
    <artifactId>income-expenditure-comparison</artifactId>
    <version>1.0-SNAPSHOT</version>
    <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>com.opencsv</groupId>
            <artifactId>opencsv</artifactId>
            <version>4.0</version>
        </dependency>
    </dependencies>
    <build>
    	<plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                    <encoding>utf8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>1.2.1</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <transformers>
                                <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                    <mainClass>views.Home</mainClass>
                                </transformer>
                            </transformers>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

maven 命令

mvn test //运行测试
mvn -U clean install //强制去远程更新snapshot的插件或依赖
mvn package //maven 打包
mvn update //更新
mvn clean package //打jar包

文件读取写入

import java.io.File;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileWriter;
 
public class cin_txt {
	static void main(String args[]) {
		try { // 防止文件建立或读取失败,用catch捕捉错误并打印,也可以throw
 
			/* 读入TXT文件 */
			String pathname = "D:\\twitter\\13_9_6\\dataset\\en\\input.txt"; // 绝对路径或相对路径都可以,这里是绝对路径,写入文件时演示相对路径
			File filename = new File(pathname); // 要读取以上路径的input。txt文件
			InputStreamReader reader = new InputStreamReader(
					new FileInputStream(filename)); // 建立一个输入流对象reader
			BufferedReader br = new BufferedReader(reader); // 建立一个对象,它把文件内容转成计算机能读懂的语言
			String line = "";
			line = br.readLine();
			while (line != null) {
				line = br.readLine(); // 一次读入一行数据
			}
 
			/* 写入Txt文件 */
			File writename = new File(".\\result\\en\\output.txt"); // 相对路径,如果没有则要建立一个新的output。txt文件
			writename.createNewFile(); // 创建新文件
			BufferedWriter out = new BufferedWriter(new FileWriter(writename));
			out.write("我会写入文件啦\r\n"); // \r\n即为换行
			out.flush(); // 把缓存区内容压入文件
			out.close(); // 最后记得关闭文件
 
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

编码

String str2 = new String(s.getBytes("gbk"), "utf-8");//默认字符集为utf-8

set

Set<Integer> s; //contains your Integers
Set<Integer> temp = new Set<Integer>();
for(Integer i : s)
    temp.add(i+1);
s.clear();
s.addAll(temp);

List

//iterator
Iterator<Integer> iterator = set.iterator();
while(iterator.hasNext()) {
	Integer setElement = iterator.next();
    if(setElement==2) {
    	iterator.remove();
    }
}

//add
List<String> copy = new ArrayList<String>(list);
for (String s : copy) {
    // And if you have to add an element to the list, add it to the original one:
    list.add("some element");
}

//ListIterator
List<String> list = Arrays.asList("zero", "one", "two");

ListIterator iter = list.listIterator();

while (iter.hasNext()) {
    System.out.println("index: " + iter.nextIndex() + " value: " + iter.next());
}

for

List<Integer> list = new ArrayList<Integer>();
Integer lastValue = null;
for (Integer i : list) {
    // do stuff
    lastValue = i;
}
// do stuff with last value

Checking if a string is empty or null

System.out.println(Strings.isNullOrEmpty("")); // true

//compare strings
new String("test").equals("test") // --> true 

//int2string
String.valueOf(number)
"" + number
Integer.toString(number)

//check somethin in list
if(Arrays.asList("a","b","c").contains("a"))

//get first character of string
ld.substring(0,1)

//You can now use a Stream to check whether an array of int, double or long contains a value
int[] a = {1,2,3,4};
boolean contains = IntStream.of(a).anyMatch(x -> x == 4);

java lambda

public class ComparatorTest {

	public static void main(String[] args) {
    
		List<Person> personList = Person.createShortList();
   
     	// Sort with Inner Class
     	Collections.sort(personList, new Comparator<Person>(){
       		public int compare(Person p1, Person p2){
         		return p1.getSurName().compareTo(p2.getSurName());
       		}
     	});
     
     	System.out.println("=== Sorted Asc SurName ===");
     	for(Person p:personList){
       		p.printName();
     	}
     
     	// Use Lambda instead
     
     	// Print Asc
     	System.out.println("=== Sorted Asc SurName ===");
     	Collections.sort(personList, (Person p1, Person p2) -> p1.getSurName().compareTo(p2.getSurName()));
 
     	for(Person p:personList){
       		p.printName();
     	}
     
     	// Print Desc
     	System.out.println("=== Sorted Desc SurName ===");
     	Collections.sort(personList, (p1,  p2) -> p2.getSurName().compareTo(p1.getSurName()));
 
     	for(Person p:personList){
        	p.printName();
     	}     
   }
}

hashMap

import java.util.HashMap;

public class CheckKeyExample {
 
 public static void main(String[] args) {
 
    // Creating a HashMap of int keys and String values
    HashMap<Integer, String> hashmap = new HashMap<Integer, String>();
 
    // Adding Key and Value pairs to HashMap
    hashmap.put(11,"Chaitanya");
    hashmap.put(22,"Pratap");
    hashmap.put(33,"Singh");
    hashmap.put(44,"Rajesh");
    hashmap.put(55,"Kate");
 
    // Checking Key Existence
    boolean flag = hashmap.containsKey(22);
    System.out.println("Key 22 exists in HashMap? : " + flag);
 
    boolean flag2 = hashmap.containsKey(55);
    System.out.println("Key 55 exists in HashMap? : " + flag2);
 
    boolean flag3 = hashmap.containsKey(99);
    System.out.println("Key 99 exists in HashMap? : " + flag3);
 }
}

interface

public interface Color {
   void fill();
}

abstract class

public abstract class AbstractFactory {
   public abstract Color getColor(String color);
   public abstract Shape getShape(String shape) ;
}

foreach hashmap

Map<String, String> map = ...
for (Map.Entry<String, String> entry : map.entrySet()){
    System.out.println(entry.getKey() + "/" + entry.getValue());
}

运行jar程序

java -jar xx.jar