Dark Horse Programmer – Java’s collection concept: the relationship between Coleection, List, Map and Set

Question: The relationship between Coleection, List, Map and Set. This article was formed during the basic testing stage of Dark Horse. . . By searching for information and API, form the following. Maybe my thinking is wrong, but it is the result of my thinking. However, it is indeed possible that it is a blind man trying to grasp the elephant, which seems to be taken out of context, because it is not considered in the entire framework of Java. After learning the whole thing and having a sense of the framework, let’s look back at it. Without further ado, here is the text: The concept of collections in Java has multiple forms, and these forms are distinguished by different ways and methods of processing data, that is, data structures. There are four forms: Coleection, List, Map, Set. (Speaking of data structures, I think of the stacks, linked lists, binary searches, hash trees, and arrays I have learned…) One is a collection, which is used to store a set of opposing elements. Usually these elements obey certain rules, and only one element can be stored in each position. There is no specified relationship between elements, and duplication is allowed. Element;…

Java container classes Collection, List, ArrayList, Vector and map, HashTable, HashMap difference Collection is the base interface of the two interfaces List and Set

Java container classes Collection, List, ArrayList, Vector and map, HashTable, HashMap difference Collection is the base interface of the two interfaces List and Set

Differences between Java container classes Collection, List, ArrayList, Vector and map, HashTable, HashMap Collection is the base interface of List and Set interfaces List adds “ordered” on top of Collection Set adds “on top of Collection” Only” ArrayList is a class that implements List… so it is ordered. The elements stored in it have a certain order in arrangement And ArrayList is stored in an array Element Another type of List, LinkedList, uses a linked list. The main difference between Collection and Map interfaces is that Collection stores a set of objects, while Map stores key/value pairs. In a Map object, each keyword has at most one associated value. Map: cannot contain two identical keys, and a key can be bound to at most one value. Null can be used as a key, and there is only one such key; there can be one or more keys corresponding to a value of null. When the get() method returns a null value, it can mean that the key does not exist in the Map, or it can also mean that the value corresponding to the key is null. Therefore, in Map, you cannot use the get() method to determine whether a…

Java basic collection framework (1)–Collection, List, LinkedList, HashSet

Java basic collection framework (1)–Collection, List, LinkedList, HashSet

Collection frame diagram: Collection interface: [java] view plaincopy import java.util.*; /* Collection defines the common functions of the collection framework. 1, add add(e); addAll(collection); 2, delete remove(e); removeAll(collection); clear(); 3, judge. contains(e); isEmpty(); 4, get iterator(); size(); 5, get the intersection. retainAll(); 6, collection variable array. toArray(); 1. The parameter type of the add method is Object. to receive any type of object. 2. All objects stored in the collection are references (addresses) What is an iterator? In fact, it is a way to remove elements from a collection. It’s like the clip in the doll game machine. The iterator is a retrieval method and will directly access the elements in the collection. So the iterator is described in the form of an inner class. Obtain the object of the inner class through the iterator() method of the container. */ class CollectionDemo { public static voidComplex elements. */ class HashSetTest { public static void sop(Object obj) { System.out.println(obj); } public static void main(String[] args) { HashSet hs = new HashSet(); hs.add(new Person(“a1” ,11)); hs.add(new Person(“a2”,12)); hs.add(new Person(“a3” ,13)); // hs.add(new Person(“a2”,12)); // hs.add(new Person(“a4”,14)); //sop(“a1:”+hs.contains(new Person(“a2”,12))); // hs.remove(new Person(“a4”,13)); Iterator it = hs.iterator(); while(it.hasNext())                                                                    Person p = (Person)it.next(); sop(p.getName()+“::”+p.getAge());…

Java container class analysis: Collection, List, ArrayList

1. Iterable and Iterator Iterable is an interface. Implementing this interface allows a collection object to traverse its own elements through an iterator. public interface Iterable Modifiers and return values Method name Description Iterator iterator() Returns an iterator whose internal elements are of type T default void forEach(Consumer action) Traverse internal elements and perform specified operations on the elements default Spliterator spliterator() Creates and returns a divisible iterator The first interface iterator() was introduced in jdk1.5 and requires subclasses to implement an internal iterator Iterator to traverse elements. The last two interfaces are newly added after JDK1.8. forEach (Consumer action) is to facilitate traversal of elements in the operation collection, and spliterator() provides an iterator that can traverse elements in parallel to adapt to current CPUs. The need for parallel traversal in the multi-core era. Among them we can look at the default modifier, which is also new after Java 8. We know that if we add a new method to an interface, then all All its specific subclasses must implement this method. In order to expand new functions to the interface without requiring each subclass to implement this method, Java 8 has added the default keyword, and the method…

List, Set and Map in Java collections

List, Set and Map in Java collections

Overview: List, Set, and Map are all interfaces; List and Set inherit from the Collection interface, and Map is an independent interface Under Set there are HashSet, LinkedHashSet, and TreeSet Under List there are ArrayList, Vector and LinkedList Under Map there are Hashtable, LinkedHashMap, HashMap, and TreeMap There is also a Queue interface under the Collection interface and a PriorityQueue class Note: The Queue interface is at the same level as List and Set, and both inherit the Collection interface. Looking at the picture, you will find that LinkedList can implement both the Queue interface and the List interface. However, LinkedList implements the Queue interface. The Queue interface narrows the access rights to LinkedList methods (that is, if the parameter type in the method is Queue, you can only access the methods defined by the Queue interface, but cannot directly access the non-Queue methods of LinkedList). So that only appropriate methods can be used. SortedSet is an interface, and the elements in it (only the TreeSet implementation is available) must be ordered. Summary: Connection interface: — List ordered and repeatable ArrayListAdvantages: The underlying data structure is an array, which is fast to query and slow to add and delete. Disadvantages:…

Java–Collection class (Collection, List, Set, Map)

Java–Collection class (Collection, List, Set, Map)

In Java, all collection classes are in the java.util package. The difference between collections and arrays: 1. Arrays can store basic types or objects; collections can only store objects. 2. Only one type can be stored in an array; while a collection can have multiple types. First, let’s take a look at the framework of the entire collection class, represented by a picture. 1. Collection Collection is the parent interface of all collection classes, which defines the most basic operation methods of collection classes: Add object: boolean add(E e) Returns true if the addition is successful; if the object already exists and the collection does not allow duplicate elements, it returns false. Delete object: boolean remove(Object o) Deletes the elements in the collection that are equal to the value of object o (e.equals(e)==true) Since List allows duplicate elements, only the first element that is equal to o is deleted. boolean removeAll(Collection c) ​​Remove elements that are equal to all objects in collection c void clear(); Clear all elements in the collection, size = 0 Judge object: boolean contains(Object o)                                                                                                                                                                                                                        out out out out of the set, and returns true. Otherwise return false. boolean containsAll(Collection c) ​​Determine whether collection c…

[Reprint] Detailed explanation of the relationship between ArrayList, List, LinkedList, and Collection in java

[Reprint] Detailed explanation of the relationship between ArrayList, List, LinkedList, and Collection in java

Reprint link: http://www.cnblogs.com/liqiu/p/3302607.html Detailed explanation of the relationship between ArrayList, List, LinkedList, and Collection in java 1. Basic introduction (Set, List, Map) Set (set): The elements in the set are not ordered in a particular way, and there are no duplicate objects. Some of its implementation classes can sort objects in a collection in a specific way. List (list): The elements in the collection are sorted by index position. There can be duplicate objects, allowing the object to be retrieved according to the index position of the object in the collection. Map: Each element in the collection contains a pair of key objects and value objects. There are no duplicate key objects in the collection, and the value objects can be repeated. Some of its implementation classes can sort key objects in a collection. 2. Basic interfaces and types 1. Iterator interface This interface allows traversing all elements in the collection. There are three methods: public boolean hasNext(): Determine whether there is a next element. public Object next(): Get the next element. Note that the return value is Object and may require type conversion. If there are no more elements available, a NoSuchElementException is thrown. Before using this method, you…

UML demonstration of Java serialization 82Set, Collection, List, and Map

UML demonstration of Java serialization 82Set, Collection, List, and Map

1. UML Demonstration Collection Collection Collection Inheritance Structure Diagram 2. Set collection 1. Characteristics of List storage elements: ordered and repeatable. In order, what is the order in which they are stored and what order they are taken out. 2. Characteristics of Set storage elements: Unordered and non-repeatable. When they are stored in the same order, when they are taken out, they may not be in the original order. 3.SortedSet features Characteristics of storing elements: Unordered and non-repeatable, but the stored elements can be automatically sorted according to the size of the elements. 3. Introduction to the underlying data structure of commonly used collection classes 1. The bottom layer of ArrayList uses an array to store elements, so the ArrayList collection is suitable for querying, but not suitable for frequent random additions and deletions of elements. 2. The bottom layer of LinkedList uses a two-way linked list. This data structure stores data. Linked lists are suitable for frequent additions and deletions of elements, but are not suitable for query operations. 3. The bottom layer of Vector is the same as the ArrrayList collection, but Vector is thread-safe and less efficient, so it is rarely used now. 4. Map collection inheritance…

Java8 converts Map<Department,List> to Map<Department,List>

1> Brian Goetz..: Map<Dept, List> namesInDept = peopleInDept.entrySet().stream() .collect(toMap(Map.Entry::getKey, e -> e.getValue().stream() .map(Person::getName) .collect(toList()));

[Java] Characteristics and usage of Map, Set, List, Queue and Stack

Copyright Statement:Please respect the results of personal labor,Indicate the source for reprinting,Thank you! Map An object that maps keys to values. A map cannot contain duplicate keys – each key can only be mapped to at most one value. Some mapping implementations can explicitly guarantee their order – such as the TreeMap class – and other mapping implementations do not guarantee the order – such as the HashMap class. The element ‘#xff0c;’ in the Map can extract the key sequence and value sequence separately. Use keySet() to extract the key sequence, to generate a Set from all the keys in the map. Use values() to extract the value sequence, to generate a Collection of all values ​​in the map. Why one generates Set, and one generates Collection? That’s because the ,key is always unique,value allows duplication. Set A collection that does not contain duplicate elements. More precisely, set does not contain a pair of elements e1 and e2 that satisfies e1.equals(e2) and contains at most one null element. Contained elements cannot be accessed randomly Only one-way traversal can be achieved using Iterator Set has no synchronization method List Contained elements can be accessed randomly The elements are ordered Elements can…

Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: 34331943@QQ.com

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top
首页
微信
电话
搜索