我想把java最常用的操作,写一些测试代码,以后用起来方便些。
1. 集合小测试: list set map 包括了集合的交集,并集,补集。
import org.apache.commons.collections4.CollectionUtils;
import java.util.*;
public class Main {
static void testList()
{
List<String> list = new ArrayList<String>();
list.add("abc");
list.add("def");
if ( list.contains("abc") ) {
list.remove("abc");
}
System.out.println(list);
}
static void testList2()
{
List list1 = new ArrayList();
list1.add("1111");
list1.add("2222");
list1.add("3333");
List list2 = new ArrayList();
list2.add("3333");
list2.add("4444");
list2.add("5555");
// 并集
list1.addAll(list2);
// 交集
list1.retainAll(list2);
// 差集
list1.removeAll(list2);
// 无重复并集
list2.removeAll(list1);
list1.addAll(list2);
}
static void testCollection(){
List<String> aList = new ArrayList<String>();
aList.add("aaa");
aList.add("bbb");
aList.add("ccc");
List<String> bList = new ArrayList<String>();
bList.add("aaa");
bList.add("ddd");
bList.add("eee");
// 并集
Collection<String> unionList = CollectionUtils.union(aList, bList);
// 交集
Collection<String> intersectionList = CollectionUtils.intersection(aList, bList);
// 是否存在交集
boolean isContained = CollectionUtils.containsAny(aList, bList);
// 交集的补集
Collection<String> disjunctionList = CollectionUtils.disjunction(aList, bList);
// 集合相减
Collection<String> subtractList = CollectionUtils.subtract(aList, bList);
// 排序
Collections.sort((List<String>) unionList);
Collections.sort((List<String>) intersectionList);
Collections.sort((List<String>) disjunctionList);
Collections.sort((List<String>) subtractList);
}
static void testhashMap(){
// 可以保持顺序的map
Map<String, String> hmap = new LinkedHashMap<String, String>();
// 不能保持顺序的map
//Map<String,String> hmap = new HashMap<String,String>();
hmap.put("Item0", "Value0");
hmap.put("Item2", "Value2");
hmap.put("Item1", "Value1");
hmap.put("Item3", "Value3");
if ( hmap.containsKey( "Item0" ) ){
Object value = hmap.get("Item0");
hmap.remove( "Item0" );
}
// 第一种遍历方式
for (String key : hmap.keySet()) {
String value = hmap.get(key);
System.out.println( key + value);
}
// 第二种遍历方式
Set<String> keySet = hmap.keySet();
//遍历存放所有key的Set集合
Iterator<String> it = keySet.iterator();
while(it.hasNext()){
//得到每一个key
String key = it.next();
//通过key获取对应的value
String value = hmap.get( key );
System.out.println( key+"="+value );
}
System.out.println( hmap );
int a = 0;
}
static void testSet(){
Set<String> set = new HashSet<String>();
set.add("s1");
set.add("s2");
set.add("s3");
for( String s: set ){
System.out.println( s );
}
}
public static void main(String[] args)
{
testList();
testList2();
testCollection();
testhashMap();
testSet();
}
}
2. 正则表达式的测试:
static void testRegex(){
///
// 测试匹配
{
Pattern p = Pattern.compile("\\d+");
Matcher m = p.matcher("22bb23");
// 要整个匹配才可以 false
System.out.println(m.matches());
// 因为22 是开头的匹配,所以是true
System.out.println( m.lookingAt() );
// find 是任意位置 true
System.out.println(m.find());
}
//
// replaceall
String value = "this abc is not abc.".replaceAll( "abc", "aaa");
System.out.println( value );
// replacefirst
System.out.println("w x j".replaceFirst("\\s", "-"));
// split
String[] array = "w x j".split("\\s");
for (String item : array) {
System.out.println(item);
}
{
// 中文匹配
String str = "abc閑人到人间123";
Pattern pattern = Pattern.compile("[\\u4e00-\\u9fa5]+");
Matcher matcher = pattern.matcher(str);
while (matcher.find()) {
System.out.println(matcher.group());
}
}
{
// img 匹配
String str = "<img src='aaa.jpg' /><img src=bbb.png/><img src=\"ccc.png\"/>" +
"<img src='ddd.exe'/><img src='eee.jpn'/>";
// 这里我们考虑了一些不规范的 img 标签写法,比如:空格、引号
Pattern pattern = Pattern.compile("<img\\s+src=(?:['\"])?(?<src>\\w+.(jpg|png))(?:['\"])?\\s*/>");
Matcher matcher = pattern.matcher(str);
while (matcher.find()) {
System.out.println(matcher.group(1));
}
}
{
String str = "<div>文章标题</div><div>发布时间</div>";
// 贪婪模式
Pattern pattern = Pattern.compile("<div>(?<title>.+)</div>");
Matcher matcher = pattern.matcher(str);
while (matcher.find()) {
System.out.println(matcher.group(1));
}
System.out.println("--------------");
// 非贪婪模式
pattern = Pattern.compile("<div>(?<title>.+?)</div>");
matcher = pattern.matcher(str);
while (matcher.find()) {
System.out.println(matcher.group(1));
}
}
}
3. 单例类
// 单例一
public class MyClass2
{
private MyClass2() {
}
private static MyClass2 instance = null;
public static MyClass2 getInstanceC() {
// 先判断实例是否存在,若不存在再对类对象进行加锁处理
if (instance == null) {
synchronized (MyClass2.class) {
if (instance == null) {
instance = new MyClass2();
}
}
}
return instance;
}
}
public class MyClass {
private static final MyClass myClass = new MyClass();
private MyClass() {}
public static MyClass getInstance() {
return myClass;
}
}