-
2020-04-09 14:51:15
比如要拿到List里名字叫张三的这个学生的身高,除了普通for循环还有其他办法吗?跪求
更多相关内容 -
快速解决List集合add元素,添加多个对象出现重复的问题
2020-08-24 20:51:02主要介绍了快速解决List集合add元素,添加多个对象出现重复的问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧 -
java 获取List集合中对象指定字段值出现最多的值
2021-03-15 10:14:10记录:已有对象集合List<Employee> ,需要获取Employee的字段 name出现最多的对象(即根据name字段分组->排序->取第一个对象); public static void main(String[] args) { List<Employee> list = ...记录:已有对象集合List<Employee> ,需要获取Employee的字段 name出现最多的字段值(即根据name字段分组->排序->取值);
public static void main(String[] args) { List<Employee> list = new ArrayList(){{ add(new Employee(10,"李二")); add(new Employee(18,"王五")); add(new Employee(11, "赵四")); add(new Employee(21,"王五")); }}; String name = list.stream() //判定字段非空 .filter(r -> Objects.nonNull(r.getName()) && !r.getName().isEmpty()) .collect(Collectors.groupingBy(Employee::getName)) .values() .stream() .sorted((a, b) -> b.size() - a.size()) //实际需要先判定结果集合非空,再获取集合元素 .collect(Collectors.toList()).get(0).get(0).getName(); }
注意! 注意!注意!
1.需要对指定指端做非空判定;
2.排序集合收集后需要对集合进行非空判定;
-
java集合中寻找指定对象的集合或对象数据
2021-03-04 04:39:04package ...import java.lang.reflect.InvocationTargetException;import java.lang.reflect.Method;import java.util.ArrayList;import java.util.HashSet;import java.util.List;import java.u...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
* @return 返回在集合中首次匹配到的对象数据
*/
public static T matchObject(List 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
* @return 返回首次符合长整型数值的数据对象
*/
public static T matchObject(List 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
* @return 返回符合长整型数值的数据集合对象
*/
public static List matchList(List list,Long id) {
List 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
* @return 返回符合长整型数值的数据集合对象
*/
public static List matchList(List list,List ids) {
List 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
* @return 返回符合长整型数据集合中的集合对象(每个对象只匹配第一次取到的值)
*/
public static List matchBatchObject(List list,List ids) {
List 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
* @param
* @return 返回符合对象相匹配的对象数据
*/
public static T matchObject(List 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
* @param
* @return 返回符合对象相匹配的集合对象
*/
public static List matchList(List list,String fieldName,K id) {
List 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
* @param
* @return 返回符合对象相匹配的集合对象
*/
public static List matchList(List list,String fieldName,List ids) {
List 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
* @param
* @return 返回符合对象相匹配的集合对象(每个对象只匹配第一次取到的值)
*/
public static List matchBatchObject(List list,String fieldName,List ids) {
List 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 List getKeyList(List list) {
List 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 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 List getKeyList(List list,String fieldName) {
List 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 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;
}
}
-
js解析json读取List中的实体对象示例
2020-12-12 18:08:19List中存放多个student对象 2、前台js 中先将结果json串转成对象 代码如下: var obj = eval(“(“+data+”)”); 3、遍历取list 中的对象 代码如下: for(var key in obj){ //第一层循环取到各个list ... -
C# 获取List集合中指定几行的数据
2019-11-29 14:52:46最近在做一个项目需要获取list集合中指定几行的数据,如何快速获取。 #region 构造方法(Start) /// <summary> /// 构造方法 /// </summary> public class MonthClass { public string Id { ...最近在做一个项目需要获取list集合中指定几行的数据,如何快速获取。
#region 构造方法(Start) /// <summary> /// 构造方法 /// </summary> public class MonthClass { public string Id { get; set; } public string Text { get; set; } } #endregion 构造方法(End) List<MonthClass> list=new List<MonthClass> (); MonthClass model=new MonthClass(); model.Id ="1"; model.Text ="1"; list.add(model) MonthClass model1=new MonthClass(); model1.Id ="2"; model1.Text ="2"; list.add(model1) MonthClass model2=new MonthClass(); model2.Id ="3"; model2.Text ="3"; list.add(model2) MonthClass model=new MonthClass(); model3.Id ="4"; model3.Text ="4"; list.add(model3)
上面构造了一个集合:list.count=4
解决问题:想要获取list集合中的下标为1至3的数据也就是(list[1]、list[2]、list[3])
List<MonthClass> list2 = list.GetRange(1, 3);
如上就解决了取值的问题。
解释:
// // 摘要: // 创建源 System.Collections.Generic.List`1 中的元素范围的浅表副本。 // // 参数: // index: // 范围开始处的从零开始的 System.Collections.Generic.List`1 索引。 // // count: // 范围中的元素数。 // // 返回结果: // 源 System.Collections.Generic.List`1 中的元素范围的浅表副本。 // // 异常: // T:System.ArgumentOutOfRangeException: // index 小于 0。- 或 -count 小于 0。 // // T:System.ArgumentException: // index 和 count 不表示 System.Collections.Generic.List`1 中元素的有效范围。 public List<T> GetRange(int index, int count);
通俗的讲:public List<T> GetRange(int index, int count);
index:表示获取list集合中的第几个数据。(说明list集合是从0开始的)
count:是需要往后面取几个数据(包括开始的那个数据)
GetRange(5, 5);
说明:获取list集合下标是5的数据,往后面在获取5个数据
结果(list[5]、list[6]、list[7]、list[8]、list[9])
GetRange(7, 8);
说明:获取list集合下标是5的数据,往后面在获取5个数据
结果(list[7]、list[8]、list[9]、list[10]、list[11]、list[12]、list[13]、list[14])
这样的解释还是可以的吧!!!!!!
C#中List集合使用GetRange方法获取指定索引范围内的所有值
在C#的List集合中有时候需要获取指定索引位置范围的元素对象来组成一个新的List集合,此时就可使用到List集合的扩展方法GetRange方法,GetRange方法专门用于获取List集合指定范围内的所有值,GetRange方法签名为List<T> GetRange(int index, int count),index为开始索引位置,count为从index开始获取元素的个数。
例如有个List<int>的集合list1,内部存储10个数字,需要获取list1集合从第5个数开始,连续的5个元素组成新的集合,可使用下列语句:
List<int> list1 = new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; var resultList= list1.GetRange(4, 5);
计算结果为,resultList集合的元素为5,6,7,8,9
-
获取list对象中的指定字段
2022-03-25 10:49:11List<Long> userId= scheduleStudents.stream().map(User::getUserId).collect(Collectors.toList()); List<Long> ids = list.stream().map(u-> u.getUserId()).collect(Collectors.toList()); -
将 List 对象转为对象中指定属性的 List 集合
2019-05-28 18:37:59将 List 对象转为对象中指定属性的 List 集合 -
获取List集合中,指定下标之间的数据
2021-03-17 15:08:14获取获取List集合中,指定下标之间的数据。 代码实现 测试:在list中添加数据0-10;获取下标为2和8之间的数据 @Test public void test() { List<Integer> list = new ArrayList<>(); for (int i = 0; ... -
java 【 获取List集合对象中某一列属性值 】
2022-01-27 13:33:19【参考:】 ...【解决:】 @GetMapping("/getranking") public String ... List<Users> one = usersRepository.findAll(); //总积分排序,使用默认排序(降序),已经在Users实体类中定义 Collections.s -
C# list常用的几个操作 改变list中某个元素的值 替换某一段数据 删除集合中指定对象
2020-02-15 09:46:481、改变list中某个元素的值 public class tb_SensorRecordModel { public int ID { get; set; } public decimal Value1 { get; set; } } List<tb_SensorRecordModel> list = new... -
list集合中指定字段去重
2021-02-27 09:57:14在开发中,有时会需要指定字段去重,以下为实现方法:假设有个房地产权的类,其中宗地代码ZDDM值重复,而我们在前端页面显示...//集合指定字段去重(宗地代码)List resultFDCQ = new List();var resultdis=resultFDCQ.... -
获取list对象集合的某一属性的list集合
2020-07-06 11:06:28//从所有学生集合中获取名字集合 List<String> supplierCodes = students.stream().map(Student :: getName).collect(Collectors.toList()); students:所有学生的对象集合 getName:name属性 ... -
关于反射取list集合中指定对象属性(直接list.get(i).getDeclareField....)
2019-12-23 16:14:351、首先,我们这里的list类型是动态的;List<BdDefDoc/Bd_billtype/so_saleOrder>list,里面这个类型对象是三个甚至是...但是我们这里为了实现动态调用,把List集合写成类型,所以我们需要反射调用; 3、跟反射... -
list集合获取指定个数
2020-09-16 09:53:50list.subList(startIndex,endIndex) 左闭右开 -
【笔记】从List中取出指定字段等于指定值的对象
2021-10-18 17:56:55例如我们有一个Student集合,每个对象有“id”,“name”,“age”三个字段,如下: @Data @AllArgsConstructor public class Student { private Integer id; private String name; private Integer age; } ... -
list stream:过滤对象集合中对象字段满足条件的对象集合
2019-03-18 12:26:00假如我们有一个User对象,...如果我们存在这样的一个List列表,如果我们希望找出这个List中name为ONE的对象,当然我们可以通过for循环来逐个匹配,然后再放入到另外一个List中,这里我们来展示一种Java1.8中为我们提... -
4种方式获取List中指定元素
2021-11-09 11:22:36import java.util.ArrayList; import java.util.Iterator;...import java.util.List; public class Test4 { public static void main(String[] args) { // List<String> resultList = new LinkedList<> -
vue获取list集合中的特定下标的方法
2021-02-08 12:03:20<h6 class="category" v-for="(category,index) in article.categories" v-if="index==1">...而这个((article.categories)[0])就是通过下标获取的对象 为什么不能直接点获取,还要通过for循环,因为vue没 -
js获取指定list集合数据中的字段 转换为map数据
2022-02-16 16:20:13let map = {}, arr = res.data.results; for (let i = 0; i ; i++) { let ai = arr[i]; if (!map[ai.docSort]) { map[ai.docSort] = ...= 'null'){ //存初始化数据 list.push({ sort: key, data: map[key], }); } }); -
删除list集合中特定元素的正确姿势
2021-01-18 14:16:03如何删除一个集合对象中的特定元素?小问题,但并不简单。 常见异常: ConcurrentModificationException java.util.ConcurrentModificationException at java.util.ArrayList$Itr.checkForComodification(ArrayList... -
Java中的List集合
2021-03-10 04:32:31集合概述为了在程序中保存数目不确定的对象,JDK中提供了一系列的特殊类,这些类可以存储任意类型的对象,并且长度可变,在Java中这些类被统称为集合。集合类都位于java.util包中。集合按照其存储类型分为两大类,即... -
List集合中对象根据某个字段进行排序或者按中文首字母排序
2021-02-24 17:30:10List集合存入的多个对象,要使其对象中根据指定的字段进行升序或者降序。将使用 Collections.sort(); 方法。 首先看Collections的常用方法: package com.company; import java.util.ArrayList; import java.util.... -
java 获取list中某个属性的集合/将list中某个属性的集合提取出来
2022-04-26 09:21:49java 获取list中某个属性的集合/将list中某个属性的集合提取出来 -
java8新特性将List中按指定属性排序过滤重复数据的方法
2020-08-25 18:49:22主要介绍了java8新特性将List中按指定属性排序过滤重复数据的方法,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下 -
mybatis 参数为对象,取对象中list集合中某个元素中的属性
2021-07-29 13:52:58vo对象: @Data public class TestVo { private String id; private List<Test> testList; } testList类 @Data public class Test{ private String likeName; } dao接口: @Mapper @Repository ... -
java8 steam 获取对象集合中指定属性的 值 累加总和
2022-01-07 11:55:55List<MoocStudyPlanStudent> studyPlanStudentList = new ArrayList<>(); studyPlanStudentList.add(new MoocStudyPlanStudent().setAcquireCoursePeriod(10d)); studyPlanStudentList.add(new ... -
如何给获取到的List集合里面的对象重新赋值
2019-07-13 14:33:34如何给获取到的List集合里面的对象重新赋值 2018年01月05日 16:55:23回到从前变成猫阅读数 7465标签:list集合更多 个人分类:java基础 版权声明:本文为博主原创文章,未经博主允许不得转载。 ... -
java8从list集合中取出某一属性的值的集合案例
2021-01-17 17:47:34我就废话不多说了,大家还是直接看代码吧~List list = new ArrayList();Order o1 = new Order("1","MCS-2019-1123");list.add(o1 );Order o2= new Order("2","MCS-2019-1124");list.add(o2);Order o3= new Order("3... -
获取自定义对象List集合中的某一字段的所有值,jdk8新特性[Optional]
2020-08-07 11:18:15// 字符串String,用英文逗号隔开 String userNames = StringUtils....// Map集合 Map<String, String> operatorRN = operatorInfoList.stream().collect(Collectors.toMap(Operator::getUsername, Operat.