Java interview container, Collection,list,set

Source: Internet
Author: User

1. A container refers to an object that can hold other objects.

2.collection/set/list of the relationship and difference?

(1) collection is a Java collection top-level interface that stores a set of not unique, unordered objects;

(2) The list interface and the set interface are the two sub-interfaces of the collections interface;

(3) List stores a set of objects that are not unique, ordered (in order of insertion);

The set interface stores a set of unique, unordered objects;

What are the characteristics and differences between 3.set and list?

(1) List:

1) is an ordered collection that uses this interface to precisely control where each element is inserted. The user is able to use the index to access the elements in the list, similar to the Java array.

2) Any method that can manipulate the index is a unique method of the list interface.

(2) Set:

1) The interface stores a set of unique, unordered objects (the order in which they are deposited and taken out is not necessarily consistent).

2) The method of manipulating data is similar to list, there is no index-related method for the set interface.

4.collection interface common methods;

Add (Object obj), which is a reference to the object that is stored;

Size (): The actual number of elements in the container;

Remove (Object obj): Deletes an element;

Remove (Collection c): Removes the same element as the element in the set C;

Retainall (Collection C): Preserves only the same elements in the current collection as in Set C;

Contains (Object obj): Determines whether the element obj exists in the collection;

IsEmpty (): Determines whether the set is empty;

Iterator (): An iterator that generates this collection;

5.addAll (c) is to add elements of set C to the set C2;

Collection C = new HashSet ();

Collection C2 = new HashSet ();

Apple a = new Apple ();

C.add (a);

C2.addall?;

Add all the elements in another container!

C2.add (c) is to add the set C as an element to the set C2;

Collection C = new HashSet ();

Collection C2 = new HashSet ();

C.add (a);

C2.add (c);

6:

object[] O0bjarr = new OBJECT[5];

Objarr[0] = 56;

OBJARR[1] = 123;

OBJARR[2] = "HelloWorld";

OBJARR[3] = new Dog ();

OBJARR[4] = new Cat ();

7. In List

Add (int index,e element): Elements element is added to the position indexed to index;

Get (int index): Gets the element indexed to index.

8.ArrayList and LinkedList:

Both implementations of the list interface, all have the elements in the list is ordered, not unique features.

ArrayList implements a variable-length array that allocates contiguous space in memory. The efficiency of traversing elements and random access elements is high;

0

1

2

3

4

5

 

aaaa

dddd

CCCC

aaaa

eeee

dddd

 

The LinkedList uses the list storage method. High efficiency when inserting and deleting elements.

The 9.ArrayList implements a variable-length array, allocating contiguous space in memory, and traversing elements and random-access elements are relatively high.

Add (int index,object obj)

AddAll (int index,collections c)

Remove (int index)

Set (int index,object obj): Replaces the element at the index position with the element obj.

Get (int index)

sublist (int beginindex,int endIndex);

Java interview container, Collection,list,set

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.