Java之集合(15个demo)
Posted nuist__NJUPT
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java之集合(15个demo)相关的知识,希望对你有一定的参考价值。
本节主要介绍java的集合,主要包括List、Set和Map,其中List的子类ArrayList和LinkedList,Set的子类HashSet和TreeSet,Map的字类HashMap等,介绍了集合的常用方法,Collections工具类以及Comparable和Comparator排序方法,同时介绍了泛型和集合的嵌套使用等。
目录
3-List集合的子类ArrayList和LinkedList
6-TreeSet集合Comparable自然排序(实现该接口并重写CompareTo()方法)
7-TreeSet集合比较排序器Comparator(匿名内部类方式)
15-Collections工具类的使用(存储学生集合并排序)
1-List集合的三种遍历方式及常用方法
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class CollectionDemo01
public static void main(String[] args)
List<String> list = new ArrayList<>() ;
list.add("java") ;
list.add("python") ;
list.add("c++") ;
//增强for遍历
for(String s : list)
System.out.println(s);
//迭代器遍历
Iterator<String> iterator = list.iterator() ;
while(iterator.hasNext())
System.out.println(iterator.next());
//for循环遍历
for(int i=0; i<list.size(); i++)
System.out.println(list.get(i));
//删除指定索引的元素
list.remove(1);
//修改指定索引的元素
list.set(1,"Matlab") ;
//获得指定索引的元素
System.out.println(list.get(1)) ;
2-List集合存储学生对象并遍历
public class Student
private String name ;
private int age ;
public Student()
public Student(String name, int age)
this.name = name;
this.age = age;
public String getName()
return name;
public void setName(String name)
this.name = name;
public int getAge()
return age;
public void setAge(int age)
this.age = age;
@Override
public String toString()
return "Student" +
"name='" + name + '\\'' +
", age=" + age +
'';
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class CollectionDemo02
public static void main(String[] args)
Student s1 = new Student("张三", 15) ;
Student s2 = new Student("李四", 18) ;
Student s3 = new Student("王五", 16) ;
List<Student> list = new ArrayList<>() ;
list.add(s1) ;
list.add(s2) ;
list.add(s3) ;
//迭代器遍历
Iterator<Student> iterator = list.iterator() ;
while(iterator.hasNext())
Student s = iterator.next() ;
System.out.println(s.getName() + "," + s.getAge());
//增强for循环遍历
for(Student student : list)
System.out.println(student.getName() + "," + student.getAge());
//for循环遍历
for(int i=0; i<list.size(); i++)
System.out.println(list.get(i).getName() + "," + list.get(i).getAge() );
3-List集合的子类ArrayList和LinkedList
import java.util.ArrayList;
import java.util.LinkedList;
public class CollectionDemo03
public static void main(String[] args)
ArrayList<String> arrayList = new ArrayList<>() ;
arrayList.add("java");
arrayList.add("python");
arrayList.add("c++");
for(String s : arrayList)
System.out.println(s);
LinkedList<String> linkedList = new LinkedList<>();
linkedList.add("Matlab") ;
linkedList.add("r") ;
linkedList.add("js");
for(String s : linkedList)
System.out.println(s);
//LinkedList的特有功能
linkedList.addFirst("hello");
linkedList.addLast("world");
System.out.println(linkedList.getFirst());
System.out.println(linkedList.getLast());
linkedList.removeFirst() ;
linkedList.removeLast() ;
4-Set集合的字类HashSet
import java.util.HashSet;
import java.util.LinkedHashSet;
public class CollectionDemo04
public static void main(String[] args)
HashSet<String> hashSet = new HashSet<>() ;
hashSet.add("java") ;
hashSet.add("c") ;
hashSet.add("python") ;
for(String s : hashSet)
System.out.println(s);
//LinkedHashSet,链表保证有序,HashSet保证唯一
LinkedHashSet<String> linkedHashSet = new LinkedHashSet<>() ;
linkedHashSet.add("java") ;
linkedHashSet.add("c") ;
linkedHashSet.add("python") ;
linkedHashSet.add("java") ;
for(String s : linkedHashSet)
System.out.println(s);
5-Set集合字类之TreeSet
import java.util.TreeSet;
public class CollectionDemo
public static void main(String[] args)
TreeSet<Integer> treeSet = new TreeSet<>() ;
treeSet.add(20) ;
treeSet.add(10) ;
treeSet.add(30);
treeSet.add(5) ;
treeSet.add(20) ;
//TreeSet集合有序且元素唯一
for(Integer x : treeSet)
System.out.println(x);
6-TreeSet集合Comparable自然排序(实现该接口并重写CompareTo()方法)
public class Student implements Comparable<Student>
private String name ;
private int age ;
public Student()
public Student(String name, int age)
this.name = name;
this.age = age;
public String getName()
return name;
public void setName(String name)
this.name = name;
public int getAge()
return age;
public void setAge(int age)
this.age = age;
@Override
public String toString()
return "Student" +
"name='" + name + '\\'' +
", age=" + age +
'';
@Override
public int compareTo(Student s)
int num1 = this.age - s.age ;
int num2 = num1 == 0 ? this.name.compareTo(s.name) : num1 ;
return num2 ;
import java.util.TreeSet;
public class CollectionDemo05
/**
* 需求:存储学生对象并遍历,按照年龄由小到大排序,
* 若年龄相同,按照姓名的字母顺序排序
* @param args
*/
public static void main(String[] args)
TreeSet<Student> treeSet = new TreeSet<>() ;
Student s1 = new Student("zhangsan", 14) ;
Student s2 = new Student("lisi", 14) ;
Student s3 = new Student("wangwu", 13);
Student s4 = new Student("maliu", 17) ;
Student s5 = new Student("maliu", 17) ;
treeSet.add(s1) ;
treeSet.add(s2) ;
treeSet.add(s3) ;
treeSet.add(s4) ;
treeSet.add(s5) ;
for(Student s : treeSet)
System.out.println(s.getName() + "," + s.getAge());
7-TreeSet集合比较排序器Comparator(匿名内部类方式)
public class Student
private String name ;
private int age ;
public Student()
public Student(String name, int age)
this.name = name;
this.age = age;
public String getName()
return name;
public void setName(String name)
this.name = name;
public int getAge()
return age;
public void setAge(int age)
this.age = age;
@Override
public String toString()
return "Student" +
"name='" + name + '\\'' +
", age=" + age +
'';
import java.util.Comparator;
import java.util.TreeSet;
public class CollectionDemo06
public static void main(String[] args)
TreeSet<Student> treeSet = new TreeSet<>(new Comparator<Student>()
@Override
public int compare(Student s1, Student s2)
int num1 = s1.getAge() - s2.getAge() ;
int num2 = num1==0 ? s1.getName().compareTo(s2.getName()) : num1 ;
return num2 ;
) ;
Student s1 = new Student("zhangsan", 14) ;
Student s2 = new Student("lisi", 14) ;
Student s3 = new Student("wangwu", 13);
Student s4 = new Student("maliu", 17) ;
Student s5 = new Student("maliu", 17) ;
treeSet.add(s1) ;
treeSet.add(s2) ;
treeSet.add(s3) ;
treeSet.add(s4) ;
treeSet.add(s5) ;
for(Student s : treeSet)
System.out.println(s.getName() + "," + s.getAge());
8-泛型类的使用
public class Generic<T>
private T t ;
public T getT()
return t;
public void setT(T t)
this.t = t;
public class CollectionDemo07
public static void main(String[] args)
/**
* 泛型类的好处:在定义时候不需要确实具体的方法参数类型
* 方法调用的时候再给出具体的类型,避免多次写重载方法
*/
Generic<String> generic = new Generic<>() ;
generic.setT("张三");
Generic<Integer> generic1 = new Generic<>() ;
generic1.setT(18);
Generic<Boolean> generic2 = new Generic<>() ;
generic2.setT(true);
System.out.println(generic.getT() + "," + generic1.getT() + "," + generic2.getT());
9-泛型方法的使用
public class Generic1
public <T> void show(T t)
System.out.println(t);
public class CollectionDemo08
public static void main(String[] args)
//定义泛型方法,可以在调用的时候传递任意类型的参数
Generic1 generic = new Generic1() ;
generic.show("张三");
generic.show(18);
generic.show(true);
10-泛型接口的使用
public interface Generic2 <T>
void show(T t) ;
public class Generic2Impl <T> implements Generic2<T>
@Override
public void show(T t)
System.out.println(t);
public class CollectionDemo09
public static void main(String[] args)
Generic2<String> generic2 = new Generic2Impl<>() ;
generic2.show("张三");
Generic2<Integer> generic3 = new Generic2Impl<>() ;
generic3.show(15) ;
11-Map集合之HashMap
import java.util.HashMap;
import java.util.Map;
public class CollectionDemo10
public static void main(String[] args)
Map<String,Integer> map = new HashMap<>() ;
map.put("张三", 18) ;
map.put("李四", 20) ;
map.put("王五", 19) ;
System.out.println(map);
System.out.println(map.isEmpty()); //判空操作
System.out.println(map.size()); //集合大小
System.out.println(map.containsKey("张三"));//是否包含键
System.out.println(map.containsValue(18)); //是否包含值
System.out.println(map.remove("张三")); //删除键值对
// map.clear(); //清空集合
//获取键值对的方式
for(String key : map.keySet())
System.out.println(key + "," + map.get(key));
for(Map.Entry m : map.entrySet())
System.out.println(m.getKey() + "," + m.getValue());
12-ArrayList嵌套HashMap
import java.util.ArrayList;
import java.util.HashMap;
public class CollectionDemo11
public static void main(String[] args)
ArrayList<HashMap> arrayList = new ArrayList<>() ;
HashMap<String,String> h1 = new HashMap<>() ;
h1.put("张三", "18") ;
h1.put("李四", "17") ;
HashMap<String,String> h2 = new HashMap<>() ;
h2.put("马六", "19") ;
h2.put("李五", "14") ;
HashMap<String,String> h3 = new HashMap<>() ;
h3.put("张二", "10") ;
h3.put("李三", "13") ;
arrayList.add(h1) ;
arrayList.add(h2);
arrayList.add(h3);
for(HashMap<String,String> hashMap : arrayList)
for(String key : hashMap.keySet())
System.out.println(key + "," + hashMap.get(key));
13-HashMap嵌套ArrayList
import java.util.ArrayList;
import java.util.HashMap;
public class CollectionDemo12
public static void main(String[] args)
HashMap<String, ArrayList> hashMap = new HashMap<>();
ArrayList<String> array1 = new ArrayList<>();
array1.add("唐僧");
array1.add("孙悟空") ;
hashMap.put("西游记",array1) ;
ArrayList<String> array2 = new ArrayList<>() ;
array2.add("武松") ;
array2.add("鲁智深") ;
hashMap.put("水浒传",array2) ;
for(String key : hashMap.keySet())
ArrayList<String> values = hashMap.get(key) ;
System.out.println(key);
for(String value : values)
System.out.println(value);
14-HashMap统计字符串每个字符出现次数
import java.util.HashMap;
import java.util.Scanner;
public class CollectionDemo13
public static void main(String[] args)
HashMap<Character,Integer> map = new HashMap<>() ;
Scanner input = new Scanner(System.in) ;
String str = input.nextLine() ;
// for(int i=0; i<str.length(); i++)
// char key = str.charAt(i) ;
// if(!map.containsKey(key))
// map.put(key,1) ;
// else
// int count = map.get(key) + 1 ;
// map.put(key,count) ;
//
//
for(int i=0; i<str.length(); i++)
char key = str.charAt(i) ;
int value = map.getOrDefault(key,0) +1 ;
map.put(key,value) ;
for(char key : map.keySet())
System.out.println(key + "的个数是:" + map.get(key));
15-Collections工具类的使用(存储学生集合并排序)
public class Student
private String name ;
private int age ;
public Student()
public Student(String name, int age)
this.name = name;
this.age = age;
public String getName()
return name;
public void setName(String name)
this.name = name;
public int getAge()
return age;
public void setAge(int age)
this.age = age;
@Override
public String toString()
return "Student" +
"name='" + name + '\\'' +
", age=" + age +
'';
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
public class CollectionDemo14
public static void main(String[] args)
ArrayList<Student> arrayList = new ArrayList<>() ;
Student s1 = new Student("linqinngxia", 30) ;
Student s2 = new Student("zhangmanyu",35) ;
Student s3 = new Student("wangzuxian", 33) ;
Student s4 = new Student("liuyan", 33) ;
arrayList.add(s1);
arrayList.add(s2);
arrayList.add(s3);
arrayList.add(s4);
/**
* 按年龄有小到大排序,年龄相同时,
* 按照姓名字母顺序排序
*/
Collections.sort(arrayList, new Comparator<Student>()
@Override
public int compare(Student s1, Student s2)
int num1 = s1.getAge() - s2.getAge() ;
int num2 = num1 == 0 ? s1.getName().compareTo(s2.getName()) : num1 ;
return num2 ;
);
for(Student student : arrayList)
System.out.println(student.getName() + "," + student.getAge());
以上是关于Java之集合(15个demo)的主要内容,如果未能解决你的问题,请参考以下文章
我想从一个名字组 数据列表中随机筛选几个人出来 用php怎么写?