Java Collection Exercises

Source: Internet
Author: User

Exercise 1,

Each student has a corresponding place of attribution, student student, address string type

Student attributes: Name and age

Note: The name and age are the same for the same student, ensuring the uniqueness of the students

Import java.util.*; Class Mapdemo {public static void main (string[] args) {hashmap<student,string> hm=new hashmap<student,str
		Ing> ();//create HashMap container to store information hm.put (new Student ("Zjd", 12), "Shandong");//Add Information Hm.put (new Student ("Zxm", 12), "Shandong");
		Hm.put (New Student ("Zjn", 12), "Shandong");
		Hm.put (New Student ("DXM", 12), "Shandong");
		Hm.put (New Student ("Wyj", 12), "Shandong");
		Hm.put (New Student ("Zxz", 12), "Shandong");
		Hm.put (New Student ("Zjj", 12), "Shandong");
		Hm.put (New Student ("Zjd", 12), "Shandong");
		Hm.put (New Student ("Zjn", 12), "Shandong");
		Hm.put (New Student ("DXM", 12), "Shandong");
		Hm.put (New Student ("Wyj", 12), "Shandong"); Set<map.entry<student,string>> S=hm.entryset ()///use EntrySet to implement HASHMAP conversion to set set Iterator<map.entry
		<Student,String>> It=s.iterator ();
			while (It.hasnext ()) {map.entry<student,string> me=it.next ();
			Student Stu=me.getkey ();
			String Address=me.getvalue ();
	      SOP (Stu.getname () + ":" +stu.getage () + ":" +address+ "EntrySet"); } set<student> S1=hm.keyset ()//Use Keyset to implement HASHMAP conversion to set set iterator<student> it1=s1.iterator ();
			while (It1.hasnext ()) {Student stu1=it1.next ();
		SOP (Stu1.getname () + ":" +stu1.getage () + ":" +hm.get (STU1) + "keyset");
	The public static void sop (Object obj) {System.out.println (obj);
	}/* Create student Class Student name: Name Age: Aging/class student{private String name;
	private int age;
		Student (String Name,int Age) {//Create constructor implementation initialization This.name=name;
	This.age=age;
		public void Setstudent (String name,int Age) {//Create Set Student information method This.name=name;
	This.age=age;
	Public String GetName () {//Get student name return this.name;
	public int Getage () {//Get student age return this.age;	
	public int hashcode () {//overwrite hashcode Let it be judged according to the information in Student return This.name.hashCode () +this.age*37; public boolean equals (Object obj) {///overwrite equals to determine whether the name and age are the same if (!
		obj instanceof Student)//To determine whether the incoming is a Student type return false;
		Student s= (Student) obj;
	Return This.name.equals (s.name) &&this.age==s.age; }
}

Exercise 2,

Gets the number of occurrences of the letter of a string:

Import java.util.*;
Class TestDemo1 
{public
	static void Main (string[] args) 
	{
		String str= " Sdfgesdgsdxjahskjsaxkskfdsfcbxbbfhj ";
		Char[] Ch=str.tochararray ()//Converts a string to a character array
		treemap<character,integer> m=new Treemap<character,integer >//This place chooses TreeMap, because he can automatically sort for
		(char c:ch)
		{
			Integer i=m.get (c);//See if
			(i==null) {
				M.put (c,1);
			} else{
				m.put (c,i+1);
			}
		SYSTEM.OUT.PRINTLN (m)//Direct Output
	}
}

Collection Extension Knowledge:

The collections class provides a variety of methods for manipulating collections such as

Collections.max ()//returns the maximum value in the collection collection

Collections.sort ()//To sort the list collection

Collections.binarysearch ()//Returns a value relative to the list collection

Collections.fill ()//Replaces all elements in the list collection with the specified element

Collections.shuffle ()//To reorder the data in the list collection

Import java.util.*;
Class Collectionstest 
{public
	static void Main (string[] args) 
	{
		arraylist<string> al=new Arraylist<string> ();//Create a ArrayList set
		al.add ("ASDASD");
		Al.add ("Sdxa");
		Al.add ("Zjdassa");
		Al.add ("ASDASD");
		Al.add ("Wdad");
		Al.add ("RDFGX");
		For (iterator It=al.iterator (); It.hasnext ();) {//Traversal result
			System.out.println (It.next ());
		}

		System.out.println ("after sort:");
		Collections.sort (AL);
		For (iterator It=al.iterator (); It.hasnext ();) {
			System.out.println (It.next ());
		}
		SOP ("Maximum output value:");
		SOP (Collections.max (AL));

		SOP ("scrambling order");
		Collections.shuffle (AL);
		For (iterator It=al.iterator (); It.hasnext ();) {
			System.out.println (It.next ());
		}
	public static void Sop (Object obj) {
		System.out.println (obj);
	}
}

An enhanced for loop is also called an advanced for loop

Format: for (array type variable variable name: Traversed collection (Collection) or array)

JDK1.5 new Features

1, variable parameters: Use must pay attention to the variable parameters must be placed in the last side of the parameter list

2, Static import: When the class name is the same, you need to specify the specific package names, when the duplicate method, you need to specify an object or class with the attached

Format: import static Package class. *;

Static import can only import a class and cannot import a method;




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.