摘要:hashset與List與ArraryList放字串到Iterator與iterable取集合物件,並用guava.jar內的splitter切割物件
package com.cht.commons.persistence.query;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.regex.Pattern;
import com.google.common.base.CharMatcher;
import com.google.common.base.Splitter;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
public class testSplitter {
private static final Pattern PARAMETER_NAME_PATTERN = Pattern.compile("\\w+");
private static final Pattern PARAMETER_PATTERN = Pattern.compile("" + "[^'^(]:("
+ PARAMETER_NAME_PATTERN.pattern() + ")" + "|[^'^(]::" + "|:("
+ PARAMETER_NAME_PATTERN.pattern() + ")" + "");
public static void main(String[] args) {
System.out.println("===========hashset放到iterator取出集合");
//======V1=============hashset放到iterator取出集合=====================================================================
//宣告型態為Set,實體為HashSet的變數 setTest,限制只能加 入String型態 的資料到setTest中
Set setTest = new HashSet();
setTest.add("apple"); // apple加入setTest
setTest.add("banana");// banana加入setTest
setTest.add("tomato");// tomato加入setTest
// 宣告Iterator型態的變數 it, 限制只能將String型態加入it
Iterator it;
// setTest呼叫iterator()方法,回傳Iterator型態的物件, 該物件包含所有setTest內所存放的值, 將該物件存入it
it = setTest.iterator();
/*
* hasNext()為使用游標走訪Iterator物件,檢查下一筆元素是包含物件, 有包含物件則傳回true,否則false。
* 游標的起始位置在Iterator第一筆元素之前,所以第一次執行hasNext(), 會檢查Iterator的第一筆元素,使用此方式可以走訪Iterator內的所有物件
*/
while (it.hasNext()) {
System.out.println(it.next());
}
/*
* next()為使用游標走訪Iterator,取出下一筆的值。
* 游標的起始位置在Iterator第一筆之前,所以第一次執行next(),會
* 取出Iterator的第一筆資料
*/
System.out.println("===========List放到iterator取出集合");
//============V2=======List放到iterator取出集合
List l = new ArrayList();
l.add("aa");
l.add("bb");
l.add("cc");
for (Iterator iter = l.iterator(); iter.hasNext();) {
String str = (String)iter.next();
System.out.println(str);
}
/*迭代器用于while循环
Iterator iter = l.iterator();
while(iter.hasNext()){
String str = (String) iter.next();
System.out.println(str);
}
*/
System.out.println("===========ArraryList放到Iterable取出集合");
//===========ArraryList放到Iterable取出集合
List<Object> randomObjects = Lists.newArrayList();
randomObjects.add(new Integer(15));
randomObjects.add(new Double(12));
randomObjects.add("hello");
randomObjects.add(Lists.newArrayList());
randomObjects.add(Maps.newConcurrentMap());
randomObjects.add("world");
Iterable<String> strings = Iterables.filter(randomObjects, String.class);
for (String s : strings) {
System.out.println(s);
}
System.out.println("============================arrarylist and Combine two iterables");
List<String> list1 = Lists.newArrayList("one");
List<String> list2 = Lists.newArrayList("two");
Iterable<String> oneAndTwo = Iterables.concat(list1, list2);
for (String s : oneAndTwo) {
System.out.println(s);
}
System.out.println("===com.google.guava_17.0.jar=====需要splliter的jar檔===============使用spllit方法切string,放至iterator取出集合");
//============================================使用spllit方法切string
// TODO Auto-generated method stub
CharSequence str = "SELECT :: FROM :: WHERE id = :id AND remark = '::'";
Iterator<String> nonParameterParts = Splitter.on(PARAMETER_PATTERN).trimResults()
.omitEmptyStrings().split(str).iterator();
while (nonParameterParts.hasNext()) {
System.out.println(nonParameterParts.next());
}
str = "A IN (:a) AND B = :ab";
Iterator<String> nonParameterParts2 = Splitter.on(PARAMETER_PATTERN).trimResults()
.omitEmptyStrings().split(str).iterator();
while (nonParameterParts2.hasNext()) {
System.out.println(nonParameterParts2.next());
}
System.out.println("====com.google.guava_17.0.jar=====需要splliter的jar檔==============使用spllit方法切string,放至Iterable取出集合");
// Iterable用法
Iterable<String> iterableResult = Splitter.on(" ")
// .trimResults(CharMatcher.DIGIT)
.omitEmptyStrings().split(str);
for (String s : iterableResult) {
System.out.println(s);
}
}
}