当前位置:网站首页>Set code exercise
Set code exercise
2022-06-27 21:39:00 【continueLR】
Catalog
List Set code :ArrayList And LinkedList
package com.java.jiheWork;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
/*
1.1、 The creation of each collection object (new)
1.2、 Add elements to the collection
1.3、 Take an element out of a collection
1.4、 Ergodic set
*/
public class ArrayListTest {
public static void main(String[] args) {
// Create a collection object
ArrayList<String> list = new ArrayList<>();
// Additive elements
list.add(" Zhang San ");
list.add(" Li Si ");
list.add(" Wang Wu ");
// Take an element out of a collection
// List Sets have subscripts
String firstElt = list.get(0);
System.out.println(firstElt);
System.out.println(list.get(2));
System.out.println("-------------------------------");
// Traverse ( Subscript mode )
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
System.out.println("-------------------------------");
// Traverse ( Iterator mode , This one is universal , all Collection Can be used )
Iterator<String> it = list.iterator();
while (it.hasNext()) {
System.out.println(it.next());
}
System.out.println("-------------------------------");
// while Change the loop to for loop
/*for(Iterator<String> it2 = list.iterator(); it2.hasNext(); ){
System.out.println("====>" + it2.next());
}*/
// Traverse (foreach The way )
for (String s : list) {
System.out.println(s);
}
System.out.println("-------------------------------");
LinkedList<Student> students = new LinkedList<Student>();
Student S1 = new Student(111," Zhang San ");
Student S2 = new Student(122," Li Si ");
Student S3 = new Student(123," Wang Wu ");
students.add(S1);
students.add(S2);
students.add(S3);
Iterator<Student> it2 = students.iterator();
while (it2.hasNext()){
System.out.println(it2.next());
}
System.out.println("-------------------------------");
for (Student ST:students) {
System.out.println(ST);
}
}
}
test result
summary :ArrayList At the bottom of the collection is the array , You can find data directly by subscript , Easy to find and modify . But when you add or delete elements, you need to move other elements , Low efficiency .
LinkedList At the bottom of the collection is the linked list , There is no need to move elements when adding or deleting , Easy to add and delete . But every time you search, you have to go through the previous data , It's not easy to find .
HashSet Code
package com.bjpowernode.javase.review;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Objects;
import java.util.Set;
/*
1.1、 The creation of each collection object (new)
1.2、 Add elements to the collection
1.3、 Take an element out of a collection
1.4、 Ergodic set
1.5、 test HashSet Set characteristics : Disorder cannot be repeated .
*/
public class HashSetTest {
public static void main(String[] args) {
// Create a collection object
HashSet<String> set = new HashSet<>();
// Additive elements
set.add("abc");
set.add("def");
set.add("king");
set.add("abc");
System.out.println(set.size());//3
// Here we find set The number of elements is only 3 individual , Because duplicate elements cannot be added to set In the collection
System.out.println("------------------------");
// Traversal methods cannot use ordinary for loop , because set Set has no subscript
// Traversal methods 1: Iterator mode
Iterator<String> it = set.iterator();
while (it.hasNext()){
System.out.println(it.next());
}
System.out.println("------------------------");
// Traversal methods 2:foreach The way
for (String s:set) {
System.out.println(s);
}
System.out.println("------------------------");
// establish Student Class collection objects
HashSet<Student> st = new HashSet<>();
// establish Student object , among s3 And s1 repeat
Student s1 = new Student(111," Zhang San ");
Student s2 = new Student(222," Li Si ");
Student s3 = new Student(111," Zhang San ");
st.add(s1);
st.add(s2);
st.add(s3);
Iterator<Student> Stu = st.iterator();
while (Stu.hasNext()){
System.out.println(Stu.next());
}
System.out.println("------------------------");
for (Student stu:st) {
System.out.println(stu);
}
}
}
class Student {
int no;
String name;
public Student() {
}
public Student(int no, String name) {
this.no = no;
this.name = name;
}
@Override
public String toString() {
return "Student{" +
"no=" + no +
", name='" + name + '\'' +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Student student = (Student) o;
return no == student.no &&
Objects.equals(name, student.name);
}
@Override
public int hashCode() {
return Objects.hash(no, name);
}
}
HashSet Set characteristics : The bottom layer is a hash table , Disorder cannot be repeated . Duplicate elements cannot be added to the collection .
TreeSet Code
package com.bjpowernode.javase.review;
import java.util.Comparator;
import java.util.Iterator;
import java.util.TreeSet;
/*
1.1、 The creation of each collection object (new)
1.2、 Add elements to the collection
1.3、 Take an element out of a collection
1.4、 Ergodic set
1.5、 test TreeSet The elements in a collection are sortable .
1.6、 test TreeSet The type stored in the collection is custom .
1.7、 Test implementation Comparable How to interface
1.8、 Test implementation Comparator How to interface ( It's best to test the following anonymous inner classes )
*/
public class TreeSetTest {
public static void main(String[] args) {
// Set creation ( You can test the following TreeSet Store... In a collection String、Integer Of . These classes are SUN Written .)
//TreeSet<Integer> ts = new TreeSet<>();
// Writing a comparator can change the rules .
TreeSet<Integer> ts = new TreeSet<>(new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
return o2 - o1; // Automatic dismantling
}
});
// Additive elements
ts.add(1);
ts.add(100);
ts.add(10);
ts.add(10);
ts.add(10);
ts.add(10);
ts.add(0);
// Traverse ( Iterator mode )
Iterator<Integer> it = ts.iterator();
while(it.hasNext()) {
Integer i = it.next();
System.out.println(i);
}
// Traverse (foreach)
for(Integer x : ts){
System.out.println(x);
}
// TreeSet Collection to store custom types
TreeSet<A> atree = new TreeSet<>();
atree.add(new A(100));
atree.add(new A(200));
atree.add(new A(500));
atree.add(new A(300));
atree.add(new A(400));
atree.add(new A(1000));
// Traverse
for(A a : atree){
System.out.println(a);
}
//TreeSet<B> btree = new TreeSet<>(new BComparator());
// Anonymous inner class mode .
TreeSet<B> btree = new TreeSet<>(new Comparator<B>() {
@Override
public int compare(B o1, B o2) {
return o1.i - o2.i;
}
});
btree.add(new B(500));
btree.add(new B(100));
btree.add(new B(200));
btree.add(new B(600));
btree.add(new B(300));
btree.add(new B(50));
for(B b : btree){
System.out.println(b);
}
}
}
// The first way : Realization Comparable Interface
class A implements Comparable<A>{
int i;
public A(int i){
this.i = i;
}
@Override
public String toString() {
return "A{" +
"i=" + i +
'}';
}
@Override
public int compareTo(A o) {
//return this.i - o.i;
return o.i - this.i;
}
}
class B {
int i;
public B(int i){
this.i = i;
}
@Override
public String toString() {
return "B{" +
"i=" + i +
'}';
}
}
// The comparator
class BComparator implements Comparator<B> {
@Override
public int compare(B o1, B o2) {
return o1.i - o2.i;
}
}
HashMap Code
package com.bjpowernode.javase.review;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
/*
1.1、 The creation of each collection object (new)
1.2、 Add elements to the collection
1.3、 Take an element out of a collection
1.4、 Ergodic set
*/
public class HashMapTest {
public static void main(String[] args) {
// establish Map aggregate
Map<Integer, String> map = new HashMap<>();
// Additive elements
map.put(1, "zhangsan");
map.put(9, "lisi");
map.put(10, "wangwu");
map.put(2, "king");
map.put(2, "simth"); // key repeat value Will be covered .
// Get the number of elements
System.out.println(map.size());
// take key yes 2 The elements of
System.out.println(map.get(2)); // smith
// Traverse Map Assembly is important , You have to be able to .
// The first way : Get all of it first key, Traverse key When , adopt key obtain value
Set<Integer> keys = map.keySet();
for(Integer key : keys){
System.out.println(key + "=" + map.get(key));
}
// The second way : Yes, it will Map Set to Set aggregate ,Set Each element in the set is Node
// This Node Node has key and value
Set<Map.Entry<Integer,String>> nodes = map.entrySet();
for(Map.Entry<Integer,String> node : nodes){
System.out.println(node.getKey() + "=" + node.getValue());
}
}
}
Properties Code
package com.bjpowernode.javase.review;
import java.util.Properties;
public class PropertiesTest {
public static void main(String[] args) {
// Create objects
Properties pro = new Properties();
// save
pro.setProperty("username", "test");
pro.setProperty("password", "test123");
// take
String username = pro.getProperty("username");
String password = pro.getProperty("password");
System.out.println(username);
System.out.println(password);
}
}
Set inheritance structure diagram
边栏推荐
- CORBA 架构体系指南(通用对象请求代理体系架构)
- Yu Wenwen, Hu Xia and other stars take you to play with the party. Pipi app ignites your summer
- 100 important knowledge points that SQL must master: using functions to process data
- Serveur mandataire SQUID
- Go from introduction to practice -- coordination mechanism (note)
- Industry case | see the operation of bank digital transformation from the king of retail
- Go从入门到实战——所有任务完成(笔记)
- 猜拳游戏专题训练
- Null pointer exception
- VMware vSphere ESXi 7.0安装教程
猜你喜欢
MySQL performance optimization index function, hidden, prefix, hash index usage (2)
Icml2022 | scalable depth Gaussian Markov random field
CEPH distributed storage
squid代理服务器
让马化腾失望了!Web3.0,毫无希望
Null pointer exception
Go from introduction to actual combat - context and task cancellation (notes)
今晚战码先锋润和赛道第2期直播丨如何参与OpenHarmony代码贡献
Codeforces Round #717 (Div. 2)
SQL必需掌握的100个重要知识点:排序检索数据
随机推荐
Go from introduction to practice - Interface (notes)
如何将队列里面的内容没秒钟执行一次优先级
Scrum和看板的区别
Go从入门到实战——多态(笔记)
Codeforces Round #721 (Div. 2)
Go from introduction to actual combat - all tasks completed (notes)
2021全球独角兽榜发布:中国301家独角兽企业全名单来了!
Experience Navicat premium 16, unlimited reset, 14 day trial method (with source code)
于文文、胡夏等明星带你玩转派对 皮皮APP点燃你的夏日
SQL Server for循环用法
系统自带的karsonzhang/fastadmin-addons报错
Acwing周赛57-数字操作-(思维+分解质因数)
SQL必需掌握的100个重要知识点:创建计算字段
Go from introduction to practice -- coordination mechanism (note)
释放开源数据库创新力量 | 【甘肃】openGauss Meetup圆满结束
SQL必需掌握的100个重要知识点:IN 操作符
Ceph分布式存储
流程控制任务
Go从入门到实战—— 多路选择和超时控制(笔记)
Go from introduction to actual combat -- channel closing and broadcasting (notes)