Does javaset inherit map_[Java interview questions] 35. Do List, Set, and Map inherit from Collection interface?
Collection is the most basic collection interface and declares common methods applicable to JAVA collections (including only Set and List). Both Set and List inherit Collection;Set has exactly the same interface as Collection,so it does not have any extra functionality,unlike there are two different Lists before. In fact, Set is Collection, but its behavior is different. (This is a typical application of inheritance and polymorphic ideas – different behaviors.) Set does not save duplicate elements (it is more responsible for how to judge that the elements are the same) Map does not inherit from the Collection interface When retrieving elements from a Map collection, “as long as the key object is given”, the corresponding value object will be returned. 1. The difference between Collection and Map The number of elements stored in each container is different. Collection type has only one element at each position. The Map type holds key-value pairs like a small database. 2. The relationship between their respective subclasses Collection -List: will store elements in a specific order. So the order of taking them out may be different from the order of putting them in. –ArrayList / LinkedList / Vector –Set : cannot contain duplicate elements –HashSet…
What are the specific differences between List, Set, and Map in Java?
List: Ordered and Repeatable ArraryList is one of the implementation classes of List, our commonly used collection, the bottom layer is Dynamic array , which can store null values and any type of data; its initialization size in the source code is private static final int DEFAULT_CAPACITY = 10; This collection is automatically expanded; it is thread-unsafe and supports random access LinkList is one of the implementation classes of List, and the bottom layer is bidirectional Linked list , it not only has the basic operations of ArraryList but also get, remove, insert methods; but it cannot be accessed randomly, so the query speed is faster than ArraryList Slow, because its bottom layer is a linked list, so the speed of adding and deleting is much faster than ArraryList Vector is one of the implementation classes of List, which is similar to ArraryList. ArraryList has some operations They all have it. The bottom layer is a dynamic array, but it is synchronized, which means it is thread-safe Set: Unordered and non-repeating HashSet can be said to be the fastest set in terms of query speed. Its internal principle is HashCode, which allows storage of And there is only one null…
Java basic collection framework – Overview of List, Set, and Map (java collection 1)
java list Array Container io hash tree get insert Write your review! Come on, watch it all Member login | User registration Recommended reading io Detailed explanation of the three ways to parse XML files in Android (DOM, PULL, SAX) Detailed explanation of the three ways to parse XML files in Android (DOM, PULL, SAX) – 1.xml file code… [detailed] Crayon Shin-chan 2023-09-25 14:33:53 io How el expression calls java, el expression usage List of contents of this article: 1. How to call the contains method of java in el expression… [detailed] Crayon Shin-chan 2023-09-25 16:12:49
Brief notes on Collection, Set, Map, and generics in Java
What is a collection Concept A container for objects that implements common operations on objects Differences from arrays The length of the array is fixed, but the length of the collection is not fixed Arrays can store basic types and reference types, while collections can only store reference types Location java.util.*; Collection system Collection parent interface Features: Represents a set of objects of any type, unordered, unsubscripted, and cannot be repeated. Create a collection Collection collection = new ArrayList(); Common methods Add elements collection.add(); Delete element collection.remove(); collection.clear(); Traverse elements (emphasis) Use enhanced for (because there is no subscript) for(Object object : collection){ } Using iterators //haNext(); Is there a next element? //next(); Get the next element //remove(); delete the current element Iterator it = collection.iterator(); while(it.hasNext()){ String object = (String)it.next(); //Forced transfer //You can use it.remove(); to remove elements // collection.remove(); cannot use other methods of collection, and a concurrent modification exception will be reported. } Judge collection.contains(); collection.isEmpty(); List sub-interface Features: orderly, subscripted, elements repeatable Create a collection object List list = new ArrayList( ); Common methods Add element list.add( ); will automatically box the basic type To delete elements, you can use index list.remove(0) Force the numbers…
JAVA collection List, array, Set, Map, direct mutual conversion
Java collection conversion [ListArray, ListSet, ArraySet, Map–>Set, Map–>List] //List–>Array List list = new ArrayList(); list.add(“tom”); list.add(“Jerval”); list.add(“WeiSi”); Object[] objects = list.toArray();//Return Object array System.out.println(“objects:”+Arrays.toString(objects)); String[] strings1 = new String[list.size()]; list.toArray(strings1);//Put the converted array into the already created object System.out.println(“strings1:”+Arrays.toString(strings1)); String[] strings2 = list.toArray(new String[0]);//Assign the converted array to the new object System.out.println(“strings2:”+Arrays.toString(strings2)); //Array–>List String[] ss = {“JJ”,”KK”}; List list1 = Arrays.asList(ss); List list2 = Arrays.asList(“AAA”,”BBB”); System.out.println(list1); System.out.println(list2); //List–>Set List list3 = new ArrayList(new HashSet()); //Set–>List Set set = new HashSet(new ArrayList()); //Array–>Set String[] strs = {“AA”,”BB”}; Set set2 = new HashSet(Arrays.asList(strs)); System.out.println(set2); //Set–>Array Set set3 = new HashSet(Arrays.asList(“PP”,”OO”)); String[] strSet = new String[set3.size()]; set3.toArray(strSet); System.out.println(Arrays.toString(strSet)); //Map operation Map map = new HashMap(); map.put(“YYY”, “UUU”); map.put(“RRR”, “TTT”); //Convert key to Set Set mapKeySet = map.keySet(); //Convert value to Set Set mapValuesSet = new HashSet(map.values()); //Convert value to List List mapValuesList = new ArrayList(map.values());
Java Advanced Collections and Generics>Collection, Set, HashSet, LinkedHashSet, TreeSet
1. Commonly used methods of Collection: Java collections can be divided into three systems: Set, List and Map:Set: unordered, non-repeatable collection. List: An ordered, repeatable collection. Map: A collection with mapping relationships. Collection interface is the parent interface of List, Set and Queue interfaces. The methods defined in this interface can be used to operate both Set collection and List and Queue collection import java.util.ArrayList;import java.util.Collection;import java.util.HashSet;import java.util.Iterator;public class TestCollections { public static void main(String[] args) { //1. Create an object of Collection interface. Collection collection = new ArrayList(); //2. Collection important method description: /** * 2.1 For adding elements: * add() * addAll() span>*/ Person p1 = new Person(); collection.add(p1); collection.add(new Person()); Collection collection2 = new ArrayList(); collection2.add(new Person()); collection2.add(new Person()); collection.addAll(collection2); System.out.println(collection.size()); /** * 2.2 Methods for accessing collections: * Get the length of the collection: size() * Methods for traversing the collection: iterator( ) can get the corresponding Iterator interface object. * * Iterator: Iterator * ①. Get the Iterator interface object: * ②. Use while loop and Iterator object to traverse each item in the collection Element. Specifically use the * hasNext() and next() methods of the Iterator interface. */ Iterator iterator = collection.iterator(); while(iterator.hasNext()){ Object…