Python 官方文档:入门教程 => 点击学习
目录Java集合框架集合List接口ArrayListVectorLinkedList:泛型:Set接口HashSetTreeSetMap接口特点:遍历:HashMapHashtab
测试
Collection col = new ArrayList();
col.add("张三");
col.add("李四");
col.add("王五");
// col.add("张三");
System.out.println(col);
// col.remove("张三");
// System.out.println(col);
for (Object o : col) {
System.out.println(o);
}
System.out.println("------------------");
Iterator it = col.iterator();
while (it.hasNext()){
String next = (String) it.next();
System.out.println(next);
}
System.out.println(col.isEmpty());
System.out.println(col.contains("张三"));
特点:有序、有下标、元素可以重复。
可以通过角标在指定位置添加查询元素。
List list = new ArrayList();
list.add("java");
list.add("c++");
list.add(1,"python");
list.add(".net");
System.out.println(list.size());
System.out.println(list.toString());
//1.for each遍历
System.out.println("---------------");
for (Object o : list) {
System.out.println(o);
}
//2.迭代器遍历
System.out.println("---------------");
Iterator iterator = list.iterator();
while (iterator.hasNext()){
System.out.println(iterator.next());
}
//3.list迭代器遍历
System.out.println("--------正序-------");
ListIterator listIterator = list.listIterator();
while (listIterator.hasNext()){
System.out.println(listIterator.next());
}
//逆序前必须先进行正序遍历,让指针指向列表最后一个元素,才能开发遍历
System.out.println("--------逆序-------");
while (listIterator.hasPrevious()){
System.out.println(listIterator.previousIndex() + ":" +listIterator.previous());
}
添加数字等基本类型数据时,会进行自动装箱的操作。
删除数字元素需要通过下标来删除,或者将需要删除的数字转成object类或者该类型对应的包装类。
subList
:返回一个子集合,含头不含尾。
List实现类
public boolean add(E e) {
ensureCapacityInternal(size + 1); // Increments modCount!!增长修改个数
elementData[size++] = e;
return true;
}
private void ensureCapacityInternal(int minCapacity) {
if (elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA) {
minCapacity = Math.max(DEFAULT_CAPACITY, minCapacity);
}
ensureExplicitCapacity(minCapacity);
}
private void ensureExplicitCapacity(int minCapacity) {
modCount++;
// overflow-conscious code
if (minCapacity - elementData.length > 0)
grow(minCapacity);
}
private void grow(int minCapacity) {
// overflow-conscious code
int oldCapacity = elementData.length;
int newCapacity = oldCapacity + (oldCapacity >> 1);
if (newCapacity - minCapacity < 0)
newCapacity = minCapacity;
if (newCapacity - MAX_ARRAY_SIZE > 0)
newCapacity = hugeCapacity(minCapacity);
// minCapacity is usually close to size, so this is a win:
elementData = Arrays.copyOf(elementData, newCapacity);
}
测试代码:
ArrayList arrayList = new ArrayList();
Student s1 = new Student("张三",18);
Student s2 = new Student("李四",18);
Student s3 = new Student("王五",18);
arrayList.add(s1);
arrayList.add(s2);
arrayList.add(s3);
System.out.println(arrayList.toString());
//删除元素(需要重写equals方法)
arrayList.remove(new Student("李四",18));
System.out.println(arrayList.size());
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Student student = (Student) o;
return age == student.age && Objects.equals(name, student.name);
}
Vector vector = new Vector();
vector.add("java");
vector.add("Python");
vector.add(".net");
System.out.println(vector.toString());
//枚举器遍历
Enumeration elements = vector.elements();
while (elements.hasMoreElements()){
System.out.println(elements.nextElement());
}
泛型集合:参数化类型、类型安全的集合,强制集合元素的类型必须一致。
特点:
特点:无序、无下标、元素不可重复
方法:全部继承自Collection中的方法。
Set实现类
public HashSet(){
map = new HashMap<>();
}
测试代码:
HashSet<Student> set = new HashSet<>();
Student s1 = new Student("张三",18);
Student s2 = new Student("李四",18);
Student s3 = new Student("王五",18);
set.add(s1);
set.add(s2);
set.add(s3);
// set.add(new Student("李四",18));
System.out.println(set.size());
System.out.println(set.toString());
// set.remove(new Student("李四",18));
// System.out.println(set.size());
// System.out.println(set.toString());
for (Student student : set) {
System.out.println(student);
}
System.out.println("====================");
Iterator<Student> iterator = set.iterator();
while (iterator.hasNext()){
System.out.println(iterator.next());
}
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Student student = (Student) o;
return age == student.age && Objects.equals(name, student.name);
}
public int hashCode() {
return Objects.hash(name, age);
}
hashcode重写方法中加入31的原因
1.31是一个质数,减少散列冲突
2.31提高执行效率
TreeSet<String> treeSet = new TreeSet<>(new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
int n1 = o1.length() - o2.length();
int n2 = o1.compareTo(o2);
return n1==0?n2:n1;
}
treeSet.add("zhangSan");
treeSet.add("wkf");
treeSet.add("asd");
treeSet.add("abc");
treeSet.add("ljCv");
treeSet.add("liSi");
treeSet.add("wanG");
System.out.println(treeSet.toString());
System.out.println(treeSet.size());
1.用于储存任意键值对(Key,Value)
2.键:无序、无下标、不允许重复
3.值:无序、无下标、允许重复
测试代码:
Map<String, String> map = new HashMap<>();
map.put("wkf","666");
map.put("qwe","678");
map.put("kfc","999");
map.put("asd","694");
Set<String> keySet = map.keySet();
for (String s : keySet) {
System.out.println(s + "=" + map.get(s));
}
System.out.println("===================");
Set<Map.Entry<String, String>> entries = map.entrySet();
for (Map.Entry<String, String> entry : entries) {
System.out.println(entry.geTKEy() +"=" + entry.getValue() );
}
static final int DEFAULT_INITIAL_CAPACITY = 1 << 4; // hashMap初始容量大小16
static final int MAXIMUM_CAPACITY = 1 << 30;//hashMap的数组最大容量
static final float DEFAULT_LOAD_FACTOR = 0.75f;//默认加载因子
static final int TREEIFY_THRESHOLD = 8;//jdk1.8开始,当链表长度大于8时,调整成红黑树
static final int UNTREEIFY_THRESHOLD = 6;//jdk1.8开始,当链表长度小于6时,调整成链表
static final int MIN_TREEIFY_CAPACITY = 64;//jdk1.8开始,当链表长度大于8时,并且集合元素个数大于等于64时调整成红黑树
transient node<K,V>[] table;//哈希表中的数组
总结:
Collections工具类
本篇文章就到这里了,希望能给您带来帮助,也希望您能够多多关注编程网的更多内容!
--结束END--
本文标题: 新手初学Java集合框架
本文链接: https://www.lsjlt.com/news/130018.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0