TreeMap集合工具类-变长参数

TreeMap集合工具类

数组拷贝

int [] src= {1,2,3,4};
 int [] dest=new int[5];
 System.arraycopy(src, 1, dest,1, 4);
 System.out.println(dest);

结果:

src = |1|2|3|4|
dest = |0|0|0|0|0|
执行System.arraycopy(src, 0, dest, 1, 4);时
第一步:从源数组(src)中,从下标0开始取,取4个,也就是src[0]-src[3],即1 2 3 4四个数
第二步:把取出的数,按顺序,存放到目标数组(dest)中,从下标1开始存,存4个,也就是dest[1]-dest[4]
所以数组dest为:|0|1|2|3|4|

创建new的情况下拷贝

Person[] src0= {new Person(100),new Person(200),new Person(300),new Person(400)};
Person [] dest0=new Person[6];
System.arraycopy(src0, 1, dest0,2, 3);
System.out.println(dest0);

结果:和上面的推导一模一样

在上述代码的前提下:

src[2]=1000;
System.out.println(desc[3]);
src0[2].setHeight(1000);
System.out.println(dest0[3].getHeight());

结果:

3
1000

为什么一个修改了,一个没修改?
前者拷贝的是数组,而后者是地址。就像引用调用和传值调用一个意思
得到同步集合

List<String>names=Collections.synchronizedList(new ArrayList<String>());

最大值/最小值

Collections.max()  min()

排序

Collections.sort()

将asList将参数转换成Array$ArrayLst集合对象

List list=Arrays.asList(1,2,3,4);

数组排序

Arrays.sort(a);

二分法查找

Arrays.binarySearch();

变长参数

Person类

public class Person {
private int height;
private List<String>tels=new ArrayList<>();
public int getHeight() {
 return height;
}
public void setHeight(int height) {
 this.height = height;
}
public Person(int height) {
 super();
 this.height = height;
}
//变长参数,可变参数,相当于数组,变长参数必须作为方法的最后参数
//public void addTels(String s1,String...str);//ok
//public void addTels(String...str,String s1)//error
public void addTel(String... str) {
 for(int i=0;i<str.length;i++)
 {
  tels.add(str[i]);
 }
 //或者
 //for(String s:str) {
  //tels.add(s);
// }
}
}

Demo类

Person p=new Person(1000);
p.addTel("1245567","123","23131","98419881");
//没变长之前,只能一个个号码添加

自定义对比器

public class PersonComparator implements Comparator<Person> {
 public int compare(Person o1, Person o2) {
  if(o1==null) {
   if(o2==null) {
    return 0;
   }
   else {
    return -1;
   }
  }
  else {
   if(o2==null) {
    return 1;
   }
   else {
    return o1.getHeight()-o2.getHeight();
   }
  }
 }
}

Person类

public class Person {
private int height;
private List<String>tels=new ArrayList<>();
public int getHeight() {
 return height;
}
public void setHeight(int height) {
 this.height = height;
}
public Person(int height) {
 super();
 this.height = height;
}
}

Demo

TreeSet<Person>ts2=new TreeSet<Person>(new PersonComparator());
 ts2.add(new Person(1));
 ts2.add(new Person(1));
 ts2.add(new Person(2));
 ts2.add(new Person(3));
 ts2.add(new Person(4));
 System.out.println(ts2.size());
 }
}

结果:

4

没有对比器出错,在原Demo中没有比较

Exception in thread "main" java.lang.ClassCastException: class fanyongjunwork.Person cannot be cast to class java.lang.Comparable (fanyongjunwork.Person is in module fanyongjun of loader 'app'; java.lang.Comparable is in module java.base of loader 'bootstrap')
 at java.base/java.util.TreeMap.compare(TreeMap.java:1291)
 at java.base/java.util.TreeMap.put(TreeMap.java:536)
 at java.base/java.util.TreeSet.add(TreeSet.java:255)
 at fanyongjun/fanyongjunwork.TreeSetDemo.main(TreeSetDemo.java:16)

若添加下列学生类:
Student

public class Student implements Comparable<Student> {
 private int age;
 private int height;
 private int weight;
 public int getAge() {
  return age;
 }
 public void setAge(int age) {
  this.age = age;
 }
 public int getHeight() {
  return height;
 }
 public void setHeight(int height) {
  this.height = height;
 }
 public int getWeight() {
  return weight;
 }
 public void setWeight(int weight) {
  this.weight = weight;
 }
 public Student(int age, int height, int weight) {
  super();
  this.age = age;
  this.height = height;
  this.weight = weight;
 }
public int hashCode() {
 return this.height | this.weight<<8|this.age<<16;
}
public boolean equals(Object obj) {
 if (obj==null) return false;
 if(obj==this) return true;
 if(obj instanceof Student) {
  Student s0=(Student)obj;
  return this.hashCode()==obj.hashCode();
 }
 return false;
}
public int compareTo(Student o) {
 // TODO Auto-generated method stub
 if(o==null) {
  return 1;
 }
 return this.height-o.height;
}
public String toString() {
 return  "Student("+height+","+weight+","+age+")";
}
 }

在上述student中,我们实现了比较,所以在Demo中不需要添加Comparator

public static void main(String[] args) {
TreeSet<Student>st=new TreeSet<Student>();
st.add(new Student(1,2,3));
st.add(new Student(3,2,1));
st.add(new Student(3,1,2));
System.out.println(st.size());

结果:

2

在学生类里面我们写入了对比器,上面的person类我们单独创建了对比器,两种方法都是可以的。
TreeSet不像其他的集合,TreeSet
需要对比器就如同其他需要重写equals和HashCode一个意思

发布了50 篇原创文章 · 获赞 75 · 访问量 6678

猜你喜欢

转载自blog.csdn.net/weixin_45822638/article/details/104095231