zoukankan      html  css  js  c++  java
  • java集合中寻找指定对象的集合或对象数据

    package com.xiniunet.web.tool;

    import java.lang.reflect.InvocationTargetException;
    import java.lang.reflect.Method;
    import java.util.ArrayList;
    import java.util.HashSet;
    import java.util.List;
    import java.util.Set;

    /**
    * Created by xn on 15/7/22.
    */
    public class ListUtil {

    /**
    * 判断集合中是否存在该对象的数据
    * @param list
    * @param value
    * @param <T>
    * @return 返回在集合中首次匹配到的对象数据
    */
    public static <T> T matchObject(List<T> list,T value) {
    if(list == null || list.size() == 0 || value == null){
    return null;
    }
    for(T t : list) {
    if(value.equals(t)){
    return t;
    }
    }
    return null;
    }

    /**
    * 判断集合中是否存在该长整型数值的数据对象
    * @param list
    * @param id
    * @param <T>
    * @return 返回首次符合长整型数值的数据对象
    */
    public static <T> T matchObject(List<T> list,Long id) {
    if(list == null || list.size() == 0 || id == null){
    return null;
    }
    Class clazz = list.get(0).getClass();
    Method method = null;
    try {
    method = clazz.getMethod("getId");
    } catch (NoSuchMethodException e) {
    e.printStackTrace();
    }
    for(T t : list) {
    try {
    if(id.equals(method.invoke(t))){
    return t;
    }
    } catch (IllegalAccessException | InvocationTargetException e) {
    e.printStackTrace();
    }
    }
    return null;
    }

    /**
    * 判断集合中是否存在该长整型数值的数据对象
    * @param list
    * @param id
    * @param <T>
    * @return 返回符合长整型数值的数据集合对象
    */
    public static <T> List<T> matchList(List<T> list,Long id) {

    List<T> result = new ArrayList();
    if(list == null || list.size() == 0 || id == null){
    return result;
    }
    Class clazz = list.get(0).getClass();
    Method method = null;
    try {
    method = clazz.getMethod("getId");
    } catch (NoSuchMethodException e) {
    e.printStackTrace();
    }

    for(T t : list) {
    try {
    if(id.equals(method.invoke(t))){
    result.add(t);
    }
    } catch (IllegalAccessException | InvocationTargetException e) {
    e.printStackTrace();
    }
    }
    return result;
    }

    /**
    * 判断集合中是否存在该长整型数据集合的集合数据
    * @param list
    * @param ids
    * @param <T>
    * @return 返回符合长整型数值的数据集合对象
    */
    public static <T> List<T> matchList(List<T> list,List<Long> ids) {

    List<T> result = new ArrayList();
    if(list == null || list.size() == 0 || ids == null || ids.size() == 0){
    return result;
    }
    Class clazz = list.get(0).getClass();
    Method method = null;
    try {
    method = clazz.getMethod("getId");
    } catch (NoSuchMethodException e) {
    e.printStackTrace();
    }

    for(Long id : ids){
    for(T t : list) {
    try {
    if(id.equals(method.invoke(t))){
    result.add(t);
    }
    } catch (IllegalAccessException | InvocationTargetException e) {
    e.printStackTrace();
    }
    }
    }

    return result;
    }

    /**
    * 循环判断集合中是否存在长整型数据集合中的数据(每个对象只匹配第一次取到的值)
    * @param list
    * @param ids
    * @param <T>
    * @return 返回符合长整型数据集合中的集合对象(每个对象只匹配第一次取到的值)
    */
    public static <T> List<T> matchBatchObject(List<T> list,List<Long> ids) {

    List<T> result = new ArrayList();
    if(list == null || list.size() == 0 || ids == null || ids.size() == 0){
    return result;
    }
    Class clazz = list.get(0).getClass();
    Method method = null;
    try {
    method = clazz.getMethod("getId");
    } catch (NoSuchMethodException e) {
    e.printStackTrace();
    }
    for(Long id :ids){
    for(T t : list) {
    try {
    if(id.equals(method.invoke(t))){
    result.add(t);
    break;
    }
    } catch (IllegalAccessException | InvocationTargetException e) {
    e.printStackTrace();
    }
    }
    }
    return result;
    }

    /**
    * 判断集合中是否存在该对象相匹配的对象数据
    * @param list
    * @param fieldName
    * @param id
    * @param <T>
    * @param <K>
    * @return 返回符合对象相匹配的对象数据
    */
    public static <T,K> T matchObject(List<T> list,String fieldName,K id) {
    if(list == null || list.size() == 0 || id == null){
    return null;
    }
    Class clazz = list.get(0).getClass();
    Method method = null;
    try {
    method = clazz.getMethod("get" + Character.isUpperCase(fieldName.charAt(0)));
    } catch (NoSuchMethodException e) {
    e.printStackTrace();
    }
    for(T t : list) {
    try {
    if(id.equals(method.invoke(t))){
    return t;
    }
    } catch (IllegalAccessException | InvocationTargetException e) {
    e.printStackTrace();
    }
    }
    return null;
    }

    /**
    * 判断集合中是否存在该对象相匹配的对象数据
    * @param list
    * @param fieldName
    * @param id
    * @param <T>
    * @param <K>
    * @return 返回符合对象相匹配的集合对象
    */
    public static <T,K> List<T> matchList(List<T> list,String fieldName,K id) {

    List<T> result = new ArrayList();
    if(list == null || list.size() == 0 || id == null){
    return result;
    }
    Class clazz = list.get(0).getClass();
    Method method = null;
    try {
    method = clazz.getMethod("get" + Character.isUpperCase(fieldName.charAt(0)));
    } catch (NoSuchMethodException e) {
    e.printStackTrace();
    }

    for(T t : list) {
    try {
    if(id.equals(method.invoke(t))){
    result.add(t);
    }
    } catch (IllegalAccessException | InvocationTargetException e) {
    e.printStackTrace();
    }
    }
    return result;
    }

    /**
    * 循环判断集合中是否存在该对象相匹配的对象数据
    * @param list
    * @param fieldName
    * @param ids
    * @param <T>
    * @param <K>
    * @return 返回符合对象相匹配的集合对象
    */
    public static <T,K> List<T> matchList(List<T> list,String fieldName,List<K> ids) {

    List<T> result = new ArrayList();
    if(list == null || list.size() == 0 || ids == null || ids.size() == 0){
    return result;
    }
    Class clazz = list.get(0).getClass();
    Method method = null;
    try {
    method = clazz.getMethod("get" + Character.isUpperCase(fieldName.charAt(0)));
    } catch (NoSuchMethodException e) {
    e.printStackTrace();
    }

    for(K id : ids){
    for(T t : list) {
    try {
    if(id.equals(method.invoke(t))){
    result.add(t);
    }
    } catch (IllegalAccessException | InvocationTargetException e) {
    e.printStackTrace();
    }
    }
    }
    return result;
    }

    /**
    * 判断集合中是否存在该对象相匹配的对象数据(每个对象只匹配第一次取到的值)
    * @param list
    * @param fieldName
    * @param ids
    * @param <T>
    * @param <K>
    * @return 返回符合对象相匹配的集合对象(每个对象只匹配第一次取到的值)
    */
    public static <T,K> List<T> matchBatchObject(List<T> list,String fieldName,List<K> ids) {

    List<T> result = new ArrayList();
    if(list == null || list.size() == 0 || ids == null || ids.size() == 0){
    return result;
    }
    Class clazz = list.get(0).getClass();
    Method method = null;
    try {
    method = clazz.getMethod("get" + Character.isUpperCase(fieldName.charAt(0)));
    } catch (NoSuchMethodException e) {
    e.printStackTrace();
    }
    for(K id :ids){
    for(T t : list) {
    try {
    if(id.equals(method.invoke(t))){
    result.add(t);
    break;
    }
    } catch (IllegalAccessException | InvocationTargetException e) {
    e.printStackTrace();
    }
    }
    }
    return result;
    }

    public static <T> List<Long> getKeyList(List<T> list) {

    List<Long> result = new ArrayList();
    if(list == null || list.size() == 0){
    return result;
    }
    Class clazz = list.get(0).getClass();
    Method method = null;
    try {
    method = clazz.getMethod("getId");
    } catch (NoSuchMethodException e) {
    e.printStackTrace();
    }

    Set<Long> set = new HashSet<>();
    for(T t : list) {
    try {
    Long id = (Long)method.invoke(t);
    if(id != null){
    set.add(id);
    }
    } catch (IllegalAccessException | InvocationTargetException e) {
    e.printStackTrace();
    }
    }
    for(Long id : set){
    result.add(id);
    }
    return result;
    }

    public static <T,K> List<K> getKeyList(List<T> list,String fieldName) {

    List<K> result = new ArrayList();
    if(list == null || list.size() == 0){
    return result;
    }
    Class clazz = list.get(0).getClass();
    Method method = null;
    try {
    method = clazz.getMethod("get" + Character.isUpperCase(fieldName.charAt(0)));
    } catch (NoSuchMethodException e) {
    e.printStackTrace();
    }
    Set<K> set = new HashSet<>();

    for(T t : list) {
    try {
    K k = (K)method.invoke(t);
    if(k != null){
    set.add(k);
    }
    } catch (IllegalAccessException | InvocationTargetException e) {
    e.printStackTrace();
    }
    }
    for(K k : set){
    result.add(k);
    }
    return result;
    }
    }
  • 相关阅读:
    hdu 3342 Legal or Not 拓排序
    hdu 1596 find the safest road Dijkstra
    hdu 1874 畅通工程续 Dijkstra
    poj 2676 sudoku dfs
    poj 2251 BFS
    poj Prime Path BFS
    poj 3278 BFS
    poj 2387 Dijkstra 模板
    poj 3083 DFS 和BFS
    poj 1062 昂贵的聘礼 dijkstra
  • 原文地址:https://www.cnblogs.com/Dream-Lasting/p/10167408.html
Copyright © 2011-2022 走看看