用户工具

站点工具


zh:courses:java2025:ch06

这是本文档旧的修订版!


第六章:集合类

课件

下载:集合类

List的实现类:ArrayList类

ArrayList类:实例

ArrayListTester.java
package cn.edu.bjut.chapter6;
 
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
 
public class ArrayListTester {
	public static void main(String[] args) {
		List<Integer> strList = new ArrayList<Integer>();
 
		strList.add("123");
		strList.add("456");
		strList.add("234");
		strList.add(2, "121");
		System.out.println(strList);
 
		strList.remove(2);
		System.out.println(strList);
 
		for (int i = 0; i < strList.size(); i++) {
			System.out.print(strList.get(i) + "\t");
		}
		System.out.println();
 
		for (Iterator<String> it = list1.iterator(); it.hasNext();) {
			System.out.print(it.next() + "\t");
		}
		System.out.println();
	}
}

ArrayList类:构造函数

ArrayListConstructorTester.java
package cn.edu.bjut.chapter6;
 
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
 
public class ArrayListConstructorTester {
	public static String getRandomString(int length) { 
		String base = "abcdefghijklmnopqrstuvwxyz0123456789";
		Random random = new Random();
		StringBuffer sb = new StringBuffer(length);
		for (int i = 0; i < length; i++) {
			int number = random.nextInt(base.length());
			sb.append(base.charAt(number));
		}
		return sb.toString();
	}
 
	public static void main(String[] args) {
		int n = 1000000;
 
		long startTime1 = System.currentTimeMillis();
		List<String> list1 = new ArrayList<String>();
		for (int i = 0; i < n; i++) {
			list1.add(getRandomString(5));
		}
		long endTime1 = System.currentTimeMillis();
 
		long startTime2 = System.currentTimeMillis();
		List<String> list2 = new ArrayList<String>(n);
		for (int i = 0; i < n; i++) {
			list2.add(getRandomString(5));
		}
		long endTime2 = System.currentTimeMillis();
 
		System.out.println((endTime1 - startTime1) + "ms\t" 
				+ (endTime2 - startTime2) + "ms");
	}
}

List排序

Person类

Person.java
package cn.edu.bjut.chapter6;
 
public class Person implements Comparable<Person> {
	private String name;
	private char gender;
 
	public Person(String name, char gender) {
		this.name = name;
		this.gender = gender;
	}
 
	public String getName() {
		return name;
	}
 
	public void setName(String name) {
		this.name = name;
	}
 
	public char getGender() {
		return gender;
	}
 
	public void setGender(char gender) {
		this.gender = gender;
	}
 
	@Override
	public String toString() {
		return (name + "\t" + gender);
	}
 
	@Override
	public int compareTo(Person o) {
		if (gender < o.gender) {
			return -1;
		} else if (gender > o.gender) {
			return 1;
		} else {
			return name.compareTo(o.name);
		}
	}
}

测试类

ListSortTester.java
package cn.edu.bjut.chapter6;
 
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
 
public class ListSortTester {
	public static void main(String[] args) {
		String[] strArray = new String[] { "dtk", "abc", "ekd", "def" };
		List<String> strList = Arrays.asList(strArray);
		System.out.println(strList);
		Collections.sort(strList);
		System.out.println(strList);
		Collections.reverse(strList);
		System.out.println(strList);
 
		List<Person> stuList = new ArrayList<Person>();
		stuList.add(new Person("李文慧", 'F'));
		stuList.add(new Person("白富美", 'F'));
		stuList.add(new Person("谢祖隆恩", 'M'));
		stuList.add(new Person("梁化祥", 'M'));
		System.out.println(stuList);
		Collections.sort(stuList);
		System.out.println(stuList);
 
		Collections.sort(stuList);
		System.out.println(stuList);
		int idx = Collections.binarySearch(stuList, new Person("白晨", 'F'));
		if (idx >= 0) {
			System.out.println("Found");
		} else {
			System.out.println("Not Found");
		}
	}
}

人员分组

Grouper类

Grouper.java
package cn.edu.bjut.chapter6;
 
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
 
public class Grouper<T> {
	private List<T> data;
	private int nfold; 
	private int[] starts = null;
 
	public Grouper(List<T> data, int nfold, long seed) {
		this.data = data; 
		this.nfold = nfold; 
 
		Collections.shuffle(data, new Random(seed));
 
		this.starts = new int[nfold + 1]; 
		for (int v = 0; v <= nfold; v++) {
			starts[v] = Math.round(data.size() * (v / (float) nfold)); 
		}
	}
 
	public List<T> getGroup(final int split) {
		if (split >= nfold || split < 0) {
			return null; 
		} 
 
		List<T> group = new ArrayList<T>(); 
		for (int m = starts[split]; m < starts[split + 1]; m++) {
			group.add(data.get(m)); 
		}
 
		return group; 
	}
}

GrouperTester类

GrouperTester.java
package cn.edu.bjut.chapter6;
 
import java.util.ArrayList;
import java.util.List;
 
public class GrouperTester {
	public static void main(String[] args) {
		List<Person> students = new ArrayList<Person>();
		students.add(new Person("李文慧", 'F'));
		students.add(new Person("白晨", 'F'));
		students.add(new Person("谢天昊", 'M'));
		students.add(new Person("梁化祥", 'M'));
		students.add(new Person("高明", 'M'));
		students.add(new Person("孔雀", 'F'));
 
		Grouper<Person> grouper = new Grouper<Person>(students, 2, 1); 
		System.out.println("The first group:");
		System.out.println(grouper.getGroup(0));
		System.out.println("The second group:"); 
		System.out.println(grouper.getGroup(1));
	}
}

Stack类实现

MyStack类

MyStack.java
package cn.edu.bjut.chapter6;
 
import java.util.LinkedList;
 
public class MyStack<E> {
	private LinkedList<E> list;
 
	public MyStack() {
		list = new LinkedList<E>();
	}
 
	public void push(E e) {
		list.push(e);
	}
 
	public E pop() {
		return list.pop();
	}
 
	public int size() {
		return list.size();
	}
 
	public boolean isEmpty() {
		return list.isEmpty();
	}
}

Stack测试类

StackTester.java
package cn.edu.bjut.chapter6;
 
public class StackTester {
	public static boolean isMatch(String str) {
		char[] charArray = str.toCharArray();
		MyStack<Character> stack = new MyStack<Character>();
		for (char c : charArray) {
			if (c == '(' || c == '[' || c == '{') {
				stack.push(c);
			} else if (c == ')' || c == ']' || c == '}') {
				if (stack.isEmpty()) {
					return false;
				}
 
				Character cc = stack.pop();
 
				switch (c) {
				case ')':
					if (cc != '(') {
						return false;
					}
					break;
				case ']':
					if (cc != '[') {
						return false;
					}
					break;
				case '}':
					if (cc != '{') {
						return false;
					}
					break;
				}
			}
		}
 
		return stack.isEmpty() ? true : false;
	}
 
	public static void main(String[] args) {
		System.out.println(isMatch("ab(dd[123]}"));
 
		System.out.println(isMatch("ab(dd[123])"));
	}
 
}

HashSet类:实例

HashSetTester.java
package cn.edu.bjut.chapter6;
 
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
 
public class HashSetTester {
	public static void main(String[] args) {
		Set<String> set = new HashSet<String>();
		set.add("Alice");
		set.add("Albert");
		set.add("Mike");
		set.add("Tim");
 
		System.out.println(set);
 
		set.remove("Mike");
		System.out.println(set);
 
		for (String name : set) {
			System.out.print(name + "\t");
		}
		System.out.println();
 
		for (Iterator<String> it = set.iterator(); it.hasNext();) {
			System.out.print(it.next() + "\t");
		}
		System.out.println();
	}
}

HashSet过滤重复元素

Name类

Name.java
package cn.edu.bjut.chapter6;
 
public class Name {
	private String firstName, lastName;
 
	public Name(String firstName, String lastName) {
		this.firstName = firstName;
		this.lastName = lastName;
	}
 
	public String getFirstName() {
		return firstName;
	}
 
	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}
 
	public String getLastName() {
		return lastName;
	}
 
	public void setLastName(String lastName) {
		this.lastName = lastName;
	}
 
	@Override
	public String toString() {
		return "[" + lastName + ", " + firstName + "]";
	}
 
	@Override
	public boolean equals(Object obj) {
		if ((obj == null) || !(obj instanceof Name)) {
			return false;
		}
 
		if (this == obj) {
			return true;
		}
 
		Name other = (Name) obj;
 
		return (firstName.equalsIgnoreCase(other.firstName) 
				&& lastName.equalsIgnoreCase(other.lastName));
	}
 
	@Override
	public int hashCode() {
		return (firstName.toLowerCase().hashCode() * 10 
				+ lastName.toLowerCase().hashCode());
	}
}

Name测试类

NameTester.java
package cn.edu.bjut.chapter6;
 
import java.util.HashSet;
import java.util.Set;
 
public class NameTester {
	public static void main(String[] args) {
		Set<Name> names = new HashSet<Name>();
		names.add(new Name("Albert", "Einstein"));
		names.add(new Name("albert", "einstein"));
		names.add(new Name("Issac", "Netwon"));
		names.add(new Name("issac", "Netwon"));
 
		System.out.println(names); // names中只有两个元素
	}
}

SortedSet的实现类:TreeSet

Name2类

Name2.java
package cn.edu.bjut.chapter6;
 
public class Name2 implements Comparable<Name2> {
	private String firstName, lastName;
 
	public Name2(String firstName, String lastName) {
		this.firstName = firstName;
		this.lastName = lastName;
	}
 
	public String getFirstName() {
		return firstName;
	}
 
	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}
 
	public String getLastName() {
		return lastName;
	}
 
	public void setLastName(String lastName) {
		this.lastName = lastName;
	}
 
	@Override
	public String toString() {
		return "[" + lastName + ", " + firstName + "]";
	}
 
	@Override
	public boolean equals(Object obj) {
		if ((obj == null) || !(obj instanceof Name2)) { return false; }
 
		if (this == obj) { return true; }
 
		Name2 other = (Name2) obj;
 
		return (firstName.equalsIgnoreCase(other.firstName) 
				&& lastName.equalsIgnoreCase(other.lastName));
	}
 
	@Override
	public int hashCode() {
		return (firstName.toLowerCase().hashCode() * 10 
				+ lastName.toLowerCase().hashCode());
	}
 
	@Override
	public int compareTo(Name2 name) {
		if (!lastName.equalsIgnoreCase(name.lastName)) {
			return lastName.compareTo(name.lastName); 
		} else {
			return firstName.compareTo(name.firstName); 
		}
	}
}

Name2测试类

Name2Tester.java
package cn.edu.bjut.chapter6;
 
import java.util.Comparator;
import java.util.Set;
import java.util.TreeSet;
 
public class Name2Tester {
	public static void main(String[] args) {
		Set<Name2> nameSet1 = new TreeSet<Name2>(); // 自然排序
		nameSet1.add(new Name2("Albert", "Einstein"));
		nameSet1.add(new Name2("Issac", "Netwon"));
		nameSet1.add(new Name2("Qiang", "Yang")); 
		nameSet1.add(new Name2("Prem", "Gopalan")); 
		nameSet1.add(new Name2("Jaewon", "Yang")); 
		System.out.println(nameSet1);
 
		Set<Name2> nameSet2 = new TreeSet<Name2>(new Comparator<Name2>() {
			@Override
			public int compare(Name2 arg0, Name2 arg1) {
				return arg0.getFirstName().compareToIgnoreCase(arg1.getFirstName());
			}
 
		}); // 自定义排序
 
		for (Name2 name: nameSet1) {
			nameSet2.add(name); 
		}
		System.out.println(nameSet2);
	}
}

匿名类

Rectangle.java
package cn.edu.bjut.chapter6;
 
public class Rectangle {
	private int length, width; 
 
	public Rectangle(int length, int width) {
		this.length = length; 
		this.width = width; 
	}
 
	public int getLength() {
		return length;
	}
 
	public void setLength(int length) {
		this.length = length;
	}
 
	public int getWidth() {
		return width;
	}
 
	public void setWidth(int width) {
		this.width = width;
	}
 
	public int perimeter() {
		System.out.println("长方形周长计算:");
		return 2 * (length + width); 
	}
}
AnonymousClassTester.java
package cn.edu.bjut.chapter6;
 
public class AnonymousClassTester {
	public void test() {
		// 创建的匿名类继承了Rectangle类
		Rectangle rect = new Rectangle(8, 8) {
			public int perimeter() {
				System.out.println("正方形周长计算:");
				return this.getLength() << 4; 
			}
		};
		System.out.println(rect.perimeter());
	}
 
	public static void main(String[] args) {
		AnonymousClassTester tester = new AnonymousClassTester();
		tester.test();
	}
}
Shape.java
package cn.edu.bjut.chapter6;
 
public interface Shape {
	void display(); 
}
AnonymousClassDemo.java
package cn.edu.bjut.chapter6;
 
public class AnonymousClassDemo {
	public void test() {
		Shape circle = new Shape() {
			@Override
			public void display() {
				System.out.println("This is a circle."); 
			}
		};
		circle.display();
	}
 
	public static void main(String[] args) {
		AnonymousClassDemo demo = new AnonymousClassDemo(); 
		demo.test();
	}
}

Map的实现类:实例

MapTester类

MapTester.java
package cn.edu.bjut.chapter6;
 
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
 
public class MapTester {
	public static void main(String[] args) {
		Map<String, Name> id2Names = new HashMap<String, Name>();
		id2Names.put("213456", new Name("Albert", "Einstein"));
		id2Names.put("123456", new Name("Albert", "Einstein"));
		id2Names.put("321456", new Name("Qiang", "Yang"));
		id2Names.put("456321", new Name("Qiang", "Yang"));
 
		System.out.println(id2Names);
		System.out.println(id2Names.keySet());
		System.out.println(id2Names.values());
 
		Map<String, Name> id2Names2 = new LinkedHashMap<String, Name>();
		id2Names2.put("213456", new Name("Albert", "Einstein"));
		id2Names2.put("123456", new Name("Albert", "Einstein"));
		id2Names2.put("321456", new Name("Qiang", "Yang"));
		id2Names2.put("456321", new Name("Qiang", "Yang"));
 
		System.out.println(id2Names2);
		System.out.println(id2Names2.keySet());
		System.out.println(id2Names2.values());
 
		for (Map.Entry<String, Name> entry : id2Names.entrySet()) {
			System.out.println(entry.getKey() + ":" + entry.getValue());
		}
	}
}

单词词频统计

WordCounter类

WordCounter.java
package cn.edu.bjut.chapter6;
 
import java.util.Map;
import java.util.TreeMap;
 
public class WordCounter {
	private String[] tokens;
	private Map<String, Integer> counter;
 
	public WordCounter(String[] tokens) {
		this.tokens = tokens;
		this.counter = new TreeMap<String, Integer>();
	}
 
	public void count() {
		for (String token : tokens) {
			if (counter.containsKey(token)) {
				int count = counter.get(token);
				counter.put(token, count + 1);
			} else {
				counter.put(token, 1);
			}
		}
	}
 
	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();
		for (Map.Entry<String, Integer> entry : counter.entrySet()) {
			sb.append(entry.getKey() + ": " + entry.getValue() + "\n");
		}
 
		return sb.toString();
	}
}

WordCounter测试类

WordCounterTester.java
package cn.edu.bjut.chapter6;
 
public class WordCounterTester {
	public static void main(String[] args) {
		String text = "1648年 , 牛顿 被 送去 读书 。 少年 时 的 牛顿 并 不是 " 
				+ "神童 , 他 成绩 一般 , 但 他 喜欢 读书 , 喜欢 看 "
				+ "一些 介绍 各种 简单 机械 模型 制作 方法 的 读物 ,  "
				+ "并 从中 受到 启发 , 自己 动手 制作 些 奇奇怪怪 的 " 
				+ "小  玩意 , 如 风车 、 木钟 、 折叠式 提灯 等等 。";
		String[] words = text.toLowerCase().split("\\s+");
 
		WordCounter wc = new WordCounter(words);
		wc.count();
		System.out.println(wc);
	}
}

WordCounter测试类2

WordCounterTester2.java
package cn.edu.bjut.chapter6;
 
import java.util.HashMap;
import java.util.Map;
 
public class WordCounterTester2 {
	public static void main(String[] args) {
		Map<String, WordCounter> map = new HashMap<String, WordCounter>();
 
		String[] ids = { "000089219800222", "000088691000018", "000166992000025" };
		String[] titles = { "Liposomal doxorubicin in the palliative treatment of head and neck cancer",
				"The long-term complications of chemotherapy in childhood genitourinary tumors",
				"Adenovirus-mediated tumor suppressor p53 gene therapy for anaplastic thyroid carcinoma in vitro and in vivo" };
 
		for (int i = 0; i < ids.length; i++) {
			WordCounter counter = new WordCounter(titles[i].toLowerCase().split("\\s+"));
			counter.count();
			map.put(ids[i], counter);
		}
 
		for (Map.Entry<String, WordCounter> entry : map.entrySet()) {
			System.out.println(entry.getKey());
			System.out.println(entry.getValue());
		}
	}
}

上机作业

  • 建立一个Corpus类,用于存储专利文献集合(每篇专利文献的信息见第五章最后一道上机作业),建立查询和删除方法,可根据专利申请号、申请日期、IPC分类号和CPC分类号查询和删除相应专利文献。

【参考答案一】

Classification.java
package cn.edu.bjut.chapter6;
 
public class Classification {
	protected char section;
	protected int mainClass;
	protected char subclass;
	protected int mainGroup;
	protected int subgroup;
 
	public final static char SEPARATOR = '/';
 
	public Classification(char section, int mainClass, char subclass, int mainGroup, int subgroup) {
		this.section = section;
		this.mainClass = mainClass;
		this.subclass = subclass;
		this.mainGroup = mainGroup;
		this.subgroup = subgroup;
	}
 
	// example: G09F 17/30
	public Classification(String symbol) {
		this.section = symbol.charAt(0);
		this.mainClass = Integer.parseInt(symbol.substring(1, 3));
		this.subclass = symbol.charAt(3);
 
		int pos = symbol.indexOf(SEPARATOR);
		this.mainGroup = Integer.parseInt(symbol.substring(4, pos).trim());
		this.subgroup = Integer.parseInt(symbol.substring(pos + 1));
	}
 
	public char getSection() {
		return section;
	}
 
	public void setSection(char section) {
		this.section = section;
	}
 
	public int getMainClass() {
		return mainClass;
	}
 
	public void setMainClass(int mainClass) {
		this.mainClass = mainClass;
	}
 
	public char getSubclass() {
		return subclass;
	}
 
	public void setSubclass(char subclass) {
		this.subclass = subclass;
	}
 
	public int getMainGroup() {
		return mainGroup;
	}
 
	public void setMainGroup(int mainGroup) {
		this.mainGroup = mainGroup;
	}
 
	public int getSubgroup() {
		return subgroup;
	}
 
	public void setSubgroup(int subgroup) {
		this.subgroup = subgroup;
	}
 
        @Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
 
		if (obj == null || getClass() != obj.getClass()) {
			return false;
		}
 
		Classification other = (Classification) obj;
		if (mainClass != other.mainClass || mainGroup != other.mainGroup || 
				section != other.section || subclass != other.subclass || subgroup != other.subgroup) {
			return false;
		}
 
		return true;
	}
 
	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();
 
		sb.append(section);
 
		sb.append(String.format("%2s", mainClass).replace(' ', '0'));
 
		sb.append(subclass);
 
		sb.append(String.format("%4s", mainGroup));
 
		sb.append(SEPARATOR);
 
		sb.append(subgroup);
 
		return sb.toString();
	}
}
Ipc.java
package cn.edu.bjut.chapter6;
 
public class Ipc extends Classification {
 
	public Ipc(char section, int mainClass, char subclass, int mainGroup, int subgroup) {
		super(section, mainClass, subclass, mainGroup, subgroup); 
	}
 
	public Ipc(String symbol) {
		super(symbol); 
	}
 
	public static void main(String[] args) {
		char section = 'A'; 
		int mainClass = 4; 
		char subclass = 'B'; 
		int mainGroup = 55; 
		int subgroup = 56; 
 
		Classification ipc = new Ipc(section, mainClass, subclass, mainGroup, subgroup); 
		System.out.println(ipc); 
 
		String symbol = "G09F 17/30"; 
		System.out.println(new Ipc(symbol)); 
	}
}
Cpc.java
package cn.edu.bjut.chapter6;
 
public class Cpc extends Classification {
	private char position; 
	private char value; 
 
	public Cpc(char section, int mainClass, char subclass, int mainGroup, int subgroup, char position, char value) {
		super(section, mainClass, subclass, mainGroup, subgroup); 
 
		this.position = position; 
		this.value = value; 
	}
 
	public Cpc(String symbol, char position, char value) {
		super(symbol); 
 
		this.position = position; 
		this.value = value; 
	}
 
	public char getPosition() {
		return position;
	}
 
	public void setPosition(char position) {
		this.position = position;
	}
 
	public char getValue() {
		return value;
	}
 
	public void setValue(char value) {
		this.value = value;
	}
 
        @Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
 
		if (!super.equals(obj)) {
			return false;
		}
 
		if (getClass() != obj.getClass()) {
			return false;
		}
 
		Cpc other = (Cpc) obj;
		if (position != other.position || value != other.value) {
			return false;
		}
 
		return true;
	}
}
Date.java
package cn.edu.bjut.chapter6;
 
public class Date {
	private int year, month, day;
 
	public Date(int year, int month, int day) {
		this.year = year;
		this.month = month;
		this.day = day;
	}
 
	// format: YYYY-MM-DD
	public Date(String str) {
		String[] parts = str.trim().split("-"); 
		this.year = Integer.parseInt(parts[0]); 
		this.month = Integer.parseInt(parts[1]); 
		this.day = Integer.parseInt(parts[2]); 
	}
 
	public int getYear() {
		return year;
	}
 
	public void setYear(int year) {
		this.year = year;
	}
 
	public int getMonth() {
		return month;
	}
 
	public void setMonth(int month) {
		this.month = month;
	}
 
	public int getDay() {
		return day;
	}
 
	public void setDay(int day) {
		this.day = day;
	}
 
        @Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
 
		if (obj == null || getClass() != obj.getClass()) {
			return false;
		}
 
		Date other = (Date) obj;
		if (day != other.day || month != other.month || year != other.year) {
			return false;
		}
 
		return true;
	}
 
	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();
 
		sb.append(year);
		if (month < 10) {
			sb.append("0" + month);
		} else {
			sb.append(month);
		}
		if (day < 10) {
			sb.append("0" + day);
		} else {
			sb.append(day);
		}
 
		return sb.toString();
	}
}
Patent.java
package cn.edu.bjut.chapter6;
 
public class Patent {
	private String applicationNo; 
	private Date applicationDate; 
	private Ipc[] ipcs; 
	private Cpc[] cpcs; 
 
	private final static String SEPERATOR = ","; 
 
	public Patent(String applicationNo, Date applicationDate, Ipc[] ipcs, Cpc[] cpcs) {
		this.applicationNo = applicationNo; 
		this.applicationDate = applicationDate; 
		this.ipcs = ipcs; 
		this.cpcs = cpcs; 
	}
 
	public Patent(String applicationNo, String applicationDate, String multiIpcs, String multiCpcs, char position, char value) {
		this.applicationNo = applicationNo; 
		this.applicationDate = new Date(applicationDate); 
 
		String[] ipcSymbols = multiIpcs.split(SEPERATOR); 
		this.ipcs = new Ipc[ipcSymbols.length]; 
		for (int i = 0; i < ipcSymbols.length; i++) {
			ipcs[i] = new Ipc(ipcSymbols[i].trim()); 
		}
 
		String[] cpcSymbols = multiCpcs.split(SEPERATOR); 
		this.cpcs = new Cpc[cpcSymbols.length]; 
		for (int i = 0; i < cpcSymbols.length; i++) {
			cpcs[i] = new Cpc(cpcSymbols[i].trim(), position, value); 
		}
	}
 
	public String getApplicationNo() {
		return applicationNo;
	}
 
	public void setApplicationNo(String applicationNo) {
		this.applicationNo = applicationNo;
	}
 
	public Date getApplicationDate() {
		return applicationDate;
	}
 
	public void setApplicationDate(Date applicationDate) {
		this.applicationDate = applicationDate;
	}
 
	public Ipc[] getIpcs() {
		return ipcs;
	}
 
	public void setIpcs(Ipc[] ipcs) {
		this.ipcs = ipcs;
	}
 
	public Cpc[] getCpcs() {
		return cpcs;
	}
 
	public void setCpcs(Cpc[] cpcs) {
		this.cpcs = cpcs;
	}
 
	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder(); 
 
		sb.append(applicationNo + "\t" + applicationDate + "\t"); 
 
		sb.append("["); 
		for (int i = 0; i < ipcs.length - 1; i++) {
			sb.append(ipcs[i] + SEPERATOR); 
		}
		sb.append(ipcs[ipcs.length - 1] + "]\t"); 
 
		sb.append("["); 
		for (int i = 0; i < cpcs.length - 1; i++) {
			sb.append(cpcs[i] + SEPERATOR); 
		}
		sb.append(cpcs[cpcs.length - 1] + "]"); 
 
		return sb.toString(); 
	}
}
CorpusList.java
package cn.edu.bjut.chapter6;
 
import java.util.ArrayList;
import java.util.List;
 
public class CorpusList {
	private List<Patent> data; 
 
	public CorpusList() {
		this.data = new ArrayList<Patent>(); 
	}
 
	public CorpusList(List<Patent> data) {
		this.data = data; 
	}
 
	public CorpusList(Patent[] patents) {
		this(); 
 
		for (Patent patent: patents) {
			data.add(patent); 
		}
	}
 
	public void add(Patent patent) {
		data.add(patent); 
	}
 
	public int size() {
		return data.size(); 
	}
 
	public boolean contains(String applicationNo) {
		if (applicationNo == null || applicationNo.trim().equals("")) {
			return false; 
		}
 
		for (Patent patent: data) {
			if (patent.getApplicationNo().equalsIgnoreCase(applicationNo)) {
				return true; 
			}
		}
 
		return false; 
	}
 
	public boolean contains(Ipc ipc) {
		if (ipc == null) {
			return false; 
		}
 
		for (Patent patent: data) {
			Ipc[] ipcs = patent.getIpcs(); 
			for (Ipc e: ipcs) {
				if (e.equals(ipc)) {
					return true; 
				}
			}
		}
 
		return false; 
	}
 
	public boolean contains(Cpc cpc) {
		if (cpc == null) {
			return false; 
		}
 
		for (Patent patent: data) {
			Cpc[] cpcs = patent.getCpcs(); 
			for (Cpc e: cpcs) {
				if (e.equals(cpc)) {
					return true; 
				}
			}
		}
 
		return false; 
	}
 
	public boolean contains(Date applicationDate) {
		if (applicationDate == null) {
			return false; 
		}
 
		for (Patent patent: data) {
			if (patent.getApplicationDate().equals(applicationDate)) {
				return true; 
			}
		}
 
		return false; 
	}
 
	public CorpusList find(String applicationNo) {
		if (applicationNo == null || applicationNo.trim().equals("")) {
			return null; 
		}
 
		CorpusList corpus = new CorpusList(); 
 
		for (Patent patent: data) {
			if (patent.getApplicationNo().equalsIgnoreCase(applicationNo)) {
				corpus.add(patent);
			}
		}
 
		return corpus.size() == 0? null: corpus;  
	}
 
	public CorpusList find(Ipc ipc) {
		if (ipc == null) {
			return null; 
		}
 
		CorpusList corpus = new CorpusList(); 
 
		for (Patent patent: data) {
			Ipc[] ipcs = patent.getIpcs(); 
			for (Ipc e: ipcs) {
				if (e.equals(ipc)) {
					corpus.add(patent);
					break; 
				}
			}
		}
 
		return corpus.size() == 0? null: corpus; 
	}
 
	public CorpusList find(Cpc cpc) {
		if (cpc == null) {
			return null; 
		}
 
		CorpusList corpus = new CorpusList(); 
 
		for (Patent patent: data) {
			Cpc[] cpcs = patent.getCpcs(); 
			for (Cpc e: cpcs) {
				if (e.equals(cpc)) {
					corpus.add(patent);
					break; 
				}
			}
		}
 
		return corpus.size() == 0? null: corpus; 
	}
 
	public CorpusList find(Date applicationDate) {
		if (applicationDate == null) {
			return null; 
		}
 
		CorpusList corpus = new CorpusList(); 
 
		for (Patent patent: data) {
			if (patent.getApplicationDate().equals(applicationDate)) {
				corpus.add(patent);
			}
		}
 
		return corpus.size() == 0? null: corpus;  
	}
 
	public CorpusList remove(String applicationNo) {
		if (applicationNo == null || applicationNo.trim().equals("")) {
			return this; 
		}
 
		CorpusList corpus = new CorpusList(); 
 
		for (Patent patent: data) {
			if (!patent.getApplicationNo().equalsIgnoreCase(applicationNo)) {
				corpus.add(patent);
			}
		}
 
		return corpus.size() == 0? null: corpus;  
	}
 
	public CorpusList remove(Ipc ipc) {
		if (ipc == null) {
			return this; 
		}
 
		CorpusList corpus = new CorpusList(); 
 
		for (Patent patent: data) {
			boolean flag = false; 
			Ipc[] ipcs = patent.getIpcs(); 
			for (Ipc e: ipcs) {
				if (e.equals(ipc)) {
					flag = true; 
					break;   
				}
			}
 
			if (!flag) {
				corpus.add(patent);
			}
		}
 
		return corpus.size() == 0? null: corpus; 
	}
 
	public CorpusList remove(Cpc cpc) {
		if (cpc == null) {
			return null; 
		}
 
		CorpusList corpus = new CorpusList(); 
 
		for (Patent patent: data) {
			boolean flag = false; 
			Cpc[] cpcs = patent.getCpcs(); 
			for (Cpc e: cpcs) {
				if (e.equals(cpc)) {
					flag = true; 
					break; 
				}
			}
 
			if (!flag) {
				corpus.add(patent);
			}
		}
 
		return corpus.size() == 0? null: corpus; 
	}
 
	public CorpusList remove(Date applicationDate) {
		if (applicationDate == null) {
			return this; 
		}
 
		CorpusList corpus = new CorpusList(); 
 
		for (Patent patent: data) {
			if (!patent.getApplicationDate().equals(applicationDate)) {
				corpus.add(patent);
			}
		}
 
		return corpus.size() == 0? null: corpus;  
	}
 
	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder(); 
 
		for (Patent patent: data) {
			sb.append(patent + "\n"); 
		}
 
		return sb.toString(); 
	}
 
	public static void main(String[] args) {
		Patent[] patents = new Patent[5]; 
		patents[0] = new Patent("14725838", "2015-05-29", "A23B 5/04, A23B 5/045", "A23B 5/04, A23B 5/45, A23L 15/20", 'F', 'I'); 
		patents[1] = new Patent("14814205", "2015-07-30", "A61H 33/00, C01B 33/107, E04B 1/84", "A61H 33/6063, A61H 33/6042", 'F', 'I'); 
		patents[2] = new Patent("15189723", "2016-06-22", "D04B 1/00", "B65H2301/44514, C01B 33/1071, F16F 15/2, H01L 27/14641", 'L', 'A'); 
		patents[3] = new Patent("14711011", "2015-05-13", "B32B 17/10, B32B 27/32", "G06F 3/485", 'L', 'A'); 
		patents[4] = new Patent("14515267", "2014-10-15", "F16F 15/02, G09G 5/34, G06F 3/0485", "A01B 15/06, A01B 15/06", 'L', 'I'); 
 
		CorpusList corpus = new CorpusList(patents); 
		System.out.println(corpus); 
 
		CorpusList corpusFound = corpus.find(new Date("2016-06-22")); 
		System.out.println(corpusFound); 
 
		CorpusList corpusAfterRemovingIpc = corpus.remove(new Ipc("F16F 15/02"));
		System.out.println(corpusAfterRemovingIpc);
 
		CorpusList corpusAfterRemovingCpc =corpus.remove(new Cpc("A23B 5/04", 'F', 'I'));
		System.out.println(corpusAfterRemovingCpc); 
	}
}

【参考答案二】

Classification.java
package cn.edu.bjut.chapter6;
 
public class Classification {
	protected char section;
	protected int mainClass;
	protected char subclass;
	protected int mainGroup;
	protected int subgroup;
 
	public final static char SEPARATOR = '/';
 
	public Classification(char section, int mainClass, char subclass, int mainGroup, int subgroup) {
		this.section = section;
		this.mainClass = mainClass;
		this.subclass = subclass;
		this.mainGroup = mainGroup;
		this.subgroup = subgroup;
	}
 
	// example: G09F 17/30
	public Classification(String symbol) {
		this.section = symbol.charAt(0);
		this.mainClass = Integer.parseInt(symbol.substring(1, 3));
		this.subclass = symbol.charAt(3);
 
		int pos = symbol.indexOf(SEPARATOR);
		this.mainGroup = Integer.parseInt(symbol.substring(4, pos).trim());
		this.subgroup = Integer.parseInt(symbol.substring(pos + 1));
	}
 
	public char getSection() {
		return section;
	}
 
	public void setSection(char section) {
		this.section = section;
	}
 
	public int getMainClass() {
		return mainClass;
	}
 
	public void setMainClass(int mainClass) {
		this.mainClass = mainClass;
	}
 
	public char getSubclass() {
		return subclass;
	}
 
	public void setSubclass(char subclass) {
		this.subclass = subclass;
	}
 
	public int getMainGroup() {
		return mainGroup;
	}
 
	public void setMainGroup(int mainGroup) {
		this.mainGroup = mainGroup;
	}
 
	public int getSubgroup() {
		return subgroup;
	}
 
	public void setSubgroup(int subgroup) {
		this.subgroup = subgroup;
	}
 
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
 
		result = prime * result + mainClass;
		result = prime * result + mainGroup;
		result = prime * result + section;
		result = prime * result + subclass;
		result = prime * result + subgroup;
 
		return result;
	}
 
	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
 
		if (obj == null || getClass() != obj.getClass()) {
			return false;
		}
 
		Classification other = (Classification) obj;
		if (mainClass != other.mainClass || mainGroup != other.mainGroup || 
				section != other.section || subclass != other.subclass || subgroup != other.subgroup) {
			return false;
		}
 
		return true;
	}
 
	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();
 
		sb.append(section);
 
		String strMainClass = String.format("%2s", mainClass).replace(' ', '0');
		sb.append(strMainClass);
 
		sb.append(subclass);
 
		String strMainGroup = String.format("%4s", mainGroup);
		sb.append(strMainGroup);
 
		sb.append(SEPARATOR);
 
		sb.append(subgroup);
 
		return sb.toString();
	}
}
Ipc.java
package cn.edu.bjut.chapter6;
 
public class Ipc extends Classification {
 
	public Ipc(char section, int mainClass, char subclass, int mainGroup, int subgroup) {
		super(section, mainClass, subclass, mainGroup, subgroup); 
	}
 
	public Ipc(String symbol) {
		super(symbol); 
	}
 
	public static void main(String[] args) {
		char section = 'A'; 
		int mainClass = 4; 
		char subclass = 'B'; 
		int mainGroup = 55; 
		int subgroup = 56; 
 
		Classification ipc = new Ipc(section, mainClass, subclass, mainGroup, subgroup); 
		System.out.println(ipc); 
 
		String symbol = "G09F 17/30"; 
		System.out.println(new Ipc(symbol)); 
	}
}
Cpc.java
package cn.edu.bjut.chapter6;
 
public class Cpc extends Classification {
	private char position; 
	private char value; 
 
	public Cpc(char section, int mainClass, char subclass, int mainGroup, int subgroup, char position, char value) {
		super(section, mainClass, subclass, mainGroup, subgroup); 
 
		this.position = position; 
		this.value = value; 
	}
 
	public Cpc(String symbol, char position, char value) {
		super(symbol); 
 
		this.position = position; 
		this.value = value; 
	}
 
	public char getPosition() {
		return position;
	}
 
	public void setPosition(char position) {
		this.position = position;
	}
 
	public char getValue() {
		return value;
	}
 
	public void setValue(char value) {
		this.value = value;
	}
 
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = super.hashCode();
 
		result = prime * result + position;
		result = prime * result + value;
 
		return result;
	}
 
	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
 
		if (!super.equals(obj)) {
			return false;
		}
 
		if (getClass() != obj.getClass()) {
			return false;
		}
 
		Cpc other = (Cpc) obj;
		if (position != other.position || value != other.value) {
			return false;
		}
 
		return true;
	}
}
Date.java
package cn.edu.bjut.chapter6;
 
public class Date {
	private int year, month, day;
 
	public Date(int year, int month, int day) {
		this.year = year;
		this.month = month;
		this.day = day;
	}
 
	// format: YYYY-MM-DD
	public Date(String str) {
		String[] parts = str.trim().split("-"); 
		this.year = Integer.parseInt(parts[0]); 
		this.month = Integer.parseInt(parts[1]); 
		this.day = Integer.parseInt(parts[2]); 
	}
 
	public int getYear() {
		return year;
	}
 
	public void setYear(int year) {
		this.year = year;
	}
 
	public int getMonth() {
		return month;
	}
 
	public void setMonth(int month) {
		this.month = month;
	}
 
	public int getDay() {
		return day;
	}
 
	public void setDay(int day) {
		this.day = day;
	}
 
	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
 
		if (obj == null || getClass() != obj.getClass()) {
			return false;
		}
 
		Date other = (Date) obj;
		if (day != other.day || month != other.month || year != other.year) {
			return false;
		}
 
		return true;
	}
 
	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();
 
		sb.append(year);
		if (month < 10) {
			sb.append("0" + month);
		} else {
			sb.append(month);
		}
		if (day < 10) {
			sb.append("0" + day);
		} else {
			sb.append(day);
		}
 
		return sb.toString();
	}
}
Patent.java
package cn.edu.bjut.chapter6;
 
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
 
public class Patent {
	private String applicationNo; 
	private Date applicationDate; 
	private LinkedHashSet<Ipc> ipcs; 
	private LinkedHashSet<Cpc> cpcs; 
 
	private final static String SEPERATOR = ","; 
 
	public Patent(String applicationNo, Date applicationDate, LinkedHashSet<Ipc> ipcs, LinkedHashSet<Cpc> cpcs) {
		this.applicationNo = applicationNo; 
		this.applicationDate = applicationDate; 
		this.ipcs = ipcs; 
		this.cpcs = cpcs; 
	}
 
	public Patent(String applicationNo, Date applicationDate, Ipc[] ipcs, Cpc[] cpcs) {
		this.applicationNo = applicationNo; 
		this.applicationDate = applicationDate; 
		this.ipcs = new LinkedHashSet<Ipc>(Arrays.asList(ipcs)); 
		this.cpcs = new LinkedHashSet<Cpc>(Arrays.asList(cpcs)); 
	}
 
	public Patent(String applicationNo, String applicationDate, String multiIpcs, String multiCpcs, char position, char value) {
		this.applicationNo = applicationNo; 
		this.applicationDate = new Date(applicationDate); 
 
		String[] ipcSymbols = multiIpcs.split(SEPERATOR); 
		this.ipcs = new LinkedHashSet<Ipc>(ipcSymbols.length); 
		for (int i = 0; i < ipcSymbols.length; i++) {
			Ipc ipc = new Ipc(ipcSymbols[i].trim()); 
			ipcs.add(ipc); 
		}
 
		String[] cpcSymbols = multiCpcs.split(SEPERATOR); 
		this.cpcs = new LinkedHashSet<Cpc>(cpcSymbols.length); 
		for (int i = 0; i < cpcSymbols.length; i++) {
			Cpc cpc = new Cpc(cpcSymbols[i].trim(), position, value); 
			cpcs.add(cpc); 
		}
	}
 
	public String getApplicationNo() {
		return applicationNo;
	}
 
	public void setApplicationNo(String applicationNo) {
		this.applicationNo = applicationNo;
	}
 
	public Date getApplicationDate() {
		return applicationDate;
	}
 
	public void setApplicationDate(Date applicationDate) {
		this.applicationDate = applicationDate;
	}
 
	public Set<Ipc> getIpcs() {
		return ipcs;
	}
 
	public void setIpcs(Ipc[] ipcs) {
		this.ipcs = new LinkedHashSet<Ipc>(Arrays.asList(ipcs));
	}
 
	public Set<Cpc> getCpcs() {
		return cpcs;
	}
 
	public void setCpcs(Cpc[] cpcs) {
		this.cpcs = new LinkedHashSet<Cpc>(Arrays.asList(cpcs));
	}
 
	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder(); 
 
		sb.append(applicationDate + "\t"); 
 
		sb.append("["); 
		List<Ipc> listIpcs = new ArrayList<Ipc>(ipcs); 
		for (int i = 0; i < listIpcs.size() - 1; i++) {
			sb.append(listIpcs.get(i) + SEPERATOR); 
		}
		sb.append(listIpcs.get(listIpcs.size() - 1) + "]\t"); 
 
		sb.append("["); 
		List<Cpc> listCpcs = new ArrayList<Cpc>(cpcs); 
		for (int i = 0; i < listCpcs.size() - 1; i++) {
			sb.append(listCpcs.get(i) + SEPERATOR); 
		}
		sb.append(listCpcs.get(listCpcs.size() - 1) + "]"); 
 
		return sb.toString(); 
	}
}
CorpusList.java
package cn.edu.bjut.chapter6;
 
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
 
public class CorpusList {
	private List<Patent> data; 
 
	public CorpusList() {
		this.data = new ArrayList<Patent>(); 
	}
 
	public CorpusList(List<Patent> data) {
		this.data = data; 
	}
 
	public CorpusList(Patent[] patents) {
		this(); 
 
		for (Patent patent: patents) {
			data.add(patent); 
		}
	}
 
	public void add(Patent patent) {
		data.add(patent); 
	}
 
	public int size() {
		return data.size(); 
	}
 
	public boolean contains(String applicationNo) {
		if (applicationNo == null || applicationNo.trim().equals("")) {
			return false; 
		}
 
		for (Patent patent: data) {
			if (patent.getApplicationNo().equalsIgnoreCase(applicationNo)) {
				return true; 
			}
		}
 
		return false; 
	}
 
	public boolean contains(Ipc ipc) {
		if (ipc == null) {
			return false; 
		}
 
		for (Patent patent: data) {
			Set<Ipc> ipcs = patent.getIpcs(); 
			if (ipcs.contains(ipc)) {
				return true; 
			}
		}
 
		return false; 
	}
 
	public boolean contains(Cpc cpc) {
		if (cpc == null) {
			return false; 
		}
 
		for (Patent patent: data) {
			Set<Cpc> cpcs = patent.getCpcs(); 
			if (cpcs.contains(cpc)) {
				return true; 
			}
		}
 
		return false; 
	}
 
	public boolean contains(Date applicationDate) {
		if (applicationDate == null) {
			return false; 
		}
 
		for (Patent patent: data) {
			if (patent.getApplicationDate().equals(applicationDate)) {
				return true; 
			}
		}
 
		return false; 
	}
 
	public CorpusList find(String applicationNo) {
		if (applicationNo == null || applicationNo.trim().equals("")) {
			return null; 
		}
 
		CorpusList corpus = new CorpusList(); 
 
		for (Patent patent: data) {
			if (patent.getApplicationNo().equalsIgnoreCase(applicationNo)) {
				corpus.add(patent);
			}
		}
 
		return corpus.size() == 0? null: corpus;  
	}
 
	public CorpusList find(Ipc ipc) {
		if (ipc == null) {
			return null; 
		}
 
		CorpusList corpus = new CorpusList(); 
 
		for (Patent patent: data) {
			Set<Ipc> ipcs = patent.getIpcs(); 
			if (ipcs.contains(ipc)) {
				corpus.add(patent);
			}
		}
 
		return corpus.size() == 0? null: corpus; 
	}
 
	public CorpusList find(Cpc cpc) {
		if (cpc == null) {
			return null; 
		}
 
		CorpusList corpus = new CorpusList(); 
 
		for (Patent patent: data) {
			Set<Cpc> cpcs = patent.getCpcs(); 
			if (cpcs.contains(cpc)) {
				corpus.add(patent);
			}
		}
 
		return corpus.size() == 0? null: corpus; 
	}
 
	public CorpusList find(Date applicationDate) {
		if (applicationDate == null) {
			return null; 
		}
 
		CorpusList corpus = new CorpusList(); 
 
		for (Patent patent: data) {
			if (patent.getApplicationDate().equals(applicationDate)) {
				corpus.add(patent);
			}
		}
 
		return corpus.size() == 0? null: corpus;  
	}
 
	public CorpusList remove(String applicationNo) {
		if (applicationNo == null || applicationNo.trim().equals("")) {
			return this; 
		}
 
		CorpusList corpus = new CorpusList(); 
 
		for (Patent patent: data) {
			if (!patent.getApplicationNo().equalsIgnoreCase(applicationNo)) {
				corpus.add(patent);
			}
		}
 
		return corpus.size() == 0? null: corpus;  
	}
 
	public CorpusList remove(Ipc ipc) {
		if (ipc == null) {
			return this; 
		}
 
		CorpusList corpus = new CorpusList(); 
 
		for (Patent patent: data) {
			Set<Ipc> ipcs = patent.getIpcs(); 
			if (!ipcs.contains(ipc)) {
				corpus.add(patent);   
			}
		}
 
		return corpus.size() == 0? null: corpus; 
	}
 
	public CorpusList remove(Cpc cpc) {
		if (cpc == null) {
			return null; 
		}
 
		CorpusList corpus = new CorpusList(); 
 
		for (Patent patent: data) {
			Set<Cpc> cpcs = patent.getCpcs(); 
			if (!cpcs.contains(cpc)) {
				corpus.add(patent);
			}
		}
 
		return corpus.size() == 0? null: corpus; 
	}
 
	public CorpusList remove(Date applicationDate) {
		if (applicationDate == null) {
			return this; 
		}
 
		CorpusList corpus = new CorpusList(); 
 
		for (Patent patent: data) {
			if (!patent.getApplicationDate().equals(applicationDate)) {
				corpus.add(patent);
			}
		}
 
		return corpus.size() == 0? null: corpus;  
	}
 
	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder(); 
 
		for (Patent patent: data) {
			sb.append(patent + "\n"); 
		}
 
		return sb.toString(); 
	}
 
	public static void main(String[] args) {
		Patent[] patents = new Patent[5]; 
		patents[0] = new Patent("14725838", "2015-05-29", "A23B 5/04, A23B 5/045", "A23B 5/04, A23B 5/45, A23L 15/20", 'F', 'I'); 
		patents[1] = new Patent("14814205", "2015-07-30", "A61H 33/00, C01B 33/107, E04B 1/84", "A61H 33/6063, A61H 33/6042", 'F', 'I'); 
		patents[2] = new Patent("15189723", "2016-06-22", "D04B 1/00", "B65H2301/44514, C01B 33/1071, F16F 15/2, H01L 27/14641", 'L', 'A'); 
		patents[3] = new Patent("14711011", "2015-05-13", "B32B 17/10, B32B 27/32", "G06F 3/485", 'L', 'A'); 
		patents[4] = new Patent("14515267", "2014-10-15", "F16F 15/02, G09G 5/34, G06F 3/0485", "A01B 15/06, A01B 15/06", 'L', 'I'); 
 
		CorpusList corpus = new CorpusList(patents); 
		System.out.println(corpus); 
 
		CorpusList corpusFound = corpus.find(new Date("2016-06-22")); 
		System.out.println(corpusFound); 
 
		CorpusList corpusAfterRemovingIpc = corpus.remove(new Ipc("F16F 15/02"));
		System.out.println(corpusAfterRemovingIpc);
 
		CorpusList corpusAfterRemovingCpc =corpus.remove(new Cpc("A23B 5/04", 'F', 'I'));
		System.out.println(corpusAfterRemovingCpc); 
	}
}

【参考答案三】

Data.java
package cn.edu.bjut.chapter6;
 
public class Date {
	private int year, month, day;
 
	public Date(int year, int month, int day) {
		this.year = year;
		this.month = month;
		this.day = day;
	}
 
	// format: YYYY-MM-DD
	public Date(String str) {
		String[] parts = str.trim().split("-"); 
		this.year = Integer.parseInt(parts[0]); 
		this.month = Integer.parseInt(parts[1]); 
		this.day = Integer.parseInt(parts[2]); 
	}
 
	public int getYear() {
		return year;
	}
 
	public void setYear(int year) {
		this.year = year;
	}
 
	public int getMonth() {
		return month;
	}
 
	public void setMonth(int month) {
		this.month = month;
	}
 
	public int getDay() {
		return day;
	}
 
	public void setDay(int day) {
		this.day = day;
	}
 
	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
 
		if (obj == null || getClass() != obj.getClass()) {
			return false;
		}
 
		Date other = (Date) obj;
		if (day != other.day || month != other.month || year != other.year) {
			return false;
		}
 
		return true;
	}
 
	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();
 
		sb.append(year);
		if (month < 10) {
			sb.append("0" + month);
		} else {
			sb.append(month);
		}
		if (day < 10) {
			sb.append("0" + day);
		} else {
			sb.append(day);
		}
 
		return sb.toString();
	}
}
</filie>
 
<file java Classification.java>
package cn.edu.bjut.chapter6;
 
public class Classification {
	protected char section;
	protected int mainClass;
	protected char subclass;
	protected int mainGroup;
	protected int subgroup;
 
	public final static char SEPARATOR = '/';
 
	public Classification(char section, int mainClass, char subclass, int mainGroup, int subgroup) {
		this.section = section;
		this.mainClass = mainClass;
		this.subclass = subclass;
		this.mainGroup = mainGroup;
		this.subgroup = subgroup;
	}
 
	// example: G09F 17/30
	public Classification(String symbol) {
		this.section = symbol.charAt(0);
		this.mainClass = Integer.parseInt(symbol.substring(1, 3));
		this.subclass = symbol.charAt(3);
 
		int pos = symbol.indexOf(SEPARATOR);
		this.mainGroup = Integer.parseInt(symbol.substring(4, pos).trim());
		this.subgroup = Integer.parseInt(symbol.substring(pos + 1));
	}
 
	public char getSection() {
		return section;
	}
 
	public void setSection(char section) {
		this.section = section;
	}
 
	public int getMainClass() {
		return mainClass;
	}
 
	public void setMainClass(int mainClass) {
		this.mainClass = mainClass;
	}
 
	public char getSubclass() {
		return subclass;
	}
 
	public void setSubclass(char subclass) {
		this.subclass = subclass;
	}
 
	public int getMainGroup() {
		return mainGroup;
	}
 
	public void setMainGroup(int mainGroup) {
		this.mainGroup = mainGroup;
	}
 
	public int getSubgroup() {
		return subgroup;
	}
 
	public void setSubgroup(int subgroup) {
		this.subgroup = subgroup;
	}
 
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
 
		result = prime * result + mainClass;
		result = prime * result + mainGroup;
		result = prime * result + section;
		result = prime * result + subclass;
		result = prime * result + subgroup;
 
		return result;
	}
 
	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
 
		if (obj == null || getClass() != obj.getClass()) {
			return false;
		}
 
		Classification other = (Classification) obj;
		if (mainClass != other.mainClass || mainGroup != other.mainGroup || 
				section != other.section || subclass != other.subclass || subgroup != other.subgroup) {
			return false;
		}
 
		return true;
	}
 
	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();
 
		sb.append(section);
 
		String strMainClass = String.format("%2s", mainClass).replace(' ', '0');
		sb.append(strMainClass);
 
		sb.append(subclass);
 
		String strMainGroup = String.format("%4s", mainGroup);
		sb.append(strMainGroup);
 
		sb.append(SEPARATOR);
 
		sb.append(subgroup);
 
		return sb.toString();
	}
}
Ipc.java
package cn.edu.bjut.chapter6;
 
public class Ipc extends Classification {
 
	public Ipc(char section, int mainClass, char subclass, int mainGroup, int subgroup) {
		super(section, mainClass, subclass, mainGroup, subgroup); 
	}
 
	public Ipc(String symbol) {
		super(symbol); 
	}
 
	public static void main(String[] args) {
		char section = 'A'; 
		int mainClass = 4; 
		char subclass = 'B'; 
		int mainGroup = 55; 
		int subgroup = 56; 
 
		Classification ipc = new Ipc(section, mainClass, subclass, mainGroup, subgroup); 
		System.out.println(ipc); 
 
		String symbol = "G09F 17/30"; 
		System.out.println(new Ipc(symbol)); 
	}
}
Cpc.java
package cn.edu.bjut.chapter6;
 
public class Cpc extends Classification {
	private char position; 
	private char value; 
 
	public Cpc(char section, int mainClass, char subclass, int mainGroup, int subgroup, char position, char value) {
		super(section, mainClass, subclass, mainGroup, subgroup); 
 
		this.position = position; 
		this.value = value; 
	}
 
	public Cpc(String symbol, char position, char value) {
		super(symbol); 
 
		this.position = position; 
		this.value = value; 
	}
 
	public char getPosition() {
		return position;
	}
 
	public void setPosition(char position) {
		this.position = position;
	}
 
	public char getValue() {
		return value;
	}
 
	public void setValue(char value) {
		this.value = value;
	}
 
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = super.hashCode();
 
		result = prime * result + position;
		result = prime * result + value;
 
		return result;
	}
 
	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
 
		if (!super.equals(obj)) {
			return false;
		}
 
		if (getClass() != obj.getClass()) {
			return false;
		}
 
		Cpc other = (Cpc) obj;
		if (position != other.position || value != other.value) {
			return false;
		}
 
		return true;
	}
}
Patent.java
package cn.edu.bjut.chapter6;
 
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
 
public class Patent {
	//private String applicationNo; 
	private Date applicationDate; 
	private LinkedHashSet<Ipc> ipcs; 
	private LinkedHashSet<Cpc> cpcs; 
 
	private final static String SEPERATOR = ","; 
 
	public Patent(Date applicationDate, LinkedHashSet<Ipc> ipcs, LinkedHashSet<Cpc> cpcs) {
		this.applicationDate = applicationDate; 
		this.ipcs = ipcs; 
		this.cpcs = cpcs; 
	}
 
	public Patent(Date applicationDate, Ipc[] ipcs, Cpc[] cpcs) {
		this.applicationDate = applicationDate; 
		this.ipcs = new LinkedHashSet<Ipc>(Arrays.asList(ipcs)); 
		this.cpcs = new LinkedHashSet<Cpc>(Arrays.asList(cpcs)); 
	}
 
	public Patent(String applicationDate, String multiIpcs, String multiCpcs, char position, char value) {
		this.applicationDate = new Date(applicationDate); 
 
		String[] ipcSymbols = multiIpcs.split(SEPERATOR); 
		this.ipcs = new LinkedHashSet<Ipc>(ipcSymbols.length); 
		for (int i = 0; i < ipcSymbols.length; i++) {
			Ipc ipc = new Ipc(ipcSymbols[i].trim()); 
			ipcs.add(ipc); 
		}
 
		String[] cpcSymbols = multiCpcs.split(SEPERATOR); 
		this.cpcs = new LinkedHashSet<Cpc>(cpcSymbols.length); 
		for (int i = 0; i < cpcSymbols.length; i++) {
			Cpc cpc = new Cpc(cpcSymbols[i].trim(), position, value); 
			cpcs.add(cpc); 
		}
	}
 
	public Date getApplicationDate() {
		return applicationDate;
	}
 
	public void setApplicationDate(Date applicationDate) {
		this.applicationDate = applicationDate;
	}
 
	public Set<Ipc> getIpcs() {
		return ipcs;
	}
 
	public void setIpcs(Ipc[] ipcs) {
		this.ipcs = new LinkedHashSet<Ipc>(Arrays.asList(ipcs));
	}
 
	public Set<Cpc> getCpcs() {
		return cpcs;
	}
 
	public void setCpcs(Cpc[] cpcs) {
		this.cpcs = new LinkedHashSet<Cpc>(Arrays.asList(cpcs));
	}
 
	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder(); 
 
		sb.append(applicationDate + "\t"); 
 
		sb.append("["); 
		List<Ipc> listIpcs = new ArrayList<Ipc>(ipcs); 
		for (int i = 0; i < listIpcs.size() - 1; i++) {
			sb.append(listIpcs.get(i) + SEPERATOR); 
		}
		sb.append(listIpcs.get(listIpcs.size() - 1) + "]\t"); 
 
		sb.append("["); 
		List<Cpc> listCpcs = new ArrayList<Cpc>(cpcs); 
		for (int i = 0; i < listCpcs.size() - 1; i++) {
			sb.append(listCpcs.get(i) + SEPERATOR); 
		}
		sb.append(listCpcs.get(listCpcs.size() - 1) + "]"); 
 
		return sb.toString(); 
	}
}
Corpus.java
package cn.edu.bjut.chapter6;
 
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
 
public class Corpus {
	private Map<String, Patent> data; 
 
	public Corpus() {
		this.data = new HashMap<String, Patent>(); 
	}
 
	public Corpus(Map<String, Patent> data) {
		this.data = data; 
	}
 
	public void add(String applicationNo, Patent patent) {
		if (applicationNo == null || applicationNo.trim().equals("")) {
			return; 
		}
 
		data.put(applicationNo.trim().toUpperCase(), patent); 
	}
 
	public int size() {
		return data.size(); 
	}
 
	public boolean contains(String applicationNo) {
		if (applicationNo == null || applicationNo.trim().equals("")) {
			return false; 
		}
 
		return data.containsKey(applicationNo.trim().toUpperCase()); 
	}
 
	public boolean contains(Ipc ipc) {
		if (ipc == null) {
			return false; 
		}
 
		for (Patent patent: data.values()) {
			Set<Ipc> ipcs = patent.getIpcs(); 
			if (ipcs.contains(ipc)) {
				return true; 
			}
		}
 
		return false; 
	}
 
	public boolean contains(Cpc cpc) {
		if (cpc == null) {
			return false; 
		}
 
		for (Patent patent: data.values()) {
			Set<Cpc> cpcs = patent.getCpcs(); 
			if (cpcs.contains(cpc)) {
				return true; 
			}
		}
 
		return false; 
	}
 
	public boolean contains(Date applicationDate) {
		if (applicationDate == null) {
			return false; 
		}
 
		for (Patent patent: data.values()) {
			if (patent.getApplicationDate().equals(applicationDate)) {
				return true; 
			}
		}
 
		return false; 
	}
 
	public Corpus find(String applicationNo) {
		if (!contains(applicationNo)) {
			return null; 
		}
 
		Map<String, Patent> subData = new HashMap<String, Patent>(); 
 
		String applicationNoAfter = applicationNo.trim().toUpperCase(); 
		subData.put(applicationNoAfter, data.get(applicationNoAfter)); 
 
		return new Corpus(subData); 
	}
 
	public Corpus find(Ipc ipc) {
		if (!contains(ipc)) {
			return null; 
		}
 
		Map<String, Patent> subData = new HashMap<String, Patent>(); 
 
		for (Map.Entry<String, Patent> entry: data.entrySet()) {
			Set<Ipc> ipcs = entry.getValue().getIpcs(); 
			if (ipcs.contains(ipc)) {
				subData.put(entry.getKey(), entry.getValue()); 
			}
		}
 
		return new Corpus(subData); 
	}
 
	public Corpus find(Cpc cpc) {
		if (!contains(cpc)) {
			return null; 
		}
 
		Map<String, Patent> subData = new HashMap<String, Patent>(); 
 
		for (Map.Entry<String, Patent> entry: data.entrySet()) {
			Set<Cpc> cpcs = entry.getValue().getCpcs(); 
			if (cpcs.contains(cpc)) {
				subData.put(entry.getKey(), entry.getValue()); 
			}
		}
 
		return new Corpus(subData); 
	}
 
	public Corpus find(Date applicationDate) {
		if (!contains(applicationDate)) {
			return null; 
		}
 
		Map<String, Patent> subData = new HashMap<String, Patent>(); 
 
		for (Map.Entry<String, Patent> entry: data.entrySet()) {
			Date date = entry.getValue().getApplicationDate(); 
			if (date.equals(applicationDate)) {
				subData.put(entry.getKey(), entry.getValue()); 
			}
		}
 
		return new Corpus(subData); 
	}
 
	public void remove(String applicationNo) {
		if (!contains(applicationNo)) {
			return; 
		}
 
		data.remove(applicationNo.trim().toUpperCase()); 
	}
 
	public void remove(Ipc ipc) {
		if (!contains(ipc)) {
			return; 
		}
 
		Set<String> keys = new HashSet<String>(); 
		for (Map.Entry<String, Patent> entry: data.entrySet()) {
			Set<Ipc> ipcs = entry.getValue().getIpcs(); 
			if (ipcs.contains(ipc)) {
				keys.add(entry.getKey()); 
			}
		}
 
		for (String key: keys) {
			data.remove(key); 
		}
	}
 
	public void remove(Cpc cpc) {
		if (!contains(cpc)) {
			return; 
		}
 
		Set<String> keys = new HashSet<String>(); 
		for (Map.Entry<String, Patent> entry: data.entrySet()) {
			Set<Cpc> cpcs = entry.getValue().getCpcs(); 
			if (cpcs.contains(cpc)) {
				keys.add(entry.getKey()); 
			}
		}
 
		for (String key: keys) {
			data.remove(key); 
		}
	}
 
	public void remove(Date applicationDate) {
		if (!contains(applicationDate)) {
			return; 
		}
 
		Set<String> keys = new HashSet<String>(); 
		for (Map.Entry<String, Patent> entry: data.entrySet()) {
			Date date = entry.getValue().getApplicationDate(); 
			if (date.equals(applicationDate)) {
				keys.add(entry.getKey()); 
			}
		}
 
		for (String key: keys) {
			data.remove(key); 
		}
	}
 
	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder(); 
 
		for (Map.Entry<String, Patent> entry: data.entrySet()) {
			sb.append(entry.getKey() + "\t" + entry.getValue() + "\n"); 
		}
 
		return sb.toString(); 
	}
 
	public static void main(String[] args) {
		Corpus corpus = new Corpus(); 
 
		corpus.add("14725838", new Patent("2015-05-29", "A23B 5/04, A23B 5/045", "A23B 5/04, A23B 5/45, A23L 15/20", 'F', 'I'));
		corpus.add("14814205", new Patent("2015-07-30", "A61H 33/00, C01B 33/107, E04B 1/84", "A61H 33/6063, A61H 33/6042", 'F', 'I'));
		corpus.add("15189723", new Patent("2016-06-22", "D04B 1/00", "B65H2301/44514, C01B 33/1071, F16F 15/2, H01L 27/14641", 'L', 'A')); 
		corpus.add("14711011", new Patent("2015-05-13", "B32B 17/10, B32B 27/32, F16F 15/02", "G06F 3/485", 'L', 'A')); 
		corpus.add("14515267", new Patent("2014-10-15", "F16F 15/02, G09G 5/34, G06F 3/0485", "A01B 15/06, A01B 15/06", 'L', 'I')); 
 
		System.out.println(corpus); 
 
		corpus.remove(new Ipc("F16F 15/02"));
		System.out.println(corpus);
 
		corpus.remove(new Cpc("A23B 5/04", 'F', 'I'));
		System.out.println(corpus); 
	}
}

返回Java课程页

zh/courses/java2025/ch06.1742733194.txt.gz · 最后更改: 2025/03/23 20:33 由 pzczxs