It 18 Palm Job _java Foundation 12th Day _ Collection

Source: Internet
Author: User


1. Describe the HASHMAP internal implementation principle.

HashMap is a sub-class implementation of map, is a key-value structure, where key is stored in a non-repeating element, HashMap is actually a "chain table hash" of the data structure, that is, the combination of arrays and linked lists.

The bottom of the HashMap is an array structure, and each item of the array is a linked list. HashMap's lookup mechanism is to first use the object's hashcode to derive an address using equals to compare the various elements of the linked list in the address if the corresponding value is taken out.





2. Describe the difference between hashset and HashMap.

In addition to HashMap and Hashtable, there is a hash set hashset, the difference is that HashSet is not a key value structure,

Just store the non-repeating elements, equivalent to the simplified version of the HashMap, just contains the key in HashMap, HashSet internal is the use of HASHMAP implementation,

Just hashset inside the HashMap all the value is the same object, so HashSet is also non-thread-safe,

In addition, the HashSet implements the set interface, the set interface inherits the collection interface, the HASHMAP implements the map interface, and the map interface and the collection interface are peers.

They all have the characteristics of non-repetition, using the hash mechanism to store and query.


3. The collection of grades uses a nested implementation of map.

Class 10, 50 people per class.

Package com.it18zhang.day12;

/*

* The collection of grades uses a nested implementation of map.

* 10 classes, 50 people per class.

* map<integer,map<string,student>>

* */


Import Java.util.HashMap;

Import Java.util.Map;

Import Java.util.Map.Entry;


public class Mapdemo {


public static void Main (string[] args) {

Create a Class collection

map<integer,map<string,string>> classes = new hashmap<integer,map<string,string>> ();

Create a class

Map<string,string> names = null;

int no = 1;

Add a class to the class collection

for (int i =1; i <=; i++) {

names = new hashmap<string,string> ();

Classes.put (i, names);

Add 50 students to a class

for (int j=1; j<=50; J + +) {

Names.put (i+ ".") +j, "Tom" +no);

no++;

}

}

System.out.println ("\ n------------------------entryset-------------------------\ n");

Traversing the entryset of a class collection

For (entry<integer,map<string,string>> Entry:classes.entrySet ()) {

Class number

Integer key = Entry.getkey ();

List Collection

map<string,string> values = Entry.getvalue ();

Traversal List Collection

For (entry<string,string> Entry2:values.entrySet ()) {

String Stuno = Entry2.getkey ();

String stuname = Entry2.getvalue ();

System.out.println (key+ "\t===>\t" +stuno+ "\t-->\t" +stuname);

}

}

System.out.println ("\ n-------------------------------keySet-----------------------------\ n");

Traversing the keyset of a class collection

For (Integer ClassNo:classes.keySet ()) {

Get class Map

map<string,string> map = Classes.get (Classno);

Traverse class map using keyset

For (String StuNo:map.keySet ()) {

String name = Map.get (Stuno);

System.out.println (classno+ "\t===\t" +stuno+ "\t===\t" +name);

}

}

//

}


}

4. Programming to achieve copy of text files. The reasonable design procedure, obtains the buffer area the size high efficiency interval.

The prompt buffer setting starts at 1k and does not exceed 10M.

Package com.it18zhang.day12;


Import Java.io.FileReader;

Import Java.io.FileWriter;

Import java.io.IOException;

public class Copyfiledemo {


public static void Main (string[] args) {

Get System Attribute Row separator

String str = system.getproperty ("Line.separator");

System.out.println (str);

Create a file to be copied and a target file

String srcfile = "D:\\aa.txt";

String targfile = "D:\\bb.txt";

Define file character read and write streams

FileReader reader = null;

FileWriter writer = null;

try{

To point a stream to a file path

reader = new FileReader (srcfile);

writer = new FileWriter (targfile,false);

Create a character buffer array for bulk read write characters

char[] buf = new char[2];

int len = 0;

while (len = Reader.read (BUF))! =-1) {

Writer.write (Buf,0,len);

}

SYSTEM.OUT.PRINTLN ("------copy over--------");

}

catch (IOException e) {

E.printstacktrace ();

}

finally{

try{

if (reader! = null)

Reader.close ();

if (writer! = null)

Writer.close ();

}

catch (IOException E1) {

E1.printstacktrace ();

}

}

}


}


This article from "Rookie Achievement Data Road" blog, reproduced please contact the author!

It 18 Palm Job _java Foundation 12th Day _ Collection

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.