1. Summary of this week's study
2. Written work
Deletion of the specified element in list (topic 4-1)
1.1 Lab summary & nbsp , &NB Sp , &NB Sp , &NB Sp , &NB Sp , &NB Sp : in the experiment, the scanner is more than just the system .In (keyboard input and the like), can also be an assigned variable, such as: Scanner in=new Scanner (nextline);
Count the number of words in the text and sort by the number of occurrences (topic 5-3)
2.1 Pseudo-code (simply write out the rough steps) For:
traverse each word of text to establish a map map; if (str recurs) key value +1 else key =1
2.2 Lab summary & nbsp , &NB Sp , &NB Sp , &NB Sp , &NB Sp , &NB Sp A: TreeMap. It uses a custom sort of collections.sort, where collections if you want to sort the map, you can convert the map to a list,List<Map.Entry<String, Integer>> list = new ArrayList<Map.Entry<String, Integer>>(tmap.entrySet());
To sort.
Inverted index (topic 5-4)
3.1 Your Submission results (Advent number)
3.2 Pseudo-code (simply write out the rough steps)
3.3 Experimental Summary
Stream and Lambda
Write a student class that has the following properties:
private Long id;private String name;private int age;private Gender gender;//枚举类型private boolean joinsACM; //是否参加过ACM比赛
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. For:
The results are as follows:
4.2 Use the stream () in Java8, filter (), collect () to write functions with 4.1 functions, and test. For:
-
At first not very will use, on the Internet query the next get a similar function code is:
List<String> result1 = lines.stream() // convert list to stream
.filter(line -> !"mkyong".equals(line)) // filter the line which equals to "mkyong"
.collect(Collectors.toList()); // collect the output and convert streams to a list
Based on the comment, you know what the code means, and then follow this rewrite and test the results as shown in 4.1, with the following code:
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. For:
-
Since we know that listlist can be stored in null, when we add null, how do not let null affect the result, in fact, in the judgment of the conditions there to add the !=null
line, the specific code is as follows.
⑤ generic class: Generalstack (Topic 5-5)
Generic class: Generalstack (Topic 5-5)
5.1 Your Submission results (Advent number)
5.2 Code for Generalstack interface
5.3 What are the benefits of generics?
Generic methods
The base reference file is Genericmain and is modified on this file.
6.1 Write Method Max, which returns the maximum value of all elements in the list. The elements in the list must implement the comparable interface. The Max method is written so String max = max(strList)
that it can run successfully, where strlist is the List<String>
type. can also make Integer maxInt = max(intList);
the operation successful, where Intlist is the List<Integer>
type.
For:
private static <T> T max(List<T> list) { Collections.sort(list,new Comparator<T>() { @Override public int compare(T o1, T o2) { return o1.toString().compareTo(o2.toString()); } } ); return list.get(list.size()-1); }
public static <T extends Object & Comparable<? super T>> T max(Collection<? extends T> coll) { return Collections.max(coll); }
Operation Result:
- In the beginning, simple generics are used, because only string and integer types convert elements directly into the string class and then compare them, but if the other type is running, an error occurs.
- It is convenient to use the method of collections to find Max.
3. Code submission record on code Cloud and PTA Experiment Summary
3.1. Code Cloud codes Submission record
3.2. PTA Experiment