讨论:关于插入到容器Set,Map,List中的值,在容器中的顺序问题

共识区块链技术社区 2004-03-24 05:58:28
我写了一个往一些容器中添加元素的测试函数,如果的结果有点惊讶。
代码如下:
...全文
375 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
zmpcoming 2004-04-04
  • 打赏
  • 举报
回复
mark
shine333 2004-03-25
  • 打赏
  • 举报
回复
删除某个key就比较麻烦一点,需要把这key所在的节点下面的子节点与其父节点联系起来。
做法就是递归提升某些子节点的辈分(遵照左先右后的顺序吧)
shine333 2004-03-25
  • 打赏
  • 举报
回复
关于TreeMap,这些最好还是看一看源码
我上面曾说过Map是按照key去找value的,因此其存放位置也是以key为标准的。

一个TreeMap,要么放到其中的key是Comparable(一个key可以自己和另外一个key进行比较)的,要么你向它提供Comparator来比较两个key的大小。

你put的时候,TreeMap从根节点(也就是你放进去的第一个key/value对)开始进行递归比较:

如果当前节点是空的话,则要放的key/value对的位置就是当前节点。
如果新进的key与当前节点的key相同,则用新近的value冲掉原有的。
如果新进的key比当前节点的key小,则查找当前节点的左节点,重新进行比较。
如果新进的key比当前节点的key大,则查找当前节点的右节点,重新进行比较。

为了描述方便,我讲的和程序里面稍微有些出入,程序是如果左右子节点为空,就新建一个节点,并把要放的key/value对放进去。

get的时候与之类似,只不过如果节点为空说明找不到相应的key
yeshucheng 2004-03-25
  • 打赏
  • 举报
回复
不错,学习
这个要理解数据结构可能更加好,呵呵
Leemaasn 2004-03-25
  • 打赏
  • 举报
回复
Up
研究一下。
北极猩猩 2004-03-25
  • 打赏
  • 举报
回复
遍历顺序与插入顺序相同的容器是是实现了List接口的容器;
遍历顺序是经过排序的容器是实现了SortedSet和SortedMap接口的容器(SortedMap是按键的值排序的)
HashSet和HashMap的遍历顺序看起来是随机的,其实它是按照hashCode顺序排列的
satangf 2004-03-25
  • 打赏
  • 举报
回复
list顺序就是按放入元素的先后排的呀。
至于具体的shine333(enihs) 已经说的差不多了。
BTXWUHEN 2004-03-24
  • 打赏
  • 举报
回复
想知道底層的可以去看它的源文件,JDK下不是有個SRC.ZIP這個壓縮文件裏面就有這個類的源文件,想知道底層的,自己解開來研究研究就行了~
老土豆T 2004-03-24
  • 打赏
  • 举报
回复
恩。不懂。。

不如看看原码吧。
  • 打赏
  • 举报
回复
我想知道更加底层的一点东西,比如tree,到底是一种什么样的顺序呢?
shine333 2004-03-24
  • 打赏
  • 举报
回复
List, Map, Set都只是接口而已,里面数据具体的存放方式可能会依具体的类不同而显得完全不一样的。比如我写的一个List其存储方式可以与ArrayList完全相反。
但是通常而言,我们说List,其实是在说ArrayList/LinkedList,Map就是HashMap/TreeMap,下面我所说的,也是基于这几个类的

(理论上)List是有序的,这个从其接口get(int), indexOf(Object)这些方法上就可以看出来
List的顺序就是你放他们的顺序,Set基本上就是一个List,唯一的区别就是同一个东西,List里面你可以多次重复放进去,而Set里面只能放一次,也就是第一次。

Map 是一个Key/Value对的集合,他们之间的关系就好像y=f(x); x是key, y是value, 一个key只能对应一个value, 因此通常是根据Key来决定存放Value(或者Key/Value对)的位置的。正是基于这个原因,Map的存放顺序与你放他们的顺序无关。而和他们的散列码hashCode有关
Hodex 2004-03-24
  • 打赏
  • 举报
回复
从名字就可以看出来,一个是list,肯定是排序了的
一个是hash,肯定是随机的
而tree也是一种有序的
fantasyCoder 2004-03-24
  • 打赏
  • 举报
回复
呵呵!!!
list存放数据是顺序的

set存放数据则是无序的
所以你取出来时和你
放进去时不同
eshow 2004-03-24
  • 打赏
  • 举报
回复
关注+学习!
  • 打赏
  • 举报
回复
不知道那位大虾能够解释一下他们的区别!!
谢谢!
  • 打赏
  • 举报
回复
输出如下:
Add elements into arrayList
11 9 7 3 1 8 9 6 5 9 6 2 11 14 3
The elements in arrayList is :
[11, 9, 7, 3, 1, 8, 9, 6, 5, 9, 6, 2, 11, 14, 3]


Add elements into linkedList
3 10 8 5 1 7 3 10 10 11 14 10 14 2 0
The elements in linkedList is :
[3, 10, 8, 5, 1, 7, 3, 10, 10, 11, 14, 10, 14, 2, 0]


Add elements into hashSet
6 10 14 13 11 9 12 8 5 2 7 3
The elements in hashSet is :
[2, 13, 8, 9, 11, 6, 3, 14, 7, 10, 5, 12]


Add elements into treeSet
6 1 14 9 8 11 4 5 2 0 13 3
The elements in treeSet is :
[0, 1, 2, 3, 4, 5, 6, 8, 9, 11, 13, 14]


Add elements into hashMap
14 7 2 13 0 9 2 8 5 5 12 8 11 9 6
The elements in hashMap is :
{4=0, 8=5, 11=8, 3=13, 7=8, 12=11, 2=2, 13=9, 9=5, 6=2, 1=7, 14=6, 10=12, 5=9, 0=14}


Add elements into treeMap
0 4 12 0 2 8 3 6 3 7 3 13 2 3 3
The elements in treeMap is :
{0=0, 1=4, 2=12, 3=0, 4=2, 5=8, 6=3, 7=6, 8=3, 9=7, 10=3, 11=13, 12=2, 13=3, 14=3}
Finished executing
  • 打赏
  • 举报
回复
import java.util.*;

public class TestCollectionSequence
{
ArrayList arrayList = null;
LinkedList linkedList = null;
HashSet hashSet = null;
TreeSet treeSet = null;
HashMap hashMap = null;
TreeMap treeMap = null;

public TestCollectionSequence()
{
// arrayList
arrayList = new ArrayList();
System.out.println("Add elements into arrayList");
for(int i = 0; i < 15; i++)
{
Integer r = new Integer((int)(Math.random()*15));
arrayList.add(r);
System.out.print(r + " ");
}
System.out.println("\nThe elements in arrayList is : ");
System.out.println(arrayList);
System.out.println("\n");

//linkedList
linkedList = new LinkedList();
System.out.println("Add elements into linkedList");
for(int i = 0; i < 15; i++)
{
Integer r = new Integer((int)(Math.random()*15));
linkedList.add(r);
System.out.print(r + " ");
}
System.out.println("\nThe elements in linkedList is : ");
System.out.println(linkedList);
System.out.println("\n");

// hashSet
hashSet = new HashSet();
System.out.println("Add elements into hashSet");
for(int i = 0; i < 15; i++)
{
Integer r = new Integer((int)(Math.random()*15));
if(hashSet.add(r))
System.out.print(r + " ");
}
System.out.println("\nThe elements in hashSet is : ");
System.out.println(hashSet);
System.out.println("\n");

//
treeSet = new TreeSet();
System.out.println("Add elements into treeSet");
for(int i = 0; i < 15; i++)
{
Integer r = new Integer((int)(Math.random()*15));
if(treeSet.add(r))
System.out.print(r + " ");
}
System.out.println("\nThe elements in treeSet is : ");
System.out.println(treeSet);
System.out.println("\n");


//hashMap
hashMap = new HashMap();
System.out.println("Add elements into hashMap");
for(int i = 0; i < 15; i++)
{
Integer r = new Integer((int)(Math.random()*15));
System.out.print(r + " ");
hashMap.put(new Integer(i), r);
}
System.out.println("\nThe elements in hashMap is : ");
System.out.println(hashMap);
System.out.println("\n");

//treeMap
treeMap = new TreeMap();
System.out.println("Add elements into treeMap");
for(int i = 0; i < 15; i++)
{
Integer r = new Integer((int)(Math.random()*15));
System.out.print(r + " ");
treeMap.put(new Integer(i), r);
}
System.out.println("\nThe elements in treeMap is : ");
System.out.println(treeMap);
}

public static void main(String[] args)
{
TestCollectionSequence test = new TestCollectionSequence();
}
}

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧