如题所述
ç´æ¥ä¸ä»£ç ï¼
package test.contain.lishaojie;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
public class TestContain {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String[] arr = new String[] { "DD", "CC", "DD", "FF", "KK"};
String target ="A";
int times = 1000;//次æ°
//转æ¢ælistæ¹å¼
long startTime = System.currentTimeMillis();
for (int i = 0; i < times; i++) {
ByList(arr, target);
}
long endTime = System.currentTimeMillis();
long duration = endTime - startTime;
System.out.println("listæ¹å¼: " + duration / 1000000);
//转æ¢æsetæ¹å¼
startTime = System.currentTimeMillis();
for (int i = 0; i < times; i++) {
BySet(arr, target);
}
endTime = System.currentTimeMillis();
duration = endTime - startTime;
System.out.println("setæ¹å¼: " + duration / 1000000);
//ç´æ¥å¾ªç¯æ¹å¼
startTime = System.currentTimeMillis();
for (int i = 0; i < times; i++) {
ByForLoop(arr, target);
}
endTime = System.currentTimeMillis();
duration = endTime - startTime;
System.out.println("循ç¯æ¹å¼: " + duration / 1000000);
//äºåæ³æ¥æ¾
startTime = System.currentTimeMillis();
for (int i = 0; i < times; i++) {
ByArraysBinarySearch(arr, target);
}
endTime = System.currentTimeMillis();
duration = endTime - startTime;
System.out.println("äºåæ³æ¥æ¾: " + duration / 1000000);
}
public static boolean ByList(String[] arr, String targetValue) {
return Arrays.asList(arr).contains(targetValue);
}
public static boolean BySet(String[] arr, String targetValue) {
Set<String> set = new HashSet<String>(Arrays.asList(arr));
return set.contains(targetValue);
}
public static boolean ByForLoop(String[] arr, String targetValue) {
for(String s: arr){
if(s.equals(targetValue))
return true;
}
return false;
}
public static boolean ByArraysBinarySearch(String[] arr, String targetValue) {
int a = Arrays.binarySearch(arr, targetValue);
if(a > 0)
return true;
else
return false;
}
}
è¿è¡ç»æå¦ä¸ï¼
listæ¹å¼: 5
setæ¹å¼: 22
循ç¯æ¹å¼: 2
äºåæ³æ¥æ¾: 3
ç»è¿å¤§éæ°æ®æµè¯å¾ªç¯æ¹å¼æçæé«ï¼å ¶æ¬¡æ¯äºåæ³ï¼æåæ¯list,åsetå 为å 为å°æ°ç»åå ¥Collectionç±»åä¸ï¼é¦å è¦å°æ°ç»å ç´ éåä¸éï¼ç¶åå使ç¨éåç±»åå ¶ä»æä½ãä½æ¯listæ¹å¼ææ¾è¦æ¯setæ¹å¼å¿«å¾å¤ï¼è¿æ¯ä¸ºä»ä¹å¢ï¼ç´æ¥ç代ç ï¼é¦å
@SafeVarargs
@SuppressWarnings("varargs")
public static <T> List<T> asList(T... a) {
return new ArrayList<>(a);
}
è¿åçæ¯ArrayListæ以setæ¹å¼è¿è¦è¿è¡ä¸æ¬¡æä½å°ArrayList转æ¢æset,
public HashSet(Collection<? extends E> c) {
map = new HashMap<>(Math.max((int) (c.size()/.75f) + 1, 16));
addAll(c);
}
ä¹ä¸addAllæ¹æ³ï¼
public boolean addAll(Collection<? extends E> c) {
boolean modified = false;
for (E e : c)
if (add(e))
modified = true;
return modified;
}
åä¸æ¬¡è¿è¡äºéç¯ï¼æ以æçæ¯è¾ä½ï¼binggo
package test.contain.lishaojie;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
public class TestContain {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String[] arr = new String[] { "DD", "CC", "DD", "FF", "KK"};
String target ="A";
int times = 1000;//次æ°
//转æ¢ælistæ¹å¼
long startTime = System.currentTimeMillis();
for (int i = 0; i < times; i++) {
ByList(arr, target);
}
long endTime = System.currentTimeMillis();
long duration = endTime - startTime;
System.out.println("listæ¹å¼: " + duration / 1000000);
//转æ¢æsetæ¹å¼
startTime = System.currentTimeMillis();
for (int i = 0; i < times; i++) {
BySet(arr, target);
}
endTime = System.currentTimeMillis();
duration = endTime - startTime;
System.out.println("setæ¹å¼: " + duration / 1000000);
//ç´æ¥å¾ªç¯æ¹å¼
startTime = System.currentTimeMillis();
for (int i = 0; i < times; i++) {
ByForLoop(arr, target);
}
endTime = System.currentTimeMillis();
duration = endTime - startTime;
System.out.println("循ç¯æ¹å¼: " + duration / 1000000);
//äºåæ³æ¥æ¾
startTime = System.currentTimeMillis();
for (int i = 0; i < times; i++) {
ByArraysBinarySearch(arr, target);
}
endTime = System.currentTimeMillis();
duration = endTime - startTime;
System.out.println("äºåæ³æ¥æ¾: " + duration / 1000000);
}
public static boolean ByList(String[] arr, String targetValue) {
return Arrays.asList(arr).contains(targetValue);
}
public static boolean BySet(String[] arr, String targetValue) {
Set<String> set = new HashSet<String>(Arrays.asList(arr));
return set.contains(targetValue);
}
public static boolean ByForLoop(String[] arr, String targetValue) {
for(String s: arr){
if(s.equals(targetValue))
return true;
}
return false;
}
public static boolean ByArraysBinarySearch(String[] arr, String targetValue) {
int a = Arrays.binarySearch(arr, targetValue);
if(a > 0)
return true;
else
return false;
}
}
è¿è¡ç»æå¦ä¸ï¼
listæ¹å¼: 5
setæ¹å¼: 22
循ç¯æ¹å¼: 2
äºåæ³æ¥æ¾: 3
ç»è¿å¤§éæ°æ®æµè¯å¾ªç¯æ¹å¼æçæé«ï¼å ¶æ¬¡æ¯äºåæ³ï¼æåæ¯list,åsetå 为å 为å°æ°ç»åå ¥Collectionç±»åä¸ï¼é¦å è¦å°æ°ç»å ç´ éåä¸éï¼ç¶åå使ç¨éåç±»åå ¶ä»æä½ãä½æ¯listæ¹å¼ææ¾è¦æ¯setæ¹å¼å¿«å¾å¤ï¼è¿æ¯ä¸ºä»ä¹å¢ï¼ç´æ¥ç代ç ï¼é¦å
@SafeVarargs
@SuppressWarnings("varargs")
public static <T> List<T> asList(T... a) {
return new ArrayList<>(a);
}
è¿åçæ¯ArrayListæ以setæ¹å¼è¿è¦è¿è¡ä¸æ¬¡æä½å°ArrayList转æ¢æset,
public HashSet(Collection<? extends E> c) {
map = new HashMap<>(Math.max((int) (c.size()/.75f) + 1, 16));
addAll(c);
}
ä¹ä¸addAllæ¹æ³ï¼
public boolean addAll(Collection<? extends E> c) {
boolean modified = false;
for (E e : c)
if (add(e))
modified = true;
return modified;
}
åä¸æ¬¡è¿è¡äºéç¯ï¼æ以æçæ¯è¾ä½ï¼binggo
温馨提示:答案为网友推荐,仅供参考
第1个回答 2016-11-03
你想问啥?????