Study Number: 201521123116 Java programming Eighth week of study summary

Source: Internet
Author: User

1. This week study summary 1.1 summarizes the set and generic related content in the way you like (mind map or other).

1.2 Choose: Collect code snippets that you think are useful

list<entry<String,integer>> list = new arraylist<entry<string,integer> > (Map.entryset ());

Scanner in = new Scanner (line);

2. Written assignment 1. Deletion of the specified element in list (topic 4-1) 1.1 Experiment Summary:
    • The returned array element type is object, and the caller is free to modify the array returned by the iterator.
    • Use lists to implement sorting.
    • Scanners can be used as traversal.
1.2 Your submission results (Advent number)

2. Count the number of words in the text and sort them by the number of occurrences

Scanner input=new Scanner (system.in);
Map<string,integer>map=new hashmap<string,integer> ();
String x;
while (true) {
X=input.next ();
if (X.equals ("!!!!!")) Break

if (Map.containskey (x)) Map.put (x, Map.get (x) +1);
if (Map.get (x) ==null) map.put (x, 1);
}
System.out.println (Map.size ());
List<entry<string,integer>> list =new arraylist<entry<string,integer>> (Map.entrySet ());
Collections.sort (list, new comparator<map.entry<string, integer>> () {
public int Compare (map.entry<string, integer> o1,map.entry<string, integer> O2) {
if (o1.getvalue () = O2.getvalue ()) {
Return O2.getvalue ()-O1.getvalue ();
}
else {
Return O1.getkey (). CompareTo (O2.getkey ());
}
}
});

for (int i=0;i<10;i++) {
System.out.println (List.get (i));

2.2 Experimental Summary

The TreeSet class implements the set interface and does not contain duplicate objects. Convert the Map object to a list object and get the key and value of the map with EntrySet ()

3. Inverted index (title 5-4) 3.1 Your submission result (the number of the study)

3.2 Pseudo-code (simply write out the rough steps)


int row = 1;
while (Scan.hasnextline ()) {
String s = scan.nextline ();
if (S.equals ("!!!!!")) {
Break
}
else {
Scanner scan1 = new Scanner (s);
Map2.put (row, s);
while (Scan1.hasnext ()) {
String S1 = Scan1.next ();
if (Map.get (s1) = = null) {
treeset<integer> set = new treeset<integer> ();
Set.add (row);
Map.put (S1, set);
}
else {
Map.get (S1). Add (row);
}
}
Scan1.close ();
}
row++;
}
For (map.entry<string, treeset<integer>> a:map.entryset ()) {
System.out.println (a);
}

if (!map.containskey (s)) {
System.out.println ("Found 0 results");
Continue
}
treeset<integer> Set1 = Map.get (s);
while (Scan1.hasnext ()) {
String S1 = Scan1.next ();
Set1.retainall (Map.get (S1));

3.3 Experimental Summary

The use of the map is not skilled, the problem is still in the help of students to complete.

Q4, stream, and lambda

Write a student class that has the following properties:

Private Long ID;

Private String name;

private int age;

Private Gender Gender; //enum type

Private Boolean joinsacm; //Have you participated in the ACM contest ?

Creates a collection object, such as list, with several student objects used for subsequent tests. 4.1 Using traditional methods to write a method, the Id>10,name for Zhang, age>20, gender for women, participated in the ACM Contest students screened out, put into a new collection. Called in Main, and then outputs the result.

Arraylist<student> alist = new arraylist<student> ();

for (Student student:arraylist) {

if (Student.getid () > 10L && student.getname (). Equals ("Guo")

Student.getage () >

Student.getgender (). Equals (Gender. Female)

STUDENT.ISJOINSACM ())

Alist.add (student);

}

}

return ALIST2,;

}

Code does not run out

4.2 Use the stream () in Java8, filter (), collect () to write functions with 4.1 functions, and test.

ArrayList list1= (ArrayList) List.stream (). Filter (e->

(E.getid () >10l&&e.getage () >20&&e.getgender (). Equals (Gender1) && E.ISJOINSACM () = =true). Collect (Collectors.tolist ());

List1. ForEach (E->system.out.println (e));

Results:

4.3 When building the test set, in addition to the normal student object, add some null to the collection, and then rewrite 4.2 so that it does not appear an exception.

Generic class: Generalstack (Topic 5-5)
5.1 Your Submission results (Advent number)

5.2 Code for Generalstack interface

Interface Generalstack<t> {public T push (t Item);p ublic t pop ();p ublic T Peek ();p ublic boolean empty ();p ublic int si Ze ();}

5.3 What are the benefits of generics?

No restriction of element types after generics, no more coercion of type conversions, fewer opportunities for program errors

3. Code submission record on code Cloud and PTA Experiment Summary

3.1. Code Cloud codes Submission record

Study Number: 201521123116 Java programming Eighth week of study summary

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.