Basic knowledge of the Java language 12

Source: Internet
Author: User
Tags set set lenovo

14th chapter (Saving objects using the collection Class)

1. The collection object in Java is like a container, which is used to hold the object of the Java class. The collection classes in Java are easy to deposit and remove, while others are easy to find. The difference between collection classes and arrays is that the length of the array is fixed, the length of the collection is variable, the array is used to hold the base type, and the collection is used to hold the object's reference . Common collection classes have a list collection, a set set, and a map collection.

2. The list collection includes all the implementation classes of the list interface and the list interface. The elements in the list collection are duplicated, and the order of the elements is the order in which the objects are inserted. Similar to arrays in Java. The list class inherits the collection interface, so it contains all the methods in the collection, and since list is a list type, the interface also provides some common methods that are appropriate for itself, all related to indexes, which store objects in a linear manner. To use a list collection, you typically declare the list type first, and then instantiate the collection through the implementation class of the list interface: ArrayList and linklist. There are two ways to get an object in the List collection 1, through a For loop statement, to iterate through collection 2, by creating an iterator to the collection object.

We can assume that the iterator iterator is the position between the two elements, and we can use Hasnext () to determine if there are any elements behind the current position. Use the next () method to return the following element, and move the position to the position before the next element. for the Remove method, we delete the element that precedes the current position, so we must call the Remove () method at least once before calling the Take () method. The position of the iterator mentioned here does not exist in the object being iterated, but it is supposed to be an accurate illustration of the method.

With a For loop can still output, why write such an interface. This is so that we can use a common approach

For instantiating the list collection class

If you don't add <String> this, your list can be put in. When all types of objects are taken out, they become object objects that need to be cast with a mandatory type conversion and <String>. List can only save String It is also string not required to force type conversion

3. TheArrarylist class implements a mutable array , allowing all elements to be included, including null. The disadvantage is that inserting or deleting elements at a specified location is slow

Package Com.lenovo.dishisizhang;


Import java.awt.List;

Import java.util.ArrayList;


public class Arraylisttest {

String A = "a", z = "Z", C = "C", d= "D", apple= "Apple";

public static void Main (string[] args) {

Java.util.List List = new ArrayList ();

for (int i = (int) ' A '; I <= (int) ' z '; i++) {

List.add (i);

}

int i = (int) (Math.random () * (List.size ()-1));

SYSTEM.OUT.PRINTLN ("Randomly obtained number is:" +i);

SYSTEM.OUT.PRINTLN ("Randomly gets the elements in the collection:" +list.get (i));

List.remove (i);//////

SYSTEM.OUT.PRINTLN ("the element with index position 10 is:" +list.get (10));

}

}

4. The objects in the set collection are not sorted in a particular way, but are simply added to the collection, but cannot contain duplicate objects in the Set collection

The set set has a set interface and a set interface implementation class, the set interface inherits the collection interface, and therefore contains all the methods of the collection interface. The implementation classes for the set interface are HashSet and TreeSet

There are values that do not allow duplicates with the set collection, so you can use the AddAll () method in the Set collection to add the Collectiion collection to the set collection to remove duplicates.

Because the objects in the set collection are not circular, the result of traversing the set collection is not the same as the order in which the set is inserted

Package Com.lenovo.dishisizhang;


Import Java.util.HashSet;

Import Java.util.Iterator;

Import Java.util.Set;


Import Javax.management.loading.PrivateClassLoader;


public class People {

private String name;

Private long Id_card;

Public people (String Name,long Id_card) {

TODO auto-generated Constructor stub

THIS.name = name;

This.id_card=id_card;

}

public void SetName (String name) {

THIS.name = name;

}

Public String GetName () {

return name;

}

public void Setid_card (long Id_card) {

This.id_card = Id_card;

}

Public long Getid_card () {

return id_card;

}

public static void Main (string[] args) {

Set HashSet = new hashset<people> ();

Hashset.add (New People ("The Beauty Diaspora", 37));

Hashset.add (New people ("Beauty Diaspora 2", 38));

Hashset.add (New people ("Beauty Diaspora 3", 39));

iterator<people> Iterator = Hashset.iterator ();

SYSTEM.OUT.PRINTLN ("The element in the collection is:");

while (Iterator.hasnext ()) {

People person = iterator.next ();

System.out.println (Person.getname () + "" +person.getid_card ());

}

}

}

5, Map does not have a collection interface, which provides a key to value mapping, the map can not contain the same key value, each key value can only map a value, to use the Map collection, first declared as the map type and then instantiated through the map interface implementation class, The implementation class of the map interface is commonly used HashMap, TREEMAP

list<string> List = (list<string>) map.values ();

collection<string> list = Map.values ();

The first one is wrong, map.values () The return value is collection type whether the map interface is collection type Independent

Using the TreeMap class to implement the Map collection, you can make the Map collection objects exist in a certain order

Let's see what the difference is between HashMap and TreeMap. HashMap quickly finds its contents through Hashcode , while all elements in TreeMap remain in a fixed order, if  You need to get an ordered result you should use TREEMAP (the order of the elements in the HashMap is not fixed).

In the Map collection

values (): The method is to get all the values in the collection----no keys, no correspondence,

KeySet ():
Deposits all the keys in the map into the set collection. Because set has iterators. All of the keys can be iterated out and then based on the Get method. Gets the value corresponding to each key. KeySet (): Only the KE can be obtained after iteration via get ()

Package Com.lenovo.dishisizhang;


Import Java.util.HashMap;

Import Java.util.Iterator;

Import Java.util.Map;

Import Java.util.Set;

Import Java.util.TreeMap;


public class Maptest {

public static void Main (string[] args) {

Map map = new HashMap ();

EMP emp = new EMP ("001", "Zhang San");

EMP EMP2 = new EMP ("002", "Zhang Er");

EMP Emp3 = new EMP ("003", "Zhang ers");

Map.put (emp.gete_id (), Emp.gete_name ());

Map.put (emp2.gete_id (), Emp2.gete_name ());

Map.put (emp3.gete_id (), Emp3.gete_name ());

Set set = Map.keyset ();

Iterator Iterator = Set.iterator ();

SYSTEM.OUT.PRINTLN ("HashMap Realization of the Union without Order:");

while (Iterator.hasnext ()) {

String string = (string) iterator.next ();

String name = (string) map.get (string);

System.out.println (string+ "" +name);

}

TreeMap TreeMap = new TreeMap ();

Treemap.putall (map);

Iterator it = Treemap.keyset (). Iterator ();

System.out.println ("TreeMap class implements the Map collection, the object is built in ascending order:");

while (It.hasnext ()) {

String string = (string) it.next ();

String name = (string) map.get (string);

System.out.println (string+ "" +name);

}

}

}

Iukik

This article from "AutoComplete" blog, reproduced please contact the author!

Related Article

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.