Java based approach 10 (collection)

1.Set collection

import java.util.Iterator;
import java.util.TreeSet;
public class UpdateStu implements Comparable<Object>{  //创建类实现Comparable接口
	String name;                            
	long id;
	public UpdateStu(String name, long id) {            //构造方法
		this.id = id;
		this.name = name;	
	}
	public int compareTo(Object o) {
		UpdateStu upstu = (UpdateStu)o;
		int result = id>upstu.id?1:(id==upstu.id?0:-1);    //参照代码说明
		return result;		
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public long getId() {
		return id;
	}
	public void setId(long id) {
		this.id = id;
	}
	public static void main(String[] args) {
		// TODO 自动生成的方法存根
		UpdateStu stu1 = new UpdateStu("李同学",01011);  
		UpdateStu stu2 = new UpdateStu("陈同学",01021);  //参照代码说明
		UpdateStu stu3 = new UpdateStu("王同学",01051);
		UpdateStu stu4 = new UpdateStu("马同学",01012);
		TreeSet<UpdateStu> tree = new TreeSet<>();
		tree.add(stu1);                                //向集合添加对象
		tree.add(stu2);
		tree.add(stu3);
		tree.add(stu4);
		Iterator<UpdateStu> it = tree.iterator();   //Set集合中所有对象的迭代器
		System.out.println("Set集合中的所有元素:");;
		while(it.hasNext()) {                            //遍历集合
			UpdateStu stu = (UpdateStu)it.next();
			System.out.println(stu.getId()+" "+stu.getName());
		}
		it = tree.headSet(stu2).iterator();           
//截取排在stu2对象之前的对象
		System.out.println("截取前面部分的集合: ");
		while(it.hasNext()) {                          //遍历集合
			UpdateStu stu = (UpdateStu)it.next();
			System.out.println(stu.getId()+" "+stu.getName());
		}
		it = tree.subSet(stu2, stu3).iterator();       
//截取排在stu2与stu3之间的对象
		System.out.println("截取中间部分的集合:");
		while(it.hasNext()) {                          //遍历集合
			UpdateStu stu = (UpdateStu)it.next();
			System.out.println(stu.getId()+" "+stu.getName());
		}
		
	}
}

2.List collection

	List<String> list = new ArrayList<>();
	list.add("a");
	list.add("b");
	list.add("c");
	int i = (int)(Math.random()*list.size());
	System.out.println("随机获取数组中的元素:"+list.get(i));
	list.remove(2);
	System.out.println("将索引是'2'的元素从数组中移除后,数组中的元素是:");
		for(int j=0; j<list.size(); j++) {
			System.out.println(list.get(j));
		}

3.Collection Interface

Collection<String> list = new ArrayList<>();
		list.add("a");                        //向集合添加数据
		list.add("b");
		list.add("c");
		Iterator<String> it = list.iterator();//创建迭代器
		while(it.hasNext()) {
			String str = (String)it.next();   //获取集合中的元素
			System.out.println(str);
		}

Comparator interface Comparable interface

  • Comparable and Comparator are used to implement a set of elements compared sorted.
  • Comparable is defined internally set the compareTo () method implementation sorting, located in the java.lang package.
  • Comparator is implemented in a set of external sorting, located in java.util package.

Example:

** Comparable: **
// 实现Comparable接口要覆盖compareTo方法, 在compareTo方法里面实现比较:
public class Person implements Comparable {
     private int age;
     public int compareTo(Person that) {
          if(this.age > that.age) return +1;
          if(this.age < that.age) return -1;
          return 0;                     // 返回age的比较结果.
     }
}
// 这时我们可以直接用 Collections.sort( personList ) 对其排序了.

**********************************************************************

**Comparator:**
// 实现Comparator需要覆盖 compare 方法:
public class Person{
     int age;
}

class PersonComparator implements Comparator { 
     public int compare(Person one, Person another) {
          if(this.age > that.age) return +1;
          if(this.age < that.age) return -1;
          return 0;                     // 返回age的比较结果.
     }
}
// Collections.sort( personList , new PersonComparator()) 可以对其排序

Guess you like

Origin blog.csdn.net/affluent6/article/details/91525213