tools
Demo01
package tools;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
/**
* @qvthor liuwenzheng
* @date 2021/5/8 11:25
*/
public class Demo01 {
public static void main(String[] args) {
String str[] = {"中文","计算机","ABC","123","qq@.com"};
List<String>list = Arrays.asList(str); //Arrays.aslist() 将数组转换成列表
System.out.println(list);
//2将数组转换成数组列表(链式列表) new ArrayList<>(Arrays.asList(str));
ArrayList<String> alist = new ArrayList<>(Arrays.asList(str));
LinkedList<String>llist = new LinkedList<>(Arrays.asList(str));
alist.remove("abc");
llist.add("123123");
System.out.println(alist);
System.out.println(llist);
//3将列表转换成数组 列表.toArray();
Object[] str2 = alist.toArray();
for (Object o :str2){
System.out.println(o+"");
}
System.out.println();
Object[] str3 = llist.toArray();
for (Object o : str3){
System.out.println(o + "");
}
}
}
Demo02
package tools;
import java.util.Arrays;
import java.util.HashSet;
/**
* @qvthor liuwenzheng
* @date 2021/5/10 8:13
*/
public class Demo02 {
public static void main(String[] args) {
String str[] = {"计算机","中文","abc","qq@qq.com","abc"};
//将数组转换为集合
HashSet<String>set = new HashSet<>(Arrays.asList());
System.out.println(set);
//将集合转换为数组
Object[]arr = set.toArray();
for (Object a : arr){
System.out.println(a);
}
}
}
Demo03
package tools;
import java.util.*;
/**
* @qvthor liuwenzheng
* @date 2021/5/10 8:22
*/
public class Demo03 {
public static void main(String[] args) {
HashMap<String,String> map = new HashMap<>();
map.put("1","a");
map.put("2","b");
map.put("3","c");
// 1 keySet()
Set<String>keys = map.keySet();
System.out.println(keys);
// 2 把set转换为其他集合
ArrayList<String> alistkeys = new ArrayList<>(keys);
System.out.println(alistkeys);
// 3去除map的值 value
Collection<String> v = map.values();
System.out.println(v);
// 4 把值转换成其他的集合 new LinkedList<>(v);
LinkedList<String> llistv = new LinkedList<>(v);
System.out.println(llistv);
}
}
Demo04迭代迭器
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
/**
* @qvthor liuwenzheng
* @date 2021/5/10 9:08
*/
public class Demo04 {
public static void main(String[] args) {
HashMap<Integer,String> map = new HashMap<>();
for (int i = 0 ; i < 20000; i++){
map.put(i , "haha" + i);
}
// 迭代器 减少资源占用
Iterator<Map.Entry<Integer, String>> iterator = map.entrySet().iterator();
while (iterator.hasNext()){
Map.Entry<Integer, String> next = iterator.next();
System.out.println(next);
}
}
}
Demo05
package tools;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
/**
* @qvthor liuwenzheng
* @date 2021/5/10 9:38
*/
public class Demo05 {
public static void main(String[] args) {
ArrayList nums = new ArrayList<>();
nums.add(1);
nums.add(3);
nums.add(4);
nums.add(5);
nums.add(2);
// 1 排序
Collections.sort(nums);
System.out.println(nums);
// 2 反转
Collections.reverse(nums);
System.out.println(nums);
// 3 混洗
Collections.shuffle(nums);
System.out.println(nums);
// 4 求最大小值
System.out.println(Collections.max(nums));
System.out.println(Collections.min(nums));
// 5 替换
Collections.replaceAll(nums,0,100);
// 6 求频率
System.out.println(Collections.frequency(nums,100));
// 7 二分搜索 , 使用的条件是集合是有序的
Collections.sort(nums);
System.out.println(nums);
int i = Collections.binarySearch(nums,3);
System.out.println("位置" + i);
}
}