JAVA Generics, collection use method __java

Source: Internet
Author: User

Work content:

1. Set Learning

2. Generic Learning

Learn to share:

1. Typical generic collection list<t>,hashmap<k,v>

Common definition Generic Collections:

list<t> list = new arraylist<t> ();

"The storage type must be of type T, you can use object to store all types of objects, and you need to force transition when you take them out."

map<k,v> map = new hashmap<k,v> ();

"K is a key value, V is a value (values), you can add members by Map.put (), through string str = Map.get (key value); To get the corresponding value of the key value"

Map instance:

Map based on the use of
import Java.util.HashMap;
Import Java.util.Map;

public class Maptest {public
	static void Main (string[] args) {
		string[] names = {"Flowers", "floret", "Shout Me"};
		String[] Ifo = {"Little girl", "Puppy", "Big Class", "Young Man"};
		map<string, string> map = new hashmap<string, string> ();
		for (int i = 0; i < names.length i++) {
			map.put (names[i], ifo[i]);
		String ifostring = Map.get ("Floret");
		System.out.println (ifostring);
	}
List instance:

List base using import java.util.ArrayList;
Import Java.util.Iterator;
Import java.util.List;

Import Java.util.Vector;
	public class Listtest {static Pet cat2;
	static list<pet> pets;
		public static void Main (string[] args) {pets = new arraylist<pet> ();
		Pet CAT1 = new Pet ("Kitten", "cat");
		CAT2 = new Pet ("Big cat", "cat");
		Pet Cat3 = new Pet ("Old cat", "cat");
		Dog dog1 = new Dog ("Little Yellow", "I'm Hungry");
		Dog dog2 = new Dog ("Little white", "I'm Hungry");
		Dog dog3 = new Dog ("Golden", "I'm Hungry");
		Pets.add (CAT1);
		Pets.add (CAT2);
		Pets.add (CAT3);
		arraylist<object> list = new arraylist<object> ();
		List.add (CAT1);
		List.add (DOG1);
		List.add (DOG2);
		List.add (CAT2);
		List.add (CAT3);
		List.add (DOG3);
				for (int i = 0; i < list.size (); i++) {if (List.get (i) instanceof Pet) {pet P1 = (PET) list.get (i);
			System.out.println (P1.name + ":" + p1.type);
				else {Dog Dog = (Dog) list.get (i);
			System.out.println (Dog.name + ":" + Dog.say); } for (Pet p:pets) {if (P.name.equaLS ("big Cat")) {System.out.println (p.name + ":" + p.type);
		} search (pets);
		Iterator<pet> iterator = Pets.iterator ();
			while (Iterator.hasnext ()) {Pet pet = (PET) iterator.next ();
		System.out.println (Pet.name + "::" + pet.type);
		} public static <T> void search (T t) {vector<pet> vpets = new vector<> ();
		Vpets.addall (pets);
		if (Vpets.contains (CAT2)) {System.out.println ("presence Cat2");
	} System.out.println (Vpets); }
}


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.