Collections framework - tools (the Collections)

First, the basic meaning

It is a wrapper class. It includes various static method of relating a set of multi-state operation. This class can not be instantiated, as a tool (such as Arrays), serving in the Java Collection Framework.

Second, the sort --sort ()

2.1 String class elements

Elements of type String
Collation:
(1) comparing character by character, until the outcome. That is the same, the next comparison.
(2) (0-9) <( AZ) <(az).

// 元素类型:字符串。排序规则:0-9,A-Z,a-z
public void testSort2() {
	List<String> stringList = new ArrayList<String>();
	for (int i = 0; i < 6; i++) {
		stringList.add(randomString());
	}
	System.out.println("\n------------排序前--------------");
	for (int i = 0; i < stringList.size(); i++) {
		System.out.print(stringList.get(i) + " ");
	}
	Collections.sort(stringList); // 排序sort()调用。
	System.out.println("\n------------排序后--------------");
	Iterator<String> it = stringList.iterator();
	while (it.hasNext()) {
		String str = it.next();
		System.out.print(str + " ");
	}
}
// 方法:随机字符串。长度小于10。字符随机。
  public String randomString() {
    String str = "";
	Random rand = new Random();
	Integer num = 1 + rand.nextInt(9);
	for (int i = 0; i < num; i++) {
		str = str + randomChar();
	}
	return str;
}
// 方法:随机字符。
public char randomChar() {
	char c;
	Random rand = new Random();
	while (true) {
		int num = rand.nextInt(123);			
		if (num > 96) {
			// 此区间转化为小写字母。
			c = (char)num;
			break;
		} else if (num > 64 && num < 91) {
			// 此区间转化为大写字母。
			c = (char) num;
			break;
		} else if (num > 47 && num < 58) {
			// 此区间转化为数字。
			c = (char) num;
			break;
		} 
	}
	return c;
}	

2.2 packaging elements

When the data element is substantially wrapper type.

// 元素类型:包装类。
 public void testSort1() {
	List<Integer> integerList = new ArrayList<Integer>();
	Random random = new Random();
	Integer num;
	for (int i = 0; i < 10; i++) {
		do {
			num = random.nextInt(100);
		} while (integerList.contains(num));
		integerList.add(num);
	}
	System.out.println("------------排序前--------------");
	for (Integer in : integerList) {
		System.out.print(in + " ");
	}
	Collections.sort(integerList);
	System.out.println("\n------------排序后--------------");
	Iterator<Integer> it = integerList.iterator();
	while (it.hasNext()) {
		Integer i = it.next();
		System.out.print(i + " ");
	}
}

2.3 earth element

Comparable binding need Comparator interface or interfaces.
I.e., the first class implements the interface, and the corresponding method of rewriting.
Implement Comparable when the interface: the default sort.

public class Student implements Comparable{
  private String id;
  private String name;
  public Student(){
  }
  public Student(String id,String name){
    this.id = id;
    this.name = name;
  }
  @Override
  public int compareTo(Student o){
    return this.id.compareTo(o.getId());
  }
}

Implement Comparator When Interface: temporary sort.

public class Course implemants Comparator{
  private String id;
  private String name;
  public Course(){
  }
  public Course(String id,String name){
    this.id = id;
    this.name = name;
  }
  @Override
  public int compare(Course1 o1, Course2 o2){
    return o1.name.compare(o2.name); // 此时为升序排列。
    //return o2.name.compare(o1.name); // 此时为降序排列。
  }
}

Call ** Collection.sort () ** method:

List<Student> studentList = new ArrayList<Student>();
Student[] studentArray = {new Student("1","张三"),new Student("3","李四"),new Student("2","王五")};
studentArray.addAll(Arrays.asList(studentList));
List<Course> courseList = new ArrayList<Course>();
Course[] courseArray = {new Course("1","语文"),new Course("3","英语"),new Course("2","数学")};
courseArray.addAll(Arrays.asList(courseList));
Collections.sort(studentList);
Collections.sort(courseArray, new Course());

Guess you like

Origin blog.csdn.net/lizengbao/article/details/86748339
Recommended