set和list转换_list和set
2023-06-13 09:14:44 时间
set集合元素唯一,无序;list集合元素可以重复,有序。
1、set转list:数据保持不变,顺序发生变化,可以使用Collections.sort进行排序(Collections.shuffle 随机排序,Collections.reverse 反转顺序)。
2、list转set:去除重复数据,只保留一个。转成linkedHashSet时,原顺序不变;转成treeSet可以排序,转成hashSet无序。
package set;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import java.util.Vector;
public class ReverseLinkedHashSet {
public static void main(String args[]){
testSetConvertList();
testListConvertSet();
}
static void testSetConvertList(){
Set<Integer> hashSet = new HashSet<Integer>();
Set<Integer> linkedHashSet = new LinkedHashSet<Integer>();
for(int i=1 ;i<20;i++){
hashSet.add(i);
linkedHashSet.add(i);
}
System.out.println("hashSet: "+hashSet);
System.out.println("linkedHashSet: "+linkedHashSet);
List<Integer> hashSetList = new ArrayList<Integer>(hashSet);
List<Integer> linkedHashSetList = new ArrayList<Integer>(linkedHashSet);
System.out.println("hashSet转list集合: "+hashSetList);
System.out.println("linkedHashSet转list集合: "+linkedHashSetList);
Collections.reverse(linkedHashSetList);
System.out.println("反转排序linkedHashSetList集合: "+linkedHashSetList);
}
@SuppressWarnings("serial")
static void testListConvertSet(){
List<Integer> vector = new Vector<Integer>();
List<Integer> linkedList = new LinkedList<Integer>();
List<Integer> dataList1 = new LinkedList<Integer>(){
{add(1);add(2);add(2);add(4);add(5);}};
List<Integer> dataList2 = new LinkedList<Integer>(){
{add(7);add(6);add(16);add(17);add(18);add(19);add(19);}};
List<Integer> dataList3 = new LinkedList<Integer>(){
{add(11);add(12);add(13);add(14);add(15);}};
vector.addAll(dataList1);
vector.addAll(dataList2);
vector.addAll(dataList3);
linkedList.addAll(dataList1);
linkedList.addAll(dataList2);
linkedList.addAll(dataList3);
System.out.println("vector: "+vector);
System.out.println("linkedList: "+linkedList);
Collections.sort(vector);
System.out.println("排序vector: "+vector);
Collections.shuffle(vector);
System.out.println("随机排序vector: "+vector);
Collections.sort(vector);
System.out.println("排序vector: "+vector);
Collections.reverse(vector);
System.out.println("反转排序vector: "+vector);
Set<Integer> hashSet = new HashSet<Integer>(linkedList);
Set<Integer> linkedSet = new LinkedHashSet<Integer>(linkedList);
System.out.println("hashSet: "+hashSet);
System.out.println("linkedSet: "+linkedSet);
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/183496.html原文链接:https://javaforall.cn
相关文章
- 深入SQL执行计划之CBO查询转换(7):Distinct 配置最优机能(Distinct Placement)
- java 时间转换成字符串_JAVA8时间新特性时间与字符串直接的转换
- java list 转json 字符串_Java之JSON字符串与List集合之间相互转换
- QRExfiltrate:一款基于二维码图像的数据转换与提取工具
- 功能熟悉Oracle的SET功能,提高工作效率(oracle的set)
- 深入浅出:Oracle数据库SET操作(oracle数据库set)
- Linux atoi函数:将数字字符串转换为整型(linuxatoi)
- 命令的使用探索Linux中Set命令的神秘面纱(linux中set)
- 妙用Oracle: 将时间格式转换(oracle时间格式转换)
- Redis清空Set的一秒操作(redis清空set)
- MySQL中如何设置变量使用SET语句(mysql 中set变量)
- MySQL中SET指令的使用规则与作用域详解(mysql中set作用域)
- 查看Redis中Set的内容(查看redis中的set)
- 使用Redis管理Set集合的实例(set集合存入redis)
- 红色随机Redis 的Set类型(set 随机 redis)
- 重塑Redis Set,实现进一步发挥功能(修改redis set)
- Oracle中如何使用SET命令(oracle中的set)
- 深入浅出Redis集群Set的简单操作(redis集群set过程)