Contents

61B-11: Libraries, Abstract Classes, Packages

Java Libraries

Collections

  • Collections is a package in Java that provides various utility classes for working with collections. /61b-11/image.png /61b-11/image-1.png

Tasks引入

3 tasks, given the text of a book:

  • Create a list of all words in the book.
  • Count the number of unique words.
  • Keep track of the number of times that specific words are mentioned.

#1 way set

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
public static int countUniqueWords(List<String> words) {
	Set<String> ss = new HashSet<>();
	for (String s : words) {
   	    ss.add(s);    	
	}
	return ss.size();
}
public static int countUniqueWords(List<String> words) {
	Set<String> ss = new HashSet<>();
	ss.addAll(words);
	return ss;
}

#2 way map

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
public static Map<String, Integer> 
        collectWordCount(List<String> words, List<String> targets) {
	Map<String, Integer> wordCounts = new HashMap<>();
	for (String s : targets) {
   	    wordCounts.put(s, 0);
	}
	for (String s : words) {
   	    if (wordCounts.containsKey(s)) {
          	int oldCount = wordCounts.get(s);
         	wordCounts.put(s, oldCount + 1);
   	    }      	
	}
	return wordCounts;
}

Python里面则是dict实现,咋一看似乎更好?但是? /61b-11/image-2.png Java9新特点: /61b-11/image-3.png

Interfaces and Abstract Classes

More interface details:

  • Can provide variables, but they are public static final.
  • final means the value can never change.
  • A class can implement multiple interfaces.

interface summary

/61b-11/image-4.png

abstract class intro

/61b-11/image-5.png

两者对比

/61b-11/image-6.png

Packages

A package is a namespace that organizes classes and interfaces. /61b-11/image-7.png