The knowledge of the data structure
1. Data Structure What is the use?
Rational use of data structures, you can more easily find the stored data.
2. The common data structure
Common data storage structure: stacks, queues, arrays, linked lists, and red-black tree.
- Stack: Stack (Stack), which is limited by a linear calculation table, only allow for insertions and deletions at one end of the table (stack) of. Characterized by last-out stack of inlet and outlet are at the top of the stack.
- Queue: Acronym Team (Queue), and its operation are the same as the linear form stacks limited, only allow one end inserted in the table, one end of the deletion. Characterized by a FIFO queue and an inlet half and an outlet side.
- Array: Array, it is an ordered sequence of elements. An array is a contiguous open space in memory and on the storage element. Find elements characteristic is fast (by the array index, you can quickly locate elements), add or delete elements slow (additions and deletions to each element creates a new array).
- List: linked list, a set of nodes (each element in the list are called Node) Node composition, node dynamically generated at runtime. Each node consists of two parts: a pointer field pointing to the next data field storing data and a node. Find features slow (only turn back look through node), add or delete elements fast (just edit link next node memory address can).
- Red-black tree: it belongs to a binary tree. Binary tree, binary tree, each node is no more than 2 of the ordered tree. Characteristics of red-black tree is the root node is black, black leaf nodes, child nodes of each node are black red, any node which is on all paths to the same leaf node of each black nodes.
Second, the collection
Collection of understanding
Collection is a container provided in Java, it can be used to store a plurality of data.
Collections and arrays difference:
- An array of fixed length set of variable length.
- Array can store the same type of element. Storing a set of object, type of object can be different.
1.
root interface separate collection class, for storing a series of elements meet certain rules.
It has two important sub-interfaces, respectively, java.util.List
and java.util.Set
.
List
Is characterized by the orderly elements, the elements can be repeated.Set
The disorder is characterized by an element, and not repeatable.
A separate set of common methods | Features |
public boolean add(E e) | Adds the specified object to the collection |
public void clear() | Empty all of the elements in the collection |
public boolean remove(E e) | Delete the specified object in the collection of the current |
public boolean contains(E e) | Determine whether the current object in the collection contains the specified |
public boolean isEmpty() | Determining whether the set is empty |
public int size() | Returns the number of elements in a set of |
public Object[] toArray() | Convert the collection array |
1.1 List collection
List interface common method | Features |
public void add(int index, E element) | The specified element to a specified location on the set of |
public E get(int index) | Returns a collection of elements specified position |
public E remove(int index) | Remove elements specified in the list position, it returns the elements removed |
public E set(int index, E element) | Location specified element is replaced with the specified elements in a set return element before update value. |
List
The main interface implementation class there java.util.ArrayList
andjava.util.LinkedList。
public class TestList { public static void main(String[] args) { // 创建List集合对象 List<String> list = new ArrayList<String>(); // 尾部添加 list.add("1"); list.add("2"); list.add("3"); // 指定位置添加 list.add(1,"没头脑"); // 删除索引位置为2的元素 System.out.println(list.remove(1)); // 修改指定位置元素 list.set(0, "5"); }
1.11 ArrayList 集合
1.12 LinkedList集合
LinkedList 方法 | 功能 |
public void addFirst(E e) | 将指定元素插入此列表的开头 |
public void addLast(E e) | 将指定元素添加到此列表的结尾 |
public E getFirst() | 返回此列表的第一个元素 |
public E getLast() | 返回此列表的最后一个元素 |
public E removeFirst() | 移除并返回此列表的第一个元素 |
public E removeLast() | 移除并返回此列表的最后一个元素 |
public E pop() | 从此列表所表示的堆栈处弹出一个元素 |
public void push(E e) | 将元素推入此列表所表示的堆栈 |
public boolean isEmpty() | 如果列表不包含元素,则返回true |
public class LinkedListDemo { public static void main(String[] args) { LinkedList<String> link = new LinkedList<String>(); //添加元素 link.addFirst("1"); link.addFirst("2"); link.addFirst("3"); System.out.println(link); // 获取元素 System.out.println(link.getFirst()); System.out.println(link.getLast()); // 删除元素 System.out.println(link.removeFirst()); System.out.println(link.removeLast()); while (!link.isEmpty()) { //判断集合是否为空 System.out.println(link.pop()); //弹出栈顶元素 } System.out.println(link); } }
1.2 set 集合
Set
接口的主要实现类有java.util.HashSet
。LinkedHashSet是HashSet子类。
区别:
HashSet存储元素无序。
LinkedHashSet
存储元素有序。-
public class TestHashSet{ public static void main(String[] args) { //创建 Set集合 HashSet<String> set = new HashSet<String>(); //添加元素 set.add(new String("1")); set.add("1"); set.add("2"); //遍历 for (String name : set) { System.out.println(name); } } }
public class TestLinkedHashSet { public static void main(String[] args) { Set<String> set = new LinkedHashSet<String>(); set.add("1"); set.add("2"); set.add("3"); set.add("4"); //迭代器 Iterator<String> it = set.iterator(); while (it.hasNext()) { System.out.println(it.next()); } } }
2 .
public static <T> boolean addAll(Collection<T> c, T... elements) | 往集合中添加一些元素 |
public static void shuffle(List<?> list) | 打乱集合顺序 |
public static <T> void sort(List<T> list) | 将集合中元素按照默认规则排序 |
public static <T> void sort(List<T> list,Comparator<? super T> ) | 将集合中元素按照指定规则排序 |
public class TestCollections { public static void main(String[] args) { ArrayList<Integer> list = new ArrayList<Integer>(); //添加元素 Collections.addAll(list, 1, 2, 3,4); //排序 Collections.sort(list);
} }
//Comparator
import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; public class TestCollections { public static void main(String[] args) { ArrayList<String> list = new ArrayList<String>(); list.add("cbae"); list.add("abafaa"); list.add("sba"); list.add("nb"); //排序方法 按照长度排序 Collections.sort(list, new Comparator<String>() { @Override public int compare(String o1, String o2) { return o2.length() - o1.length(); } }); System.out.println(list); } }
//Comparable public class TestStudent implements Comparable<Student>{ @Override public int compareTo(Student o) { return this.age-o.age;//升序 } }
- Comparator:强行对某个对象进行整体排序。可以将Comparator 传递给sort方法(如Collections.sort或 Arrays.sort),从而允许在排序顺序上实现精确控制。还可以使用Comparator来控制某些数据结构(如有序set或有序映射)的顺序,或者为那些没有自然顺序的对象collection提供排序。
3.
常用方法 | 功能 |
public V put(K key, V value) | 把指定的键与指定的值添加到Map集合中。 |
public V remove(Object key) | 把指定的键 所对应的键值对元素 在Map集合中删除,返回被删除元素的值。 |
public V get(Object key) | 根据指定的键,在Map集合中获取对应的值。 |
boolean containsKey(Object key) | 判断集合中是否包含指定的键。 |
public Set<K> keySet() | 获取Map集合中所有的键,存储到Set集合中。 |
public Set<Map.Entry<K,V>> entrySet() | 获取到Map集合中所有的键值对对象的集合(Set集合)。 |
import java.util.HashMap; public class TestMap { public static void main(String[] args){ //创建HashMap HashMap<String, String> map = new HashMap<String, String>(); //添加键值对 map.put("1","张三"); map.put("2","李四"); map.put("3","张三"); //获取键对应值 System.out.println(map.get("3")); //删除键,返回删除键的值 System.out.println(map.remove("2")); } }
Map中存在两种对象,key和value,Entry(项)将键值对封装成了对象。
-
-
public V getValue()
:获取Entry对象中的值。
-
public Set<Map.Entry<K,V>> entrySet()
: 获取到Map集合中所有的键值对对象的集合(Set集合)。
遍历Map集合
import java.util.HashMap; import java.util.Map.Entry; public class TestMap { public static void main(String[] args){ //创建HashMap HashMap<String, String> map = new HashMap<String, String>(); //添加键值对 map.put("1","张三"); map.put("2","李四"); map.put("3","张三"); for (Entry<String, String> entry : map.entrySet()) { // 解析 String key = entry.getKey(); String value = entry.getValue(); System.out.println(key+"的值:"+value); } } }
温馨提示
- 如果您对本文有疑问,请在评论部分留言,我会在最短时间回复。
- 如果本文帮助了您,也请评论关注,作为对我的一份鼓励。
- 如果您感觉我写的有问题,也请批评指正,我会尽量修改。