1. Set concept
Array (container): Create an array of specified length and use the array to store multiple data. When the program is running, the number of data changes, but once the array is given, it cannot be changed. Frequent expansion is not advisable. Arrays The advantage is that the query speed is fast
Collection (container): There are various operations on arrays, with many additions and deletions, and few queries. The linked list structure is suitable for programs. This kind of storage has different operation requirements. It is recommended to store the same type of data.
2. Collection API
3.List interface and implementation class
Let's talk about the Collection interface first - it defines methods for accessing a group of objects, and its sub-interfaces Set and List are defined respectively.
- The data objects in a Set have no order and cannot be repeated.
- The data objects in the List are ordered and repeatable.
ArrayList : List inherits the Collection interface and has three implemented classes. The bottom layer is an array. Query is fast, but addition and deletion are slow.
LinkedList : Array list, data is stored in array, the bottom layer is a linked list, addition and deletion are fast, but query is slow
Vect : Array list, add synchronization lock, thread-safe
- add(int index, E element)
- get(int index)
- indexOf(Object o)
- lastIndexOf(Object o)
- remove(int index) deletes and returns the element at the specified position
- removeRange (int fromIndex, int toIndex) deletes elements in the specified range (inherited by subclasses)
- set(int index, E element)
// 集合(容器):
// 数组(容器):创建一个指定长度的数组,使用数组来存储多个数据,
// 程序运行时,数据运行时数据数量是改变的,但是数组一旦给定,就不能频繁的扩容,数组优点是查询速度快
// 对数据操作多种多样的,增删多,查询少,链表结构针对程序中,这种不同存储操作需求
// collection(单列)
// list(可重复)
// set(不可重复)
// Map(多列)
public static void main(String[] args) {
Collection<String> a=new ArrayList<>();
a.add("a");
a.add("b");
a.add("c");
a.add("d");
Collection<String> b=new ArrayList<>();
b.add("e");
b.add("f");
b.add("g");
b.add("h");
// a.addAll(b);//集合全部添加到另一个集合中
// a.clear();//将集合中的所有元素全部清空
// System.out.println( a.equals(b));//比较俩个集合中的元素是否相等
// System.out.println(a.isEmpty());//判断是否为空集合
// a.remove("c");//在集合中指定删除某一个元素
// System.out.println( a.remove("c"));//成功为ture,失败为false
// System.out.println( a.retainAll(b));//保留取交集,成功为ture,失败为false
// System.out.println(a.size());//输出该集合的长度 //数组length是为属性,字符串length()为方法,集合size()为方法
// String [] sobject=a.toArray(new String [a.size()]);//将集合转化为指定类型
// System.out.println(Arrays.toString(sobject));
// Object [] b1=a.toArray();//转化成Object类型
System.out.println(a);
System.out.println(b);
- add(int index,Object element)
- addFirist(Object element)
- addLast(Object element)
- get(int index)
- removeFirst()
- removeLast()
- remove(int index)
- getFirst()
ArrayList<String> s=new ArrayList<>();
s.add("a");
s.add("b");
s.add("c");
s.add("d");
System.out.println(s.get(2));//根据索引得到指定位置的元素
s.remove(0);//删除并返回指定位置的元素
System.out.println(s);
s.set(0,"X");//替换指定元素并返回数组
System.out.println(s);
s.size();
System.out.println(s.size());//返回实际元素个数
s.addFirst("X");
s.addLast("X");
System.out.println(s.get(2));
System.out.println(s);
- for loop traverses
- Enhanced for loop traversal
- Iterator traversal (Iterator)
You must have mastered the for loop and enhanced for loop. Now I will show you the iterator loop:
迭代器遍历集合1:返回了一个ArrayList的内部类对象,实现Iterator接口
// 此类部类,专门用作对集合进行遍历时的控制
// ArrayList<String> llist=new ArrayList();
// llist.add("a");
// llist.add("b");
// llist.add("c");
// llist.add("d");
// llist.add("e");
// llist.add("f");
// llist.add("g");
// llist.add("g");
// Iterator<String> it=llist.iterator();
// while(it.hasNext()){//判断集合中是否有元素
// String e=it.next();//将下一个元素赋给e
// if(e.equals("g")){
// it.remove();//删除时用迭代器进行方法调用,底部有自动的计数器
// }
// }System.out.println(llist);
//迭代器集合2,提供ListIterator()方法
LinkedList<String> llist = new LinkedList();
llist.add("a");
llist.add("b");
llist.add("c");
llist.add("d");
llist.add("e");
llist.add("f");
llist.add("g");
llist.add("g");
ListIterator<String> it = llist.listIterator(llist.size());//此方法可以指定位置进行迭代
// while(it.hasNext()){
// String e=it.next();
// System.out.println(e);
// }
while (it.hasPrevious()) {//此方法可以将集合中的元素进行逆序遍历
String e = it.previous();
System.out.println(e);
}
}
Set interface
//set共同点:不可重复,无序(不是按照添加的顺序进行排序,杂乱),无序且不重复的集合
//有遍历俩种形式:1.增强for循环 2.迭代器迭代(无索引故不能使用for循环)
// Hashset:存储的元素不固定
// HashSet<String> hset=new HashSet<>();
// hset.add("a");
// hset.add("d");
// hset.add("b");
// hset.add("x");
// hset.add("l");
// hset.add("l");
// hset.add("l");
// System.out.println(hset);
//Treeset:可以按照元素的自然顺序进行排序,有序(根据元素的自然顺序进行排序)且不能存储重复元素
// TreeSet<String> hset=new TreeSet<>();
// hset.add("a");
// hset.add("d");
// hset.add("b");
// hset.add("x");
// hset.add("l");
// System.out.println(hset);
//可以把另外一个集合的元素赋给Treeset。前提是该集合必须和Comparable有关系或者是继承
// Demo3 demo1=new Demo3(1,"baoma");
// Demo3 demo2=new Demo3(2,"baoma");
// Demo3 demo3=new Demo3(3,"baoma");
// TreeSet<Demo3> a=new TreeSet<>();
// a.add(demo1);
// a.add(demo3);
// a.add(demo2);
// System.out.println(a);
Map interface
- V put(K key,V value)
- V remove(Object key)
- void clear()
- boolean containsKey(Object key)
- boolean containsValue(Object value)
- boolean isEmpty()
- int size()
- V get(Object key)
- Collection<V> values()
- Set<K> keySet()
-
Set<Map.Entry<K,V>> entrySet()
Map interface
- The key values of elements in HashMap cannot be repeated.
- The sort order is not fixed, and a null key can be stored.
- All elements in TreeMap maintain a certain fixed order. If you need to get an ordered Map, you should use TreeMap. The class where the key value is located must implement the Comparable interface.
- Synchronization is achieved.
- Keys cannot be stored as null
Map collection traversal
- Find value based on key
- Get the set of all keys
- Traverse the collection of keys and obtain each key
- Find value based on key
- Find the key and value of the object based on the key value
- Get a collection of all key-value pairs
- Traverse the collection of key-value objects and obtain each key-value object
- Find the key and value of the object based on the key value
map遍历的俩种方法
// HashMap<String,String> a=new HashMap();
// a.put("a","aa");
// a.put("b","bb");
// a.put("c","cc");
// System.out.println(a);
// for (String key:a.keySet()) {
// System.out.println("key:"+key+"value:"+a.get(key));
// }
HashMap<String,String> a=new HashMap();
a.put("a","aa");
a.put("b","bb");
a.put("c","cc");
for (Map.Entry<String,String> c:a.entrySet()) {
System.out.println("key:"+c.getKey()+"value:"+c.getValue());
}
}
● TreeMap
- Suitable for traversing keys in natural order or custom order.
- TreeMap is sorted according to the key value. The key value needs to implement the Comparable interface and override the compareTo method. TreeMap sorts keys according to the logic of compareTo.
- The key is a red-black tree structure, which can ensure the sorting and uniqueness of the key.
Notice:
HashMap structure: key-value pair, the key cannot be repeated, the value can be repeated, and a null value can be stored
Underlying storage structure: java8
Data structure: 1. Array (hash value)
2. Linked list
3. Red-black tree
The process of adding elements: Calculate its hash value based on the element. After calculation, calculate the position of the element in the array, then encapsulate the element into a Node object, store the encapsulated Node object in the corresponding location, and then add the element Store it in. If there is the same position, the new element will be stored in the next position of the element in the same position. When the length of the linked list reaches a certain length, it will be converted into a red-black tree.
The default length of hash array is 16
Load factor is 0.75
The hash array expansion is twice the original array length.
When the length of the linked list is 8 and the length of the hash array is greater than or equal to 64, the linked list will be converted into a red-black tree.
Collections class
- addAl l(Col lection<? super T> c, T... elements);
- binarySearch(List<? extends Comparable<? super T>> l ist, T key)
- sort(List<T> l ist)
- sort(List<T> l ist, Comparator<? super T> c)
- swap(List<?> l ist, int i, int j)
- copy(List<? super T> dest, List<? extends T> src) ; Note that dest size needs to be greater than or equal to src.size
- emptyList() returns an empty collection and cannot add data
- fi l l(List<? super T> l ist, T obj)
- max(Col lection<? extends T> col l)
- min(Col lection<? extends T> col l)
- replaceAl l(List<T> l ist, T oldVal, T newVal)
- reverse(List<?> l ist)
- shuffle(List<?> l ist) random sorting
- copy(dest,src) set copy
ArrayList<Integer> list=new ArrayList<>();
list.add(1);
list.add(5);
list.add(3);
list.add(6);
list.add(2);
list.add(4);
// Collections.addAll(list,6,9,8);//int...a 添加若干个元素
Collections.sort(list);
// System.out.println(Collections.binarySearch(list,2));//查找元素的索引
// ArrayList<Integer> list1=new ArrayList<>();
// list1.add(0);
// list1.add(0);
// list1.add(0);
// list1.add(0);
// list1.add(0);
// list1.add(0);
// list1.add(0);
// Collections.copy(list1,list);//复制,list1的size必须比list的size长
// System.out.println(list1);
// Collections.swap(list,1,3);
// Collections.fill(list,1);
// List<Integer> list1 =Collections.emptyList();
// System.out.println(list1);
// System.out.println(Collections.max(list));
// System.out.println(Collections.min(list));
System.out.println(list);