Collection Based on JAVA

Source: Internet
Author: User

Overview: 1. collection java Collection framework root interface 2. common Sub-interfaces: List, Set, and Queue. Note that map is self-contained. method: [java] package com. cxy. collection; import java. util. arrayList; import java. util. collection; import java. util. hashSet; import java. util. iterator;/*** @ author chenxiaoyang */public class CollectionTest {/*** Note: * 1. collection java Collection framework root interface * 2. common Sub-interfaces: List, Set, and Queue. Note that map is self-contained. System * 3. method: add, include, traverse, intersection, empty, size, empty, etc. */public static void main (String [] args) {Collection children = new ArrayList (); // note that the wildcard type is not added here, so many yellow line warnings // Add children. add ("James"); children. add ("red"); children. add (""); System. out. println ("============================"); // whether the System is included. out. println ("Is there any children named James in kindergarten? A: "+ children. contains (" Xiao Ming "); System. out. println (" Is there any children named Xiao Hei in kindergarten? A: "+ children. contains (""); System. out. println ("============================"); // traverse (two methods) System. out. print ("Iterator method traversal:"); Iterator it = children. iterator (); // Iterator (Iterator) Please refer to the blog article dedicated to iterator while (it. hasNext () {System. out. print (String) it. next () + ""); // because no generic type is used, you need to change it here} System. out. println (""); // keep the format, no practical use/* the above method pays too much attention to the traversal process itself, which is somewhat complicated for beginners, try foreach. * foreach is a convenient Traversal method provided by java 5 */System. o Ut. print ("foreach method traversal:"); for (Object one: children) {System. out. print (String) one + "");} System. out. println (""); // keep the format, no practical use of System. out. println ("===================="); // convert to an array Object [] array = children. toArray (); System. out. println ("array size:" + array. length); System. out. println ("============================"); // Delete the System. out. println ("before deletion:" + children); // This printing method actually uses the toString method of the Collection implementation class children. remove ("James"); System. out. println ("deleted:" + children); System. out. println ("===================="); // intersection Collection goodBoySet = new HashSet (); // a set goodBoySet. add ("James"); goodBoySet. add (""); www.2cto.com children. retainAll (goodBoySet); // The data that exists in the goodBoySet set in the children set. In short, it is the intersection. System. out. println ("intersection Result:" + children); System. out. println ("============================"); // empty, size, and empty System. out. println ("is the set empty? A: "+ children. isEmpty (); System. out. println ("set size:" + children. size (); children. clear (); System. out. println ("is the set empty after being cleared? A: "+ children. isEmpty (); System. out. println ("cleared, set size:" + children. size (); System. out. println ("============================ ");}}

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.