Collections类解析
Collections类解析
fengsehng 发表于1年前
Collections类解析
• 发表于 1年前
• 阅读 0
• 收藏 0
• 评论 0

代码：

负整数、零或正整数，根据此对象是小于、等于还是大于指定对象。

``````class Employee  implements Comparable<Employee>
{
public Employee(String n, double s)
{
name = n;
salary = s;
Random ID =  new Random();
id = ID.nextInt( 10000000 );
}
public int getId()
{
return id;
}
public String getName()
{
return name;
}
public double getSalary()
{
return salary;
}
public void raiseSalary( double byPercent)
{
double raise  = salary *byPercent/ 100 ;
salary+=raise;
}
public int compareTo(Employee other)
{
if (id<other.id) //这里比较的是什么 sort方法实现的就是按照此比较的东西从小到大排列
return - 1 ;
if (id>other.id)
return 1 ;
return 0 ;
}
private int id;
private String name;
private double salary;
}
``````

2)混排（Shuffling）

``````Collections.Shuffling(list)
double array[] = {112, 111, 23, 456, 231 };
for (int i = 0; i < array.length; i++) {
list.add(new Double(array[i]));
}
Collections.shuffle(list);
for (int i = 0; i < array.length; i++) {
System.out.println(li.get(i));
}
//结果：112,111,23,456,231``````

3) 反转(Reverse)

``````  使用Reverse方法可以根据元素的自然顺序 对指定列表按降序进行排序。
``````
``````Collections.reverse(list)
double array[] = {112, 111, 23, 456, 231 };
for (int i = 0; i < array.length; i++) {
list.add(new Double(array[i]));
}
Collections. reverse (list);
for (int i = 0; i < array.length; i++) {
System.out.println(li.get(i));
}
//结果：231,456,23,111,112``````

4) 替换所以的元素(Fill)

``````使用指定元素替换指定列表中的所有元素。
String str[] = {"dd","aa","bb","cc","ee"};
for(int j=0;j<str.length;j++){
li.add(new String(str[j]));
}
Collections.fill(li,"aaa");
for (int i = 0; i < li.size(); i++) {
System.out.println("list[" + i + "]=" + li.get(i));

}
//结果：aaa,aaa,aaa,aaa,aaa``````

5) 拷贝(Copy)

``````Collections.copy(list,li): 后面一个参数是目标列表 ,前一个是源列表
double array[] = {112, 111, 23, 456, 231 };
List list = new ArrayList();
List li = new ArrayList();
for (int i = 0; i < array.length; i++) {
list.add(new Double(array[i]));
}
double arr[] = {1131,333};
String str[] = {"dd","aa","bb","cc","ee"};
for(int j=0;j<arr.length;j++){
li.add(new Double(arr[j]));
}
Collections.copy(list,li);
for (int i = 0; i <list.size(); i++) {
System.out.println("list[" + i + "]=" + list.get(i));
}
//结果：1131,333,23,456,231``````

6) 返回Collections中最小元素(min)

``````Collections.min(list)
double array[] = {112, 111, 23, 456, 231 };
List list = new ArrayList();
for (int i = 0; i < array.length; i++) {
list.add(new Double(array[i]));
}
Collections.min(list);
for (int i = 0; i <list.size(); i++) {
System.out.println("list[" + i + "]=" + list.get(i));
}
//结果：23``````

7) 返回Collections中最大元素(max)

``````Collections.max(list)
double array[] = {112, 111, 23, 456, 231 };
List list = new ArrayList();
for (int i = 0; i < array.length; i++) {
list.add(new Double(array[i]));
}
Collections.max(list);
for (int i = 0; i <list.size(); i++) {
System.out.println("list[" + i + "]=" + list.get(i));
}
//结果：456``````

8) lastIndexOfSubList

``````int count = Collections.lastIndexOfSubList(list,li);
double array[] = {112, 111, 23, 456, 231 };
List list = new ArrayList();
List li = new ArrayList();
for (int i = 0; i < array.length; i++) {
list.add(new Double(array[i]));
}
double arr[] = {111};
String str[] = {"dd","aa","bb","cc","ee"};
for(int j=0;j<arr.length;j++){
li.add(new Double(arr[j]));
}
Int locations = Collections. lastIndexOfSubList (list,li);
System.out.println(“===”+ locations);
//结果 3``````

9) IndexOfSubList

``````int count = Collections.indexOfSubList(list,li);
double array[] = {112, 111, 23, 456, 231 };
List list = new ArrayList();
List li = new ArrayList();
for (int i = 0; i < array.length; i++) {
list.add(new Double(array[i]));
}
double arr[] = {111};
String str[] = {"dd","aa","bb","cc","ee"};
for(int j=0;j<arr.length;j++){
li.add(new Double(arr[j]));
}
Int locations = Collections.indexOfSubList(list,li);
System.out.println(“===”+ locations);
//结果 1``````

10) Rotate

``````Collections.rotate(list,-1);

double array[] = {112, 111, 23, 456, 231 };
List list = new ArrayList();
for (int i = 0; i < array.length; i++) {
list.add(new Double(array[i]));
}
Collections.rotate(list,-1);
for (int i = 0; i <list.size(); i++) {
System.out.println("list[" + i + "]=" + list.get(i));
}
//结果：111,23,456,231,112``````

×