【工作记录】重写equals()解决List的indexOf()失效问题

      前几天看公司代码时候,同事写了一个从中间扭转List的方法,挺好玩的,自己写了一个,List包含了一个Form,基本代码如下:

package exec;

import java.util.LinkedList;
import java.util.List;
import java.util.Random;
//主程序类 - vincent
public class ChangeSequence {

	public static void main(String[] args) {
		List<OrderForm> orderList = new LinkedList<OrderForm>();
		orderList = getOrderInfo();
		orderList = sequenceChange(orderList, 3);
	}

	public static List<OrderForm> sequenceChange(List<OrderForm> params, Integer orderId){
		if(null == orderId){
			return new LinkedList<>(params);
		}
		
		OrderForm form = new OrderForm();
		
		form.setId(params.get(3).getId());
		int index = params.indexOf(form);
		
		//是否全部长度
		if(index < params.size()){
			List<OrderForm> formList = new LinkedList<OrderForm>();
			formList.addAll(params.subList(index, params.size()));
			formList.addAll(params.subList(0, index));
			
			for(OrderForm form1 : formList){
				System.out.println("排序之后的顺序" + form1.getId());
			}
			
			return formList;
		}
		return new LinkedList<>(params);
	}
	
	public static List<OrderForm> getOrderInfo(){
		List<OrderForm> orderList = new LinkedList<OrderForm>();
		for(int i=0;i<10;i++){
			OrderForm form = new OrderForm();
			form.setId((long)i);
			form.setName("员工" + i);
			Random random = new Random(100);
			form.setScore(random);
			if(i % 3 == 0){
				form.setGroup("A");
			}else if(i % 3 == 1){
				form.setGroup("B");
			}else{
				form.setGroup("C");
			}
			orderList.add(form);
		}
		
		for(OrderForm form1 : orderList){
			System.out.println("排序之前的顺序" + form1.getId());
		}
		
		return orderList;
	}
}
package exec;

import java.util.Random;
//form实体类
public class OrderForm {
	
	private Long id;
	private String name;
	private Random score;
	private String group;
	
	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Random getScore() {
		return score;
	}
	public void setScore(Random random) {
		this.score = random;
	}
	public String getGroup() {
		return group;
	}
	public void setGroup(String group) {
		this.group = group;
	}
}

     运行过程发现问题:

form.setId(params.get(3).getId());
int index = params.indexOf(form);

      如上,这句代码,只通过form赋值id,之后在List<Form>中匹配,index每次都是"-1",发现当List中的内容是Object时,要通过indexOf()匹配,发现form的每一个属性值都得匹配上才行。

      之后换了一个思路:

private List<BwAutoLoanEmpDailyForm> turnRoundEmpList(List<BwAutoLoanEmpDailyForm> orginEmpList, Long empId) {
	List<Long> empIds = new ArrayList<Long>();
	for(BwAutoLoanEmpDailyForm form : orginEmpList){
		empIds.add(form.getDailyEmpId());
	}
	int index = empIds.indexOf(empIds) + 1;
}
      通过把List中所有Object的id扒出来,构成一个新的List,然后找到索引的位置,可以解决上述这个问题。

      但是,这种做法还不够好,“空闲”和“时间”两个维度的复杂度全部增加了,在同事指点下,看了下这个indexOf()的源码,如下:

      1.List.java中的indexOf()方法

public interface List<E> extends Collection<E> {
	/**
	 * Returns the index of the first occurrence of the specified element
	 * in this list, or -1 if this list does not contain the element.
	 * More formally, returns the lowest index <tt>i</tt> such that
	 * <tt>(o==null ? get(i)==null : o.equals(get(i)))</tt>,
	 * or -1 if there is no such index.
	 *
	 * @param o element to search for
	 * @return the index of the first occurrence of the specified element in
	 *         this list, or -1 if this list does not contain the element
	 * @throws ClassCastException if the type of the specified element
	 *         is incompatible with this list
	 *         (<a href="Collection.html#optional-restrictions">optional</a>)
	 * @throws NullPointerException if the specified element is null and this
	 *         list does not permit null elements
	 *         (<a href="Collection.html#optional-restrictions">optional</a>)
	 */
    int indexOf(Object o);

}

     2.ArrayList中的indexOf()

/**
 * Returns the index of the first occurrence of the specified element
 * in this list, or -1 if this list does not contain the element.
 * More formally, returns the lowest index <tt>i</tt> such that
 * <tt>(o==null ? get(i)==null : o.equals(get(i)))</tt>,
 * or -1 if there is no such index.
 */
public int indexOf(Object o) {
	if (o == null) {
		for (int i = 0; i < size; i++)
			if (elementData[i]==null)
				return i;
	} else {
		for (int i = 0; i < size; i++)
			if (o.equals(elementData[i]))
				return i;
	}
	return -1;
}

     3.LinkedList.java中的indexOf()

/**
 * Returns the index of the first occurrence of the specified element
 * in this list, or -1 if this list does not contain the element.
 * More formally, returns the lowest index {@code i} such that
 * <tt>(o==null ? get(i)==null : o.equals(get(i)))</tt>,
 * or -1 if there is no such index.
 *
 * @param o element to search for
 * @return the index of the first occurrence of the specified element in
 *         this list, or -1 if this list does not contain the element
 */
public int indexOf(Object o) {
	int index = 0;
	if (o == null) {
		for (Node<E> x = first; x != null; x = x.next) {
			if (x.item == null)
				return index;
			index++;
		}
	} else {
		for (Node<E> x = first; x != null; x = x.next) {
			if (o.equals(x.item))
				return index;
			index++;
		}
	}
	return -1;
}

    发现对List接口的实现,无论是“ArrayList”还是“LinkedList”,匹配的根本都是equals()方法,于是乎,对例子中“OrderForm”的equals方法进行重写:

package exec;

import java.util.Random;

public class OrderForm {
	
	private Long id;
	private String name;
	private Random score;
	private String group;
	
	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Random getScore() {
		return score;
	}
	public void setScore(Random random) {
		this.score = random;
	}
	public String getGroup() {
		return group;
	}
	public void setGroup(String group) {
		this.group = group;
	}
	
	@Override
	public boolean equals(Object obj) {
		if (obj == null) {
			return false ;
		}
		if (obj instanceof OrderForm){
			OrderForm bwAutoLoanEmpDailyForm = (OrderForm) obj;
			if(bwAutoLoanEmpDailyForm.id.longValue() == this.id.longValue()){
				return true ;
			}
		}
		return false ;
	}
}

    此时,撤销掉“code_piece_2”,完美解决这个问题。


Addition:
     1.重写equals()方法的时候,对于两个Long类型的数值进行相等判断,切记直接使用“==”,可以通过.longValue()形式来解决,当然"=="在java中有很多坑,究竟哪些数据类型不能用“==”来判断,期待以后的总结。
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值