Three common types of collections in Java

Source: Internet
Author: User

A set of sets. Its main realization class has HashSet, TreeSet. A reference to the object is stored, and duplicate objects are not allowed.  

Code:

  public class Settest {

public static void Main (string[] args) {
Set set=new HashSet ();
Add data
Set.add ("abc");
Set.add ("CBA");
Set.add ("abc");//Intentional repetition
Set.add (123);
Set.add (TRUE);

System.out.println ("Number of collection elements:" +set.size ());
Iterate through every element in a collection
Iterator It=set.iterator ();
while (It.hasnext ()) {
System.out.println (It.next ());
}
}
}

Judged by the Equals () method in Java. The Equals () method must be overloaded if there is a special requirement.

1.HashSet (), calls the object's Hashcode () method, obtains the hash code, and then computes the position of the holding object in the collection. The hash code is compared with the Equals () method to determine whether to repeat. Therefore, overloading the Equals () method also overloads the Hashcode () method.

2.TreeSet (), inherits the Shortedset interface and is able to sort the objects in the collection. The default sort is a natural sort, but this method only sorts objects that implement the comparable interface, which is implemented in Java for numeric and character objects such as Integer, Byte, Double, Character, String, and so on.
If there is a special sort, you must overload the CompareTo () method under the interface or construct the collection through the implementation class of the comparator interface.  

Second, the list collection, its main implementation class has LinkedList, ArrayList, the former realizes the chain table structure, the latter can represent the size variable array. The list is characterized by the ability to save objects in a linear manner and to allow duplicate objects to be stored. The list is able to sort by using the static method of the Collections class. Sort (List list) is sorted naturally; Sort (List listm,comparator codddmparator) customized sort.

Code:

  public class Listtest {

ArrayList
static void Testone () {
List list=new ArrayList ();
Add data
List.add ("abc");
List.add ("CBA");
List.add (123);
List.add (0, "fist");
View Collection Length
System.out.println ("Store" +list.size () + "elements");
List.remove (0);//Delete First element
View whether the collection contains CBA
if (List.contains ("CBA")) {
System.out.println ("contains elements CBA");
}

Take out the second element in the collection
System.out.println ("The second element is:" +list.get (1));

Take out all the elements in the collection
for (int i = 0; i < list.size (); i++) {
System.out.println (List.get (i));
}
}

LinkedList
static void Testtwo () {
LinkedList list=new LinkedList ();
adding elements
List.add ("AAAA");
List.add (123123);
List.addfirst ("1111111");
List.addlast ("2222222");
List.add ("33333333");

System.out.println ("Number of elements:" +list.size ());
Take out a third element
System.out.println ("The third element is:" +list.get (2));

First element
System.out.println ("first element:" +list.getfirst ());
System.out.println ("last element:" +list.getlast ());

Delete First element
List.removefirst ();

for (Object object:list) {
System.out.println (object);
}
}


public static void Main (string[] args) {
Testone ();
Testtwo ();
}
}

Third, the map collection, its main realization class has HashMap, TreeMap. Map does not have a uniqueness requirement for a value, and is unique to a health requirement, if the existing health is added, the original value object will be overwritten. The HashMap class accesses the key object according to the hash algorithm, and you can overload the Equals (), Hashcode () methods to compare the keys, but the two must be identical. TreeMap, which can be ordered naturally, can also be constructed TreeMap by passing comparator's implementation class.

Code:

public class Maptest {

public static void Main (string[] args) {
Instantiate a Collection Object
Map map=new HashMap ();
Add data
Map.put ("P01", "Zhangsan");
Map.put ("P02", "Lucy");
Map.put ("Psex", "male");
Map.put ("page", "39");
Map.put ("page", "//key"), repetition will be overwritten by the following

Determine if there is a key for Psex
if (Map.containskey ("Psex")) {
SYSTEM.OUT.PRINTLN ("presence");
}
SYSTEM.OUT.PRINTLN ("Set Size:" +map.size ());
SYSTEM.OUT.PRINTLN ("Output key is the value of page:" +map.get ("page"));

Iterate through all the data in the Map collection
Iterator It=map.keyset (). Iterator ();
while (It.hasnext ()) {
String Key=it.next (). toString ();
System.out.println ("key=" +key+ ", value=" +map.get (key));
}
/*
Set Set=map.keyset ();//Remove all keys from the map and encapsulate them in the set set
Iterator It=set.iterator ();
while (It.hasnext ()) {
String Key=it.next (). toString ();
System.out.println ("key=" +key+ ", value=" +map.get (key));
}
*/
}
}

  

Three common types of collections in Java

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.