In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces what the Java collection is, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.
Java collection 1. What is a collection?
Concept: a collection is a container for storing objects, and defines common methods for manipulating multiple objects, which can realize the function of arrays.
The difference between and array:
(1) the length of the array is fixed and the length of the set is not fixed.
(2) arrays can store basic types and reference types, and collections can only store reference types.
Location: java.util.*
2.Collection system set
3.Collection interface
The Collection interface defines many operations for collections through top-level design, which can be roughly divided into several methods, such as adding, deleting, traversing, judging, and so on.
Increase
Boolean add (E e) ensures that this collection contains the specified elements (optional). Boolean addAll (Collection c) removes all elements (optional actions) of all elements contained in this collection (optional actions). Default boolean removeIf (Predicate c) retains only the elements contained in the specified collection (optional operation).
Ergodic
Iterator iterator () returns an iterator for the elements in this collection. Object [] toArray () returns an array containing all the elements in this collection. T [] toArray (T [] a) returns an array containing all the elements in this collection; the runtime type of the returned array is the runtime type of the specified array.
Judge
Boolean contains (Object o) returns true if the collection contains the specified element. Boolean containsAll (Collection c) returns true if this collection contains all the elements of the specified collection. Boolean isEmpty () returns true if the collection does not contain any elements.
Other
Int size () returns the number of elements in this collection. Boolean equals (Object o) compares the specified object to this collection for equality. Int hashCode () returns the hash code value of this collection. The use of 4.Collection
Two commonly used methods of traversal: forEach and Iterator
Public class Demo1 {public static void main (String [] args) {Collection collection = new ArrayList (); collection.add ("apple"); collection.add ("banana"); collection.add ("watermelon"); / / 1.foreach for (Object object: collection) {System.out.println (object);} / * * 2. Using Iterator, it includes three methods: * hasnext (): determine whether there is a next element * next (): get the next element * remove (): delete the last element returned by the iterator * / Iterator it = collection.iterator (); while (it.hasNext ()) {String object = (String) it.next () / / collection.remove (object); cannot use the deletion method of the traversed collection. Concurrent modification exception it.remove (); System.out.println (object);} System.out.println (collection.size ());}} 5.List subinterface (1) basic information will be reported.
Features: orderly, subscript, elements can be repeated.
List adds a lot of methods to the parent interface Collection.
Increase
Void add (int index, E element) inserts the specified element at the specified location in the list (optional operation). Boolean addAll (Collection list) reverses the order of the elements in the specified list. Static void shuffle (List list) Random substitution specifies that the list uses a random default source. Static void swap (List list, int I, int j) exchanges elements at the specified location in the specified list. Static void copy (List
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.