最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

java中List對象列表實(shí)現(xiàn)去重或取出及排序的方法

 更新時(shí)間:2017年08月07日 08:31:56   作者:Ryan.Miao  
這篇文章主要介紹了關(guān)于java中List對象列表實(shí)現(xiàn)去重或取出以及排序的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面跟著小編來一起學(xué)習(xí)學(xué)習(xí)吧。

前言

因?yàn)樵诿嬖嚨臅r(shí)候碰到幾次list的去重和排序,覺著有必要給大家總結(jié)一下具體的方法,分享出來供大家學(xué)習(xí)參考,話不多說了,來一起看看下面介紹的一種做法:

一、list去重

1.1 實(shí)體類Student

List<Student>容量10k以上,要求去重復(fù)。這里Student的重復(fù)標(biāo)準(zhǔn)是屬性相同,因此需要重寫equals和hashcode方法,不知道有幾個(gè)可以手寫出來。

student的equals方法:

public void equals(Object o){
 if(this == o) retun true;
 if(!(o instanceof Student)) return false;
 Student stu = (Studend)o;
 if(id!=stu.id) return false;
 if(age!=stu.age) return false;
 return name!=null ? name.equals(stu.name) : stu.name ==null; 
}

這里只要記住宗旨是比較Student的屬性即可,如果屬性相同則相等。先考慮地址相等,然后類型匹配instanceof。接下來是各種屬性,int屬性直接雙等號比較,String類型需要判斷是否為null,如果是null則都是null返回true,如果不是null則比較equals。

student的hashcode方法:

public int hashCode(){
 int result = id;
 reuslt = 31*id +(name!=null?name.hashCode():0);
 reuslt = 31*age;
 return reuslt;
}

hashCode是為了hash表計(jì)算做輔助,方便快速查找。因此hash算法的結(jié)果要盡量的散列。這里用到31,這個(gè)31在別的博客中看到的原因是這樣的: obj*31==obj<<5-obj.左移5位相當(dāng)乘以2的5次方,就是32.null的hashCode為空。

通過equals和hashCode的實(shí)現(xiàn)可以發(fā)現(xiàn),如果equals為true,則所有屬性相同,而屬性相同則計(jì)算出的hashCode必然相同。然而hashCode相同,屬性未必一樣,即equals不一定為真。

關(guān)于hashCode的價(jià)值體現(xiàn)并不在這里,而在于HashMap的實(shí)現(xiàn)。HashMap內(nèi)部是通過鏈表數(shù)組的hash結(jié)構(gòu)來實(shí)現(xiàn)的,這里就要用到hashcode。

下面是完整的Student代碼:

package com.test.arithmetic.listequals;

/**
 * 這里id,name,age相同則Student相同,
 * 若有其他相同
 * Created by Administrator on 2016/3/29.
 */
public class Student {
 int id;
 String name;
 int age;

 public Student(int id, String name, int age) {
  this.id = id;
  this.name = name;
  this.age = age;
 }

 @Override
 public boolean equals(Object o) {
  if (this == o) return true;
  if (!(o instanceof Student)) return false;

  Student student = (Student) o;

  if (id != student.id) return false;
  if (age != student.age) return false;
  return name != null ? name.equals(student.name) : student.name == null;

 }

 @Override
 public int hashCode() {
  int result = id;
  result = 31 * result + (name != null ? name.hashCode() : 0);
  result = 31 * result + age;
  return result;
 }

}

1.2通過HashSet去重

如果你覺得自己可以hold住一個(gè)完善的hash算法就可以自己去實(shí)現(xiàn)它。這里采用jdk自帶的HashSet來完成重復(fù)獲取。

先放代碼:

package com.test.arithmetic.listequals;
import org.junit.Assert;

import java.util.*;

/**
 * 取出list中重復(fù)的Student對象
 * Created by Administrator on 2016/3/29.
 */
public class ObtainListEquals {
 public static void main(String[] args){
  //原始數(shù)據(jù)
  List<Student> list = new ArrayList<>();
  //重復(fù)數(shù)據(jù)
  List<Student> list2 = new ArrayList<>();
  //填充
  for (int i = 0; i < 10 ; i++) {
   list.add(new Student(i,"_"+i,18+i));
   Random random = new Random();
   if (random.nextBoolean()){
    list.add(new Student(i,"_"+i,18+i));
   }
  }
  //使用hashset去重復(fù),set為重復(fù)的集合,可以通過new ArrayList(set)轉(zhuǎn)換成list
  HashSet<Student> set = new HashSet<>();
  for (Student student : list) {
   boolean add = set.add(student);
   if (!add){
    list2.add(student);
   }
  }
  //比較
  Assert.assertEquals(list.size(),list2.size()+set.size());
  
 }

}

去重的原理和簡單,無論你僅僅是想把重復(fù)的丟掉,或者將重復(fù)的取出來。這里去掉的是第二次遇到的對象,取出的也是第二次遇到的對象。HashSet中的add方法會返回一個(gè)Boolean值,如果插入的值已經(jīng)存在,則直接返回false。關(guān)于hashset的源碼放到以后研究。大概的說,是通過HashMap的key來實(shí)現(xiàn)的,而HashMap在1.8中改動很大,據(jù)說是用紅黑樹實(shí)現(xiàn)的,提高了get的時(shí)間復(fù)雜度。

二、list對象排序

同樣list中存放的是Student對象,我需要一個(gè)規(guī)則來排序。這個(gè)排序的規(guī)則這里定義為id的比較大小。參考:java中l(wèi)ist排序

2.1 Student對象實(shí)現(xiàn)Comparable接口

Comparable接口提供一個(gè)比較的compareTo(Object o)方法,通過返回值>0,=0,<0比較大小。這里由于僅僅把id當(dāng)做比較大小的方法,直接用id做減法,如果是要比較對象,建議套用this.property.compareTo(o.property) .

package com.test.arithmetic.listequals;

/**
 * 這里id,name,age相同則Student相同,
 * 若有其他相同
 * Created by Administrator on 2016/3/29.
 */
public class Student implements Comparable<Student>{
  int id;
  String name;
  int age;

  public Student(int id, String name, int age) {
    this.id = id;
    this.name = name;
    this.age = age;
  }

  @Override
  public boolean equals(Object o) {
    if (this == o) return true;
    if (!(o instanceof Student)) return false;

    Student student = (Student) o;

    if (id != student.id) return false;
    if (age != student.age) return false;
    return name != null ? name.equals(student.name) : student.name == null;

  }

  @Override
  public int hashCode() {
    int result = id;
    result = 31 * result + (name != null ? name.hashCode() : 0);
    result = 31 * result + age;
    return result;
  }

  @Override
  public int compareTo(Student o) {
    return this.id-o.id;
  }
}

通過Collections.sort(list)排序:

package com.test.arithmetic.list.sort;

import com.test.arithmetic.list.Student;
import org.junit.Before;
import org.junit.Test;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 對list中對象排序
 * Created by Administrator on 2016/3/29.
 */
public class SortList {
  List<Student> list;
  @Before
  public void setUp(){
    list = new ArrayList<>();
    for (int i = 0; i < 10; i++) {
      int v = (int)(Math.random() * 100);
      list.add(new Student(v,"_"+v,18+v));
    }
    System.out.println("原list:"+list);
  }
  //方法一,對象實(shí)現(xiàn)Comparable接口
  @Test
  public void byImplements(){
    Collections.sort(list);
    System.out.println("排序后:"+list);
  }
}

2.2 重載sort方法,傳入一個(gè)比較器

Student類還是未實(shí)現(xiàn)Comparable接口之前的:

package com.test.arithmetic.list;

/**
 * 這里id,name,age相同則Student相同,
 * 若有其他相同
 * Created by Administrator on 2016/3/29.
 */
public class Student{
  int id;
  String name;
  int age;

  public Student(int id, String name, int age) {
    this.id = id;
    this.name = name;
    this.age = age;
  }

  public int getId() {
    return id;
  }

  public Student(int id) {
    this.id = id;
  }

  @Override
  public boolean equals(Object o) {
    if (this == o) return true;
    if (!(o instanceof Student)) return false;

    Student student = (Student) o;

    if (id != student.id) return false;
    if (age != student.age) return false;
    return name != null ? name.equals(student.name) : student.name == null;

  }

  @Override
  public int hashCode() {
    int result = id;
    result = 31 * result + (name != null ? name.hashCode() : 0);
    result = 31 * result + age;
    return result;
  }

  @Override
  public String toString() {
    return "Student{" +
        "id=" + id +
        ", name='" + name + '\'' +
        ", age=" + age +
        '}';
  }
}

在排序的代碼出添加排序規(guī)則:

package com.test.arithmetic.list.sort;

import com.test.arithmetic.list.Student;
import org.junit.Before;
import org.junit.Test;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * 對list中對象排序
 * Created by Administrator on 2016/3/29.
 */
public class SortList {
  List<Student> list;
  @Before
  public void setUp(){
    list = new ArrayList<>();
    for (int i = 0; i < 10; i++) {
      int v = (int)(Math.random() * 100);
      list.add(new Student(v,"_"+v,18+v));
    }
    System.out.println("原list:"+list);
  }
  //方法一,對象實(shí)現(xiàn)Comparable接口
  @Test
  public void byImplements(){
//    Collections.sort(list);
    System.out.println("排序后:"+list);
  }

  /*方法二,添加比較器*/
  @Test
  public void byOverideCompare(){

    Collections.sort(list, new Comparator<Student>() {
      @Override
      public int compare(Student o1, Student o2) {
        return o1.getId()-o2.getId();
      }
    });
    System.out.println(list);
  }
}

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。

相關(guān)文章

  • SpringBoot實(shí)現(xiàn)devtools實(shí)現(xiàn)熱部署過程解析

    SpringBoot實(shí)現(xiàn)devtools實(shí)現(xiàn)熱部署過程解析

    這篇文章主要介紹了SpringBoot實(shí)現(xiàn)devtools實(shí)現(xiàn)熱部署過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-03-03
  • k8s部署的java服務(wù)添加idea調(diào)試參數(shù)的方法

    k8s部署的java服務(wù)添加idea調(diào)試參數(shù)的方法

    文章介紹了如何在K8S容器中的Java服務(wù)上進(jìn)行遠(yuǎn)程調(diào)試,包括配置Deployment、Service以及本地IDEA的調(diào)試設(shè)置,感興趣的朋友跟隨小編一起看看吧
    2025-02-02
  • java實(shí)現(xiàn)的冒泡排序算法示例

    java實(shí)現(xiàn)的冒泡排序算法示例

    這篇文章主要介紹了java實(shí)現(xiàn)的冒泡排序算法,結(jié)合實(shí)例形式分析了冒泡排序算法的具體操作步驟與實(shí)現(xiàn)技巧,需要的朋友可以參考下
    2017-01-01
  • mybatis+lombok出現(xiàn)java.lang.IndexOutOfBoundsException錯誤及解決

    mybatis+lombok出現(xiàn)java.lang.IndexOutOfBoundsException錯誤及解決

    在使用MyBatis和Lombok時(shí),如果遇到j(luò)ava.lang.IndexOutOfBoundsException問題,是因?yàn)镸yBatis在嘗試將查詢結(jié)果封裝成Java對象時(shí),找不到構(gòu)造函數(shù)中對應(yīng)的字段,這通常是由于Lombok的@Builder注解生成了全參構(gòu)造函數(shù)
    2025-02-02
  • springboot如何實(shí)現(xiàn)導(dǎo)入其他配置類

    springboot如何實(shí)現(xiàn)導(dǎo)入其他配置類

    這篇文章主要介紹了springboot如何實(shí)現(xiàn)導(dǎo)入其他配置類問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-11-11
  • 基于Spring Boot使用JpaRepository刪除數(shù)據(jù)時(shí)的注意事項(xiàng)

    基于Spring Boot使用JpaRepository刪除數(shù)據(jù)時(shí)的注意事項(xiàng)

    這篇文章主要介紹了Spring Boot使用JpaRepository刪除數(shù)據(jù)時(shí)的注意事項(xiàng),具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-06-06
  • Spring Boot非Web項(xiàng)目運(yùn)行配置的方法教程

    Spring Boot非Web項(xiàng)目運(yùn)行配置的方法教程

    這篇文章主要介紹了Spring Boot非Web項(xiàng)目運(yùn)行配置的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用Spring Boot具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-09-09
  • Java中finally和return的關(guān)系實(shí)例解析

    Java中finally和return的關(guān)系實(shí)例解析

    這篇文章主要介紹了Java中finally和return的關(guān)系實(shí)例解析,總結(jié)了二者的關(guān)系,然后分享了相關(guān)代碼示例,小編覺得還是挺不錯的,具有一定借鑒價(jià)值,需要的朋友可以參考下
    2018-02-02
  • java中ArrayList和LinkedList的區(qū)別詳解

    java中ArrayList和LinkedList的區(qū)別詳解

    這篇文章主要介紹了java中ArrayList和LinkedList的區(qū)別詳解,幫助大家更好的理解和使用Java,感興趣的朋友可以了解下
    2021-01-01
  • 聊聊關(guān)于Java方法重寫的反思

    聊聊關(guān)于Java方法重寫的反思

    最近在開發(fā)中遇到一個(gè)關(guān)于Java方法重寫的一些問題,對于方法重寫的用法以及可能導(dǎo)致的問題產(chǎn)生了一些思考,本文用于記錄下這些想法,希望對大家也有所幫助
    2023-05-05

最新評論

阿鲁科尔沁旗| 原阳县| 开原市| 南华县| 福海县| 来凤县| 夏津县| 宁陵县| 靖远县| 呼伦贝尔市| 衢州市| 类乌齐县| 北流市| 北流市| 广灵县| 商城县| 乌鲁木齐市| 佛山市| 辉南县| 杭州市| 龙岩市| 通海县| 固阳县| 通榆县| 旌德县| 德兴市| 汝阳县| 新密市| 钟山县| 顺义区| 金阳县| 如皋市| 井陉县| 山阳县| 修文县| 绥滨县| 盘锦市| 安多县| 手游| 乌鲁木齐市| 九寨沟县|