标签:Map集合 tree 行存储 add block hello 数据 city 从后往前
是对象的容器,定义了对多个对象进行操作的常用方法,类似数组功能
和数组的区别:
package oop;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
public class Test {
public static void main(String[] args) {
//创建集合
Collection collection=new ArrayList();
//添加元素
collection.add("apple");
collection.add("banana");
collection.add("peach");
System.out.println(collection.toString());
//删除元素
collection.remove("apple");
System.out.println(collection.size());
//遍历元素
//1.增强for
for (Object object:collection
) {
System.out.println(object);
}
//2.使用迭代器(专门用来遍历集合的一种方式)
//hasNext():有没有下一个元素;
//next():获取下一个元素;
//remove():删除当前元素
Iterator it=collection.iterator();
while(it.hasNext()){
String s=(String)it.next();
System.out.println(s);
//在遍历过程中不能使用collection的删除方法,可以使用迭代器的删除方法
it.remove();
}
System.out.println(collection.size());
//判断元素是否存在
System.out.println(collection.contains("apple"));
System.out.println(collection.isEmpty());
}
}
List接口的实现类有ArrayList,Vector,LinkedList
package oop;
import java.util.ArrayList;
import java.util.List;
import java.util.Iterator;
import java.util.ListIterator;
public class Test {
public static void main(String[] args) {
List list=new ArrayList();
//添加元素(自动装箱)
list.add("apple");
list.add("banna");
list.add(0,"pen");
System.out.println(list.toString());
//删除
list.remove(1);
System.out.println(list.toString());
//遍历
//1.使用for遍历
for(int i=0;i<list.size();i++){
System.out.println(list.get(i));
}
//2.使用增强for
for(Object object:list){
System.out.println(object);
}
//3.使用迭代器
Iterator it=list.iterator();
while(it.hasNext()){
System.out.println(it.next());
}
//4.使用列表迭代器ListIterator,可以向前向后遍历,添加,删除,修改元素
//从前往后遍历
ListIterator lit=list.listIterator();
while(lit.hasNext()){
System.out.println(lit.nextIndex()+":"+lit.next());
}
//从后往前
while(lit.hasPrevious()){
System.out.println(lit.previousIndex()+":"+lit.previous());
}
//判断
System.out.println(list.contains("pen"));
System.out.println(list.isEmpty());
//获取位置
System.out.println(list.indexOf("pen"));
//subList;返回子集合,含头不含尾
List subList=list.subList(0,1);
System.out.println(subList.toString());
}
}
package oop;
import java.util.ArrayList;
import java.util.List;
import java.util.Iterator;
import java.util.ListIterator;
public class Test {
public static void main(String[] args) {
//创建集合
ArrayList al=new ArrayList();
//添加元素
Student s1=new Student("zs",19);
Student s2=new Student("ls",20);
Student s3=new Student("ww",21);
al.add(s1);
al.add(s2);
al.add(s3);
//删除元素
al.remove(0);
System.out.println(al.size());
//遍历元素
// 使用迭代器
Iterator it =al.iterator();
while(it.hasNext()){
Student s=(Student)it.next();//it.next()是Object类型,所以需要强转换
System.out.println(s);
}
ListIterator lt=al.listIterator();
while(lt.hasNext()){
System.out.println(lt.nextIndex()+":"+lt.next());
}
while(lt.hasPrevious()){
System.out.println(lt.previousIndex()+":"+lt.previous());
}
}
}
ArrayList源码解析
package oop;
import java.util.*;
public class Test {
public static void main(String[] args) {
//创建集合
Vector vector= new Vector();
//添加元素
vector.add("apple");
vector.add("bannana");
vector.add("peach");
//遍历
//使用枚举器
Enumeration en=vector.elements();
while(en.hasMoreElements()){
System.out.println(en.nextElement());
}
}
}
链表结构实现,存储结构是双向链表
ArrayList:必须开辟连续空间,查询快,增删慢
LinkedList:无需开辟连续空间,查询慢,增删快
把类型作为参数来传递
package oop;
/**
* 泛型类
* 语法:类名<T,E,...>,T是类型占位符,表示一种引用类型
*/
public class Test<T> {
//使用泛型
//创建变量,不能实例化
T t;
//作为方法的参数
public void show(T t){
System.out.println(t);
}
//泛型作为方法的返回值
public T getT(){
return t;
}
}
调用
package oop;
public class Test2 {
public static void main(String[] args) {
Test<String> test=new Test<>();
test.t="dengwenxiong";
test.show("你刚好");
String str=test.getT();
System.out.println(str);
Test<Integer> test1=new Test<>();
test1.t=100;
test1.show(101);
int num1=test1.getT();
System.out.println(num1);
}
}
package oop;
/**
*泛型接口
* 语法:接口名<T>
*/
public interface test3<T> {
String name="dengwenxiong";
T server(T t);
}
package oop;
/**
* 泛型方法
* 语法:<T> 返回值类型
*/
public class Test{
public <T> void show(T t){
System.out.println("泛型方法"+t);
}
}
例:ArrayList
al=new ArrayList ();
那么往集合里添加数据就必须是String类型的,否则会报错;
无序,无下标,元素不可重复
Set接口的使用
package oop;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
public class Test{
public static void main(String[] args) {
Set<String> set=new HashSet<>();
//添加数据
set.add("peach");
set.add("apple");
set.add("bannaa");
set.add("apple");//set不允许有重复元素
System.out.println(set.toString());
//遍历
//使用增强for,无法使用普通for循环,因为其没有下标索引
for(String string:set){
System.out.println(string);
}
//使用迭代器Iterator
Iterator<String> it =set.iterator();
while(it.hasNext()){
System.out.println(it.next());
}
}
}
存储过程:
package oop;
import java.util.HashSet;
public class Test{
public static void main(String[] args) {
HashSet<Student> hashSet=new HashSet<>();
Student s1=new Student("deng",19);
Student s2=new Student("wen",11);
Student s3=new Student("xiong",12);
hashSet.add(s1);
hashSet.add(s2);
hashSet.add(s3);
hashSet.add(new Student("deng",19));//也能添加进去;重写equals()方法可避免
System.out.println(hashSet.toString());
//
hashSet.remove(new Student("deng",19));//重写了equals方法后可删除
System.out.println(hashSet.toString());
}
}
package oop;
import java.util.TreeSet;
//其元素必须实现Comparable接口,compareTo()方法返回值为0,认为是重复元素
public class Test{
public static void main(String[] args) {
//创建集合
TreeSet<Student> treeSet=new TreeSet<>();
//添加元素
Student s1=new Student("deng",11);
Student s2=new Student("wen",12);
Student s4=new Student("wen",15);
Student s3=new Student("xiong",13);
treeSet.add(s1);
treeSet.add(s2);
treeSet.add(s3);
treeSet.add(s4);
System.out.println(treeSet.toString());
}
}
package oop;
public class Student implements Comparable {
private String name;
private int age;
public Student(String name,int age){
this.name=name;
this.age=age;
}
@Override
public String toString() {
return "Student{" + "name="+name+" "+"age=" + age + ‘}‘;
}
@Override
public int compareTo(Object o) {
if(o instanceof Student) {
Student p=(Student)o;
int n1 = this.name.compareTo(p.name);
int n2 = this.age - p.age;
return n1 == 0 ? n2 : n1;
}
return 0;
}
}
‘‘‘java
package oop;
import java.util.Comparator;
import java.util.TreeSet;
//使用compartor元素可以不实现comparable接口
public class Test{
public static void main(String[] args) {
//创建集合,并指定规则
TreeSet
@Override
public int compare(Student o1, Student o2) {
int n1=o1.getAge()-o2.getAge();
int n2=o1.getName().compareTo(o2.getName());
return n1==0?n2:n1;
}
});
//添加元素
Student s1=new Student("deng",11);
Student s2=new Student("wen",12);
Student s4=new Student("wen",15);
Student s3=new Student("xiong",13);
treeSet.add(s1);
treeSet.add(s2);
treeSet.add(s3);
treeSet.add(s4);
System.out.println(treeSet.toString());
}
}
‘‘‘
package oop;
import java.util.Comparator;
import java.util.TreeSet;
//使用compartor实现按字符串的长度进行排序
public class Test{
public static void main(String[] args) {
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("helloworld");
treeSet.add("zhang");
treeSet.add("lisi");
treeSet.add("wangwu");
treeSet.add("beijing");
treeSet.add("xian");
treeSet.add("nanjing");
System.out.println(treeSet.toString());
}
}
Map接口特点:
package oop;
import java.util.*;
//map接口使用
public class Test{
public static void main(String[] args) {
//创建集合
Map<String,String> map=new HashMap<>();
//添加元素
map.put("cn","中国");
map.put("usa","美国");
map.put("us","英国");
System.out.println(map.toString());
//删除
map.remove("us");
System.out.println(map.toString());
//遍历
//使用keySet()--返回的是一个key的set集合;
Set<String> set=map.keySet();
for(String str:set){
System.out.println(str+":"+map.get(str));
}
//使用entrySet进行遍历,返回一个Entry对象(键值对)的set集合
for(Map.Entry<String,String> entries:map.entrySet()){
System.out.println(entries.getKey()+":"+entries.getValue());
}
//判断
System.out.println(map.containsKey("cn"));
System.out.println(map.containsValue("中国"));
}
}
package oop;
import java.util.*;
//HashMap的使用;存储结构:哈希表(数组+链表+红黑树);使用key的hashcode和equals判断重复
public class Test{
public static void main(String[] args) {
HashMap<Student,String> student=new HashMap<>();
Student s1=new Student("deng",12);
Student s2=new Student("wen",13);
Student s3=new Student("xiong",14);
student.put(s1,"beijing");
student.put(s2,"tianmen");
student.put(s3,"hangzhou");
System.out.println(student.toString());
}
}
当数组长度大于64,链表长度大于8试,会从链表转换成红黑树进行存储;当链表长度小于6时,会变为链表存储。线程不安全,运行效率快;允许null作为key或value;
实现了SortedMap接口(是Map的子接口),可以对key自动排序
package oop;
import java.util.*;
//TreeMap的使用;存储结构:红黑树;
public class Test{
public static void main(String[] args) {
//定制比较,或者Student类必须实现Comparable接口
TreeMap<Student,String> treeMap=new TreeMap<>(new Comparator<Student>(){
@Override
public int compare(Student o1, Student o2) {
int n1=o1.getAge()-o2.getAge();
int n2=o1.getName().compareTo(o2.getName());
return n1==0?n2:n1;
}
});
Student s1=new Student("deng",11);
Student s2=new Student("wen",12);
Student s3=new Student("xiong",13);
treeMap.put(s1,"上海");
treeMap.put(s2,"北京");
treeMap.put(s3,"湖北");
System.out.println(treeMap.toString());
}
}
package oop;
import java.util.*;
public class Test{
public static void main(String[] args) {
List<Integer> lt=new ArrayList<>();
lt.add(10);
lt.add(340);
lt.add(101);
lt.add(1054);
lt.add(102);
lt.add(1089);
System.out.println(lt.toString());
//排序sort()
Collections.sort(lt);
System.out.println(lt);
//搜索binarySearch()
int i=Collections.binarySearch(lt,340);
System.out.println(i);
//复制copy()
List<Integer> cplt=new ArrayList<>();
for(int j=0;j<lt.size();j++){
cplt.add(0);
}
Collections.copy(cplt,lt);
System.out.println(cplt);
//反转reverse()
Collections.reverse(lt);
System.out.println(lt);
//打乱shuffle()
Collections.shuffle(lt);
System.out.println(lt);
//list转成数组
Integer[] arr=lt.toArray(new Integer[0]);
System.out.println(Arrays.toString(arr));
//将数组转成list,受限list,无法添加和删除;
String[] names={"zhangshan","lisi","wangwu"};
List<String> list2=Arrays.asList(names);
System.out.println(list2);
}
}
标签:Map集合 tree 行存储 add block hello 数据 city 从后往前
原文地址:https://www.cnblogs.com/python-road/p/13220854.html