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…

JAVA07Object class, Date class, Calendar class, System class, packaging class, Collection, generics, List, Set, data structure, Collections

JAVA07Object class, Date class, Calendar class, System class, packaging class, Collection, generics, List, Set, data structure, Collections

1. Overview java.lang.Object class is in the java language The following class is the parent class of all classes. The other class Object is the root class of the class hierarchy. Each class uses Object as its superclass. All objects (including arrays) implement the methods of this class. It contains 11 methods. 1 package cn.itcast.demo01.demo01; 2 3 public class Demo01ToString { 4 public static void main(String[] args) { 5 //The Person class inherits the Object class by default, and you can use the toString method 6 //Create Person object 7 Person person = new Person(“张三”,19); 8 String s = person.toString(); 9 // Directly printing the name of the object is actually calling the toStirng method of the object person = person.toString 10 System.out.println(s);//cn.itcast.demo01.demo01.Person@75412c2f 11 System.out.println(person);//cn.itcast.demo01.demo01.Person@75412c2f 12 } 13 } 14 15 1 package cn.itcast.demo01.demo01; 2 3 public class Person { 4 private String name; //Private member variables 5 private int age;// Private member variable 6 //Add the corresponding construction method 7 8 9 public Person() { 10 } 11 // Directly printing the address value of the object is meaningless, you need to override the toString method 12 13 14 @Override 15 public String toString() { 16 // Return super.toString()…

[Java Basics Consolidation Series] Java data collection, List, Map, Set, JUC, everything you need

Popular Series: [Java Basics Consolidation Series] Java Memory Overflow and Memory Leak [Java Basics Consolidation Series] Java class initialization execution sequence [Java Basics Consolidation Series] The most comprehensive technical architecture mind map for advanced Java advancement [Java Basics Consolidation Series] Understanding Java Parental Delegation Mechanism Programming Life,Wonderful Preview Table of contents 1. Preface 2. Collection framework 3 .Set interface 3.1 The difference between Set and List 4. Commonly used collection implementation classes 4.1 ArrayList 4.2 LinkedList 4.3 HashMap 4.4 HashTable 4.5 ConcurrentHashMap 4.6 TreeMap 4.7 HashSet 5 .Five commonly used concurrency packages 5.1 ConcurrentHashMap 5.2 CopyOnWriteArrayList 5.3 CopyOnWriteArraySet 5.4 ArrayBlockingQueue 5.5 LinkedBlockingQueue 6. Summary 1. Preface Data storage includes a variety of data structures. But overall it can be divided into four categories: linear tables, hash trees, and graphs. And our java actually also has its own commonly used “data structure” – that is, collections. But the underlying implementation of collections in Java is also implemented by data structures such as arrays, linked lists, and trees. Below we will give a certain in-depth explanation of some commonly used collections. 2. Collection framework As a Java developer, you must know all the commonly used collections. Let’s use a picture to list…

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…

[Written interview questions] Java collection related interview questions List, Map, Set, etc.

1. List 1、subList Does not return newlistobjects—is different from StringsubString Return the part of the original list from [fromIndex,toIndex) View, in fact, the returned list is supported by the original list. To the original list and the returned list The “non-structural changes”(non-structuralchanges) made will affect each other. Supplement: How to delete ListData in a certain range list.subList(from,to).clear();–Release the element and clear the internal attributes 2, linked list and sequence list The speed of addition and deletion is determined by the position of the element 3, ListImplementationRandomAccess Interface Indicates support for fast random access And depending on whether to implement this interface, it is decided to use sequence binary search (forloop traversal) or iterator binary search indexedBinarySerach/iteratorBinarySerach ArrayList中forLoop traversal is faster than iterator traversal 4, ArrayList expansion (1) is first called when using add ensureCapacityInternalmethod, pass in size+1 and check whether it needs to be expanded elementData The size of the array; (2)newCapacity=Expand the array to the original1.5 times (3) Use the grow method to expand the capacity (4) Use System.arraycopy to copy the original array into the copyarray 5, Array and ArrayList Storage type Whether to specify the size Two , Map 1, HashMap Underlying layer Jdk1.7: Array+Linked list [Head…

Java basic collection framework – Overview of List, Set, and Map (java collection 1)

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

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–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…

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…

Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: [email protected]

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
首页
微信
电话
搜索