Java List Collection practice (for yourself)

Source: Internet
Author: User
Import java.util.ArrayList;
Import Java.util.Iterator;

Import java.util.LinkedList;
		public class Linkedlisttest {public static void main (string[] args) {//Linkedlistmethod ();
		Arraylistmethod ();
	Arraylistmethod_2 (); * * * To the custom object, get the collection of the same element * Important is the Equals method * the contains and remove (obj) methods of the list collection are all called equals/public static VO
		ID arraylistmethod_2 () {ArrayList Al = new ArrayList ();
		Al.add (New Person1 ("Xiaoming", 22));
		Al.add (New Person1 ("Xiaoli", 10));
		Al.add (New Person1 ("Xiaoli", 10));
		Al.add (New Person1 ("Xiaoliu", 12)); for (Iterator it = Al.iterator (); It.hasnext ();)
			{Person1 P1 = (Person1) it.next ();
		System.out.println (P1.name + "..." + p1.age);
		} System.out.println ("------------");
		Al = Linkedlisttest.singleelement (AL); for (Iterator it = Al.iterator (); It.hasnext ();)
			{Person1 P1 = (Person1) it.next ();
		System.out.println (P1.name + "..." + p1.age); }/* ArrayList practice to get rid of containers with the same elements/public static void Arraylistmethod () {ARraylist al = new ArrayList ();
		Al.add ("Java01");
		Al.add ("Java02");
		Al.add ("Java01");
		Al.add ("Java02");
		Al.add ("Java01");
		Al.add ("java03");
		System.out.println (AL);
		Al = Singleelement (AL);
	System.out.println (AL);

		* * * Returns a method to return the set after the same element * * public static ArrayList singleelement (ArrayList al) {ArrayList Newal = new ArrayList (); for (Iterator it = Al.iterator (); It.hasnext ();)
			{Object o = It.next ();
			if (!newal.contains (o)) {newal.add (o);
	} return Newal;
		/* * LinkedList impersonation queue/public static void Linkedlistmethod () {Duilie dl = new Duilie ();
		Dl.add ("Java01");
		Dl.add ("Java02");
		Dl.add ("java03");
		Dl.add ("Java04");
		while (!dl.isnull ()) {System.out.println (Dl.get ());

	}}/* The emulated Queue class */class Duilie {private LinkedList list;
	Duilie () {list = new LinkedList ();
	public void Add (Object o) {list.addfirst (o);
	Public Object get () {return List.removefirst (); public Boolean isNull () {return List.isEmpty ();
	} class Person1 {String name;

	int age;
		Person1 (String name, int gae) {this.name = name;
	This.age = age; @Override public boolean equals (Object obj) {if (!
		obj instanceof Person1)) {return false;
		} Person1 p1 = (Person1) obj;
	Return Name.equals (p1.name) && age==p1.age; }
	
	
}

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.