关于list array,字符串

/**
*  选取list对象内属性抽取为新list
*/
public static List<String> arrayToList(List<Student> students){
   //方法一:转list
   List<String> collect = students.stream().map(m -> 
      return m.getStudentName();
   
   }).collect(Collectors.toList());


//方法二:转list
   List<String> collect2 = students.stream().map(Student:: 
           getStudentName).collect(Collectors.toList());

   return collect;
}

/**
*  选取list对象根据某个属性条件进行过滤
*/
public static List<Student> arrayToList(List<Student> students){
   List<Student> newStudent = students.stream().filter(s -> 
          100==s.getGrade).collect(Collectors.toList());

   return newStudent;
}

/**
* 从循环列表中删除元素的更好方法1-使用迭代器提供的remove
*/
  //直接Arrays.asList(数组),返回的是内部的私有静态类,通过new ArrayList<String>可以让数组转的list成为一个真实可变的
  ArrayList<String> list = new ArrayList<String>(Arrays.asList(strings));
//list使用迭代器删除元素比for,foreach循环要更好,因为删除的时候,列表的大小会缩小,索引会更改。因此使用for删除会存在问题,而使用foreach,存在报错concurrentmodificationException
  Iterator<String> iter = list.iterator();
  while (iter.hasNext()) {
	String s = iter.next();
	if (s.equals("!")) {
		iter.remove();
	}
}

/**
* 从循环列表中删除元素的更好方法2-建立一个集合记录要删除的元素,直接全部删除,不走索引(不推荐)
*/
  ArrayList<String> list = new ArrayList<String>(Arrays.asList(strings));
  ArrayList<String> deleteList = new ArrayList<String>(Arrays.asList("!"));
  deleteList.foreach(l->{
    list.remove(l);
  })  
}

/**
* 从循环列表中删除元素的更好方法3-使用并发集合类CopyOnWriteArrayList而不是ArrayList来避免 
* ConcurrentModificationException
*/
 List<String> list = new CopyOnWriteArrayList<String>();
 list.add("hello");
 list.add("world");
 list.add("!");
 
for (String s : list) {
    if (s.equals("!")) {
        list.remove(s);
    }
}





// 获取学生成绩总和

int sum = list.stream().mapToInt(Student::getGrade).sum();

//根据学生成绩进行排序
//降序
 List<Student> NewStudents = students.stream.sorted(Comparator.comparing(Student::getGrade)
    .reversed()).collect(Collectors.toList()); 
// 升序
 List<Student> NewStudents = students.stream.sorted(Comparator.comparing(Student::getGrade)
    .collect(Collectors.toList()); 

//综合 1,过滤缺考学生 2,根据学生成绩降序排序  3,若有重复数据则去重
List<Student> NewStudents = students.stream.filter(s ->"N".equals(s.getIsExcem)).sorted(Comparator.comparing(Student::getGrade).reversed())
    .distinct.collect(Collectors.toList());

/**
*  数组转list
*/
public static List<Student> arrayToList(Student[] students){
//转list1
   List<Student> collect = Arrays.stream(students).collect(Collectors.toList());
(类似转集合:Set<String> set = new HashSet<String>(Arrays.asList(stringArray));)
//转list2
  String[] strings = {"你好", "啊", "!"}
   List<Student> list = Arrays.asList(strings);(适用于对象型数据的数组(String、Integer...),此方法得到的List的长度不可改变,但是当更新一个时,另一个自动更新)
// 判断非空
   List<Student> studentList= Optional.ofNullable(collect).orElse(Collections.emptyList());
//判断数组是否包含某个值
    Arrays.asList(studentList).contains("!");
//打印数组
System.out.println(Arrays.toString(students));
//连接两个数组
int[] array1 = { 1, 2, 3, 4, 5 };
int[] array2 = { 6, 7, 8, 9, 10 };
int[] newArray = ArrayUtils.addAll(array1, array2);
//数组转字符串并用逗号拼接
String  student = StringUtils.join(students,",");
//根据逗号将字符串转数组
Student[] students2 =student.split(",");
//list转数组
 ArrayList<String> list = new ArrayList<String>(Arrays.asList(strings));
 Student[] s = new Student[list.size]
 list.toArray(s);
//反转数组
String[] re = {"hello","world"};
ArrayUtils.reverse(re);
(数组变为"world","hello")
//删除数组元素
int[] Array = { 1, 2, 3, 4, 5 };
int[] newArray = ArrayUtils.removeElement(intArray, 3);
int[] newArray2 = ArrayUtils.remove(intArray, 0);根据index删除
int[] newArray3 = ArrayUtils.removeElements(intArray, 3,4);删除多个元素


}
//重复字符串
String s = "1234";
String s2 = StringUtils.repeat(s,2);("12341234");

//统计字符串中某个字符出现的次数
int num = StringUtils.countMatches(s2+"1","1");
num = 3;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值