集合的概念:
对象的容器,和数组类似,定义了对多个对象进行操作的常用方法。
List集合:
有序、有下标、元素可以重复。(ArrayList(存储结构数组)、LinkedList(存储结构双向链表)、Vector)
/*
ArrayListDemo
*/
public class ArrayListDemo {
public static void main(String[] args) {
ArrayList<String> a = new ArrayList<String>();
// objects.add("sfa");
// objects.add(123);
//
// for (Object object : objects) {
// String s = (String)object;
// System.out.println(object);
a.add("sfagsa");
a.add("fafaga");
a.add("3532");
for (String s : a) {
System.out.println(s);
}
}
}
/*
Vector
*/
public class VectorDemo {
public static void main(String[] args) {
Vector vector = new Vector();
vector.add("我");
vector.add("你");
vector.add("它");
//删除
// vector.remove(0);
// vector.remove("它");
// vector.clear();
//遍历——枚举的方式
Enumeration elements = vector.elements();
while (elements.hasMoreElements()){
String string = (String)elements.nextElement();
System.out.println(string);
}
//判断
System.out.println(vector.contains("你"));
System.out.println(vector.isEmpty());
}
}
Set集合:
无序、无下标、元素不可以重复。(HashSet(哈希表+数组+链表+红黑树提高效率)、TreeSet(红黑树)、特例LinkedHashSet(哈希表+链表,区别于Set集合,LinkedHashSet是有序的))
/**
* HashSet集合的使用
* 存储结构:哈希表(数组+链表+红黑树)
*/
public class HashSetDemo01 {
public static void main(String[] args) {
//创建集合
HashSet<Student> students = new HashSet<>();
//添加数据
Student s1 = new Student("liupeng", 23);
Student s2 = new Student("Huang", 21);
Student s3 = new Student("Orz", 25);
students.add(s1);
students.add(s2);
students.add(s3);
//不重复
// students.add(s2);
//可以添加,new了个不同名的数据
students.add(new Student("liupeng",23));
System.out.println("元素个数:"+students.size());
System.out.println(students.toString());//排列无序
//删除
// students.remove(s1);
// System.out.println(students.toString());
//遍历
for (Student student : students) {
System.out.println(student.toString());
}
System.out.println("--------------------------------");
Iterator<Student> iterator = students.iterator();
while (iterator.hasNext()){
System.out.println(iterator.next());
}
//判断
System.out.println(students.contains(s1));
System.out.println(students.isEmpty());
}
}
/*
TreeSet存储结构:红黑树
无序,不重复
*/
public class TreeSetDemo {
public static void main(String[] args) {
//创建集合
TreeSet<String> treeSet = new TreeSet<>();
//添加
treeSet.add("abc");
treeSet.add("xyz");
treeSet.add("hello");
System.out.println("元素个数:"+treeSet.size());
System.out.println(treeSet.toString());
//删除
// treeSet.remove("abc");
// System.out.println("元素个数:"+treeSet.size());
// System.out.println(treeSet.toString());
//遍历
for (String s : treeSet) {
System.out.println(s);
}
//迭代器遍历
Iterator<String> iterator = treeSet.iterator();
while (iterator.hasNext()){
System.out.println(iterator.next());
}
//判断
System.out.println(treeSet.contains("xyz"));
}
}
Map集合:
存储一对数据,无序、无下标,键不可以重复,值可以重复。(HashMap(哈希表)、HashTable(几乎不用)、TreeMap(红黑树))
Collections:
集合工具类:
定义除了存取以外的集合常用方法:排序sort、二分查找binarySearch、复制copy、反转reverse、打乱shuffle等方法
public class CollectionDemo {
public static void main(String[] args) {
List<Integer> list = new ArrayList<>();
list.add(20);
list.add(5);
list.add(12);
list.add(9);
list.add(27);
System.out.println("排序之前:"+list);
//sort排序
Collections.sort(list);
System.out.println(list);
//二分查找binarySearch
int i = Collections.binarySearch(list,12);
System.out.println(i);
//copy复制
List<Integer> dest = new ArrayList<>();
//需要使两个的长度一样
for (int n=0; n<list.size();n++){
dest.add(0);
}
Collections.copy(dest,list);
System.out.println(dest);
//reverse 反转
Collections.reverse(list);
System.out.println(list);
//shuffle打乱
Collections.shuffle(list);
System.out.println(list);
//list转换成数组
Integer[] arr = list.toArray(new Integer[0]);
System.out.println(arr.length);
System.out.println(Arrays.toString(arr));
//数组转换成集合
String[] names = {"刘鹏","张三","李四"};
List<String> list12 = Arrays.asList(names);
System.out.println(list12);
}
}