JAVA class set important

Source: Internet
Author: User

Equivalent to a container, which only stores objects, and the length can be changed;
Array: it is also a container that stores objects and basic data types with immutable length;
First, write down the concept of a set: to accommodate different types of data is based on unknown, that is, java uses a limited collection class to accommodate infinite types of data objects;
In fact, a collection class is equivalent to a container. It is used to store objects and has a variable length;
The collection classes in java are divided into three types: Set, List, and Map );
Set: The elements are unordered and do not contain duplicate elements;
List: ordered and contains repeated elements through badge operations;
Map: To be continued .....;
I forgot to write an important point. The java. util. Collection interface is the most basic Set interface, which describes the Set and List types and interfaces;

Set ------- HashSet
Collection -----
Shortlist
List ------
Vector ------ Stack
Map ------ Hashtable ---- Properties
Universal Methods in the Collection interface:

Public boolean add (E e): add an element to the set. If the element is added successfully, true is returned. Otherwise, false is returned ;,

Public boolean remove (Object o): deletes a specified element from the element. If the deletion is successful, true is returned; otherwise, false is returned;
Public void clear (): deletes all elements in the set;
 
Public boolean contains (Object o): determines whether the set contains the specified element;
 
Public Object [] toArray (): returns an array containing all elements in the current set.
Public boolean isEmpty (): determines whether the set is empty;
Public int size: returns the number of elements in the set;
/*
Set: a container.
1. used to store objects.
2. The Container length is variable.
Because the internal data structure of the container for object loading is different,
Many of these containers have formed a system after continuous extraction.
This system is called a collection framework.
When we learn a system,
First, we need to clarify the basic functions of the system.
Demonstrate methods in Collection.
*/
Import java. util .*;
Class ArrayListDemo
{
Public static void main (String [] args)
{
// 1. Create a container through the Collection subclass object. In fact, all elements in the collection objects are referenced (addresses)
ArrayList al = new ArrayList ();
// 2. You can add some elements to the common Collection methods.
Al. add ("abc1 ");
Al. add ("abc2 ");
Al. add ("abc3 ");
Al. add ("abc4 ");
ArrayList al1 = new ArrayList ();
Al1.add ("qq1 ");
Al1.add ("qq2 ");
Al1.add ("qq3 ");
// Al1.add ("abc1 ");
// Add a bunch of elements.
// Al. addAll (al1 );
// 3. delete an element.
Al. remove ("abc3 ");
// 4, intersection.
// Boolean B = al. retainAll (al1 );
// System. out. println ("B =" + B );
// 5. Clear the set.
// Al. clear ();
// Obtain the length of the set.
// 6. determine an element. In fact, the equals method is used. Determine whether the elements are the same.
// Boolean B = al. contains ("abc22 ");
// System. out. println ("B =" + B );
// System. out. println (al. size ());
IteratorDemo ();
// System. out. println (al. toString (); // [abc1, abc2, abc4]
/*
Iterator it = al. iterator ();
While (it. hasNext ())
{
System. out. println (it. next () + "..");
}
Al. retainAll (al1 );
Iterator it1 = al. iterator ();
While (it1.hasNext ())
{
System. out. println (it1.next () + "--");
}
*/
}
Public static void iteratorDemo ()
{
ArrayList al = new ArrayList ();
Al. add ("abc1 ");
Al. add ("abc2 ");
Al. add ("abc3 ");
Al. add ("abc4 ");
/*
What is an iterator?
It is the object used to retrieve the elements in the set.
This object is implemented in different ways because the data structure of each container is different.
The iterator is the content in the container, so it is implemented through internal classes, that is, internal container encapsulation.
You only need to obtain this object through the iterator () method to operate the elements in the container.
The iterator is like a bucket in a large game machine. The iterator is the clip in the game machine!
*/
Iterator it = al. iterator ();
While (it. hasNext ())
{
System. out. println (it. next ());
}
For (Iterator it1 = al. iterator (); it1.hasNext ();)
{
System. out. println (it1.next ());
}
// System. out. println (it. next ());
// System. out. println (it. next ());
// System. out. println (it. next ());
// System. out. println (it. next ());
// System. out. println (it. next (); //. NoSuchElementException
}
}
/*
Create two containers, A B
Add elements to the container respectively.
Requirement: Obtain the elements in the intersection of container A and container B,
Print these elements.
*/
First, let's talk about List:
The List set includes ArrayList, LinkList, and Vector;
List Method:
Void add (int index, E element): insert the element object after index.
Boolean add (E e): Insert Object e to the end of the linked list;
E remove (int index): Delete the element with the specified index number in the linked list;
Boolean remove (Object o): deletes the first specified Element in the linked list;
 
E get (int index): gets the element of the specified index number in the linked list;
Int size (): returns the number of elements in the linked list;
 
Int indexOf (Object obj): If the obj element is found in the linked list, the index value of this element is returned. If no value is found,-1 is returned;
List <E> subList (int fromIndex, int toIndex): obtains the sublinked List from formindex to toIndex;
Void clear (): clears all stored elements in the linked list;
This example demonstrates the special method in the List interface.
You can use the badge to operate elements.
*/
Import java. util .*;
Class ArrayListDemo2
{
Public static void main (String [] args)
{
ArrayList al = new ArrayList ();
Al. add ("abc1 ");
Al. add ("abc2 ");
Al. add ("abc3 ");
System. out. println (al );
System. out. println ("-----------------------");
// 1. Add an element at the specified position.
Al. add (1, "haha ");
// 2. Modify the element at the specified position.
Al. set (2, "qq ");
// 3. Delete the element at the specified position.
Al. remove (0 );
// 4. Obtain the position of the specified element.
Int index = al. indexOf ("qq ");
System. out. println ("index =" + index );
// 5. Obtain the sublist.
List ll = al. subList (1, 2 );
System. out. println ("ll:" + ll );
// 6. Obtain the element.
For (int x = 0; x <al. size (); x ++)
{
System. out. println ("get (" + x + "):" + al. get (x ));
}
System. out. println (al );
}
}
Instance: ArrayList
Constructor
ArrayList ()
Create an empty list with an initial capacity of 10.
ArrayList (Collection <? Extends E> c)
Construct a list of elements that contain the specified collection. These elements are arranged in the order returned by the collection iterator.
ArrayList (int initialCapacity)
Creates an empty list with the specified initial capacity.
Method: (there are too many duplicates, and only the new ones are written)
Int lastIndexOf (Object o)
Returns the last index of the specified Element in the list, or-1 if the list does not contain an index.
E remove (int index)
Removes an element from the specified position in the list.
Boolean remove (Object o)
Removes the specified element that appears for the first time in the list (if any ).
Protected void removeRange (int fromIndex, int toIndex)
Remove all elements in the list between fromIndex (included) and toIndex (excluded.
Void trimToSize ()
Adjust the capacity of the ArrayList instance to the current size of the list.
Import java. util .*;
Class CollectionDemo
{
Public static void main (String [] args)
{
ArrayList arr = new ArrayList ();
Arr. add ("abc1 ");
Arr. add ("abc2 ");
Arr. add ("abc3 ");
Arr. add ("abc4 ");
System. out. println (arr. size ());
Arr. remove ("abc3 ");
Arr. clear ();
System. out. println (arr. contains ("abc3 "));
System. out. println (arr );
}
}
Sort list class:
Constructor:
Partition list ()
Creates an empty list.
Collections list (Collection <? Extends E> c)
Constructs a list of elements in the specified collection. These elements are arranged in the order returned by the collection iterator.
Common method:
 
Void addFirst (E e)
Insert the specified element at the beginning of the list.
Void addLast (E e)
Add the specified element to the end of the list.
Void clear ()
Remove all elements from this list.
Boolean contains (Object o)
Returns true if the list contains the specified element.
E element ()
Obtain but not remove the header (the first element) of this list ).
E get (int index)
Returns the element at the specified position in the list.
E getFirst ()
Returns the first element of the list.
E getLast ()
Returns the last element of the list.
Int indexOf (Object o)
Returns the index of the specified element that appears for the first time in this list. If this element is not included in this list,-1 is returned.
Int lastIndexOf (Object o)
Returns the index of the last specified element in this list. If this element is not included in this list,-1 is returned.
ListIterator <E> listIterator (int index)
Returns the list iterator of the elements in this list (in the proper order), starting from the specified position in the list.
E remove ()
Obtains and removes the header (the first element) of this list ).
E remove (int index)
Removes the element at the specified position in the list.
Boolean remove (Object o)
Remove the specified Element (if any) that appears for the first time from the list ).
E removeFirst ()
Remove and return the first element in this list.
Import java. util .*;
Class program listdemo
{
Public static void main (String [] args)
{
Linked List link = new linked list ();
Link. addFirst ("abc1 ");
Link. addFirst ("abc2 ");
Link. addFirst ("abc3 ");
Link. addFirst ("abc4 ");
While (! Link. isEmpty ()){
System. out. println (link. removeFirst ());
}
}
}
/* Output
Abc4
Abc3
Abc2
Abc1 */

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.