In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
Java Set, List, Map what is the difference between the three, many novices are not very clear about this, in order to help you solve this problem, the following small series will be explained in detail for everyone, there are people who need this can learn, I hope you can harvest.
JAVA collections are mainly divided into three types: Set, List and Map.
Collection interface: Collection is the most basic collection interface, declaring common methods applicable to JAVA collections (only Set and List). Set and List both inherit Conllection, Map
Methods of the Collection interface:
boolean add(Object o): Adds a reference to an object to the collection
void clear(): deletes all objects in the collection, i.e. no longer holds references to them
boolean isEmpty(): Determines if the collection is empty
boolean contains(Object o): Determines whether a collection holds a reference to a particular object
Iterartor iterator(): Returns an Iterator object that can be used to iterate over elements in a collection
boolean remove(Object o): Remove a reference to an object from the collection
int size(): Returns the number of elements in the collection
Object[] toArray(): Returns an array containing all the elements in the collection
Both Iterator() and toArray() methods are used for all elements of a collection, the former returning an Iterator object and the latter an array containing all elements of the collection.
The Iterator interface declares the following methods:
hasNext(): Determine whether the elements in the collection have been traversed, if not, return true
next(): Returns the next element
remove(): removes the last element returned by the next() method from the collection.
Set: Set is the simplest kind of set. The objects in the collection are not sorted in a particular way and there are no duplicate objects. The Set interface mainly implements two implementation classes:
HashSet: The HashSet class accesses objects in the collection according to the hash algorithm, and the access speed is relatively fast.
TreeSet: The TreeSet class implements the SortedSet interface, enabling sorting of objects in a collection.
Usage of Set: What is stored is a reference to the object, no duplicate object
Set set=new HashSet();
String s1=new String("hello");
String s2=s1;
String s3=new String("world");
set.add(s1);
set.add(s2);
set.add(s3);
System.out.println(set.size());//Print the number of objects in the collection is 2.
How does the add() method of Set determine whether an object is already stored in the set?
boolean isExists=false;
Iterator iterator=set.iterator();
while(it.hasNext()) {
String oldStr=it.next();
if(newStr.equals(oldStr)){
isExists=true;
}
}
List: A List is characterized by its elements being stored linearly, allowing duplicate objects to be stored in the collection.
The main implementation classes of List interface include:
ArrayList() : Represents an array of variable length. Random access to elements is possible, and insertion and deletion of elements into ArrayList() is slow.
LinkedList(): Uses a linked list data structure in the implementation. Fast insertion and deletion, slow access.
Random access to a List is to randomly retrieve only elements located at a particular location. The get(int index) method of List puts back the object in the collection at the index position specified by the parameter index, with subscripts starting with "0." There are two basic ways to retrieve all objects in a collection:
1: for loop and get() method:
for(int i=0; i
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.