首页 > 编程语言 >java集合

java集合

时间:2024-08-09 19:52:25浏览次数:16  
标签:java System util add 集合 import public

package com.jihe;

import java.util.ArrayList;

public class ArrayListDemo1 {

    public static void main(String[] args) {
        //创建一个ArrayList实例
        ArrayList<String> fruits = new ArrayList<>();

        //添加元素
        fruits.add("Apple");
        fruits.add("Banana");
        fruits.add("Cherry");

        //打印列表大小
        System.out.println("List size:"+fruits.size());

        //检查列表是否包含某个元素
        if(fruits.contains("Banana")){
            System.out.println("Banana is in the list.");
        }

        ///获取并打印指定索引处的元素
        String secondFruit = fruits.get(1);
        System.out.println("Second fruit is:"+secondFruit);

        //在指定位置插入一个元素
        fruits.add(1,"Berry");
        System.out.println("List after insertion:" + fruits);

        //替换指定位置的元素
        fruits.set(1,"Blackberry");
        System.out.println("List after replacement: "+fruits);


        //删除指定位置的元素
        fruits.remove(1);
        System.out.println("List after deletion: "+fruits);

        //清空列表
        fruits.clear();
        System.out.println("List is empty now? "+fruits.isEmpty());

    }
}

package com.jihe;

public class CircularArrayQueue<E> implements Queue<E>{

    private int head;
    private int tail;
    CircularArrayQueue(int capacity){}
    @Override
    public void add(E element) {

    }

    @Override
    public E remove() {
        return null;
    }

    @Override
    public int size() {
        return 0;
    }
    private E[] elements;
}

package com.jihe;

public interface Collection <E>{
    boolean add(E element);
    Iterable<E> iterator();
}

package com.jihe;

import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class CollectionSortDemo1 {

    public static void main(String[] args) {
        //定义数组
        Integer[] array = {50,100,30,120,10,20};
        //2.对数组元素进行排序
        //基于Collections工具类对数组或集合进行排序
        List<Integer> list = Arrays.asList(array);//将数组转换为list集合
        Collections.sort(list);
        System.out.println(list);
    }
}

package com.jihe;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class CollectionSortDemo02 {

    public static void main(String[] args) {
        Rect p1 = new Rect(10,20);
        Rect p2 = new Rect(5,30);
        Rect p3 = new Rect(8,30);
        List list = new ArrayList<Rect>();
        list.add(p1);
        list.add(p2);
        list.add(p3);
        //调用Collections集合的sort方法对集合元素进行排序
        Collections.sort(list);
        System.out.println(list);
    }
}

package com.jihe;

import java.awt.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

public class CollectionSortDemo3 {
    public static void main(String[] args) {
        Point p1 = new Point(10,20);
        Point p2 = new Point(5,30);
        List<Point> list = new ArrayList<>();

        list.add(p1);
        list.add(p2);
        //调用
        Comparator<Point> comparator1 = new Comparator<Point>() {
            @Override
            public int compare(Point o1, Point o2) {
                return o1.getX() - o2.getX();
            }
        };
        Comparator<Point> comparator2 = new Comparator<Point>() {
            @Override
            public int compare(Point o1, Point o2) {
                return o1.getY() - o2.getY();
            }
        };

        Collections.sort(list,comparator1);
        System.out.println(list);
        Collections.sort(list,comparator2);
        System.out.println(list);

    }
}

package com.jihe;

import javafx.beans.binding.ObjectExpression;

import java.util.Comparator;
import java.util.Objects;

public class Item implements Comparator<Item> {
    private String description;
    private int partNumber;

    public Item(String aDescription,int aPartNumber){
        description = aDescription;
        partNumber = aPartNumber;
    }

    public String getDescription()
    {
        return description;
    }

    public String toString(){
        return "[description="+description+", partNumber="+partNumber+"]";
    }


    @Override
    public int compare(Item o1, Item o2) {
        return 0;
    }

    public boolean equals(Object otherObject)
    {
        if(this == otherObject)return true;
        if(otherObject == null)return false;
        if(getClass() != otherObject.getClass())return false;
        Item other = (Item) otherObject;
        return Objects.equals(description,other.description) && partNumber == other.partNumber;
    }

    public int hashCode(){
        return Objects.hash(description,partNumber);
    }

    public  int compare(Item other)
    {
        int diff = Integer.compare(partNumber,other.partNumber);
        return diff != 0 ? diff : description.compareTo(other.description);
    }


}

package com.jihe;

import java.util.function.Consumer;

public interface Iterator <E>{
    E next();
    boolean hasNext();
    void remove();

    default void forEachRemaining(Consumer<? super E> action) {

    }
}

package com.jihe;

import sun.plugin.javascript.navig4.Link;

public class LinkedListQueue<E> implements Queue<E>{
    private Link head;
    private Link tail;

    LinkedListQueue(){}
    @Override
    public void add(E element) {

    }

    @Override
    public E remove() {
        return null;
    }

    @Override
    public int size() {
        return 0;
    }
}

package com.jihe;

import java.util.*;

public class LinkedListTest {

    public static void main(String[] args) {
        LinkedList<String> a = new LinkedList<String>();
            a.add("Amy");
            a.add("Carl");
            a.add("Erica");

                LinkedList<String> b = new LinkedList<String>();
                b.add("Bob");
                b.add("Doug");
                b.add("Frances");
                b.add("Gloria");


        ListIterator<String> aIter = a.listIterator();
        java.util.Iterator<String> bIter = b.iterator();

        while (bIter.hasNext())
        {
            if(aIter.hasNext())aIter.next();
            aIter.add(bIter.next());
        }

        System.out.println(a);

        //remove every second word from b

        bIter = b.iterator();
        while(bIter.hasNext())
        {
            bIter.next();//skip one element
            if(bIter.hasNext())
            {
                bIter.next();//skip next element
                bIter.remove();//remove that
            }
        }
        System.out.println(b);

        a.removeAll(b);
        System.out.println(a);
    }
}

package com.jihe;

import java.util.HashMap;

public class MapTest {
    public static void main(String[] args) {
//        HashMap staff = new HashMap<String,Employee>();
    }

}

package com.jihe;

import java.time.LocalDate;
import java.util.PriorityQueue;

public class PriorityQueueTest {

    public static void main(String[] args) {
        PriorityQueue pq = new PriorityQueue<LocalDate>();
        pq.add(LocalDate.of(1906,12,9));
        pq.add(LocalDate.of(1815,12,10));
        pq.add(LocalDate.of(1903,12,3));
        pq.add(LocalDate.of(1910,6,22));

        System.out.println("Iterating over elements...");
        for(Object date : pq){
            System.out.println(date);
        }

        System.out.println("Removing elements ...");
        while(!pq.isEmpty())
            System.out.println(pq.remove());
    }
}

package com.jihe;

public interface Queue <E>{
    void add(E element);
    E remove();
    int size();
}

package com.jihe;


import java.util.Comparator;

public class Rect implements Comparator<Rect> {
    private Integer width;
    private Integer height;
    public Rect(){

    }
    public Rect(int width,int height){
        this.width = width;
        this.height = height;
    }

    public  int compareTo(Rect o){
        return this.width - o.width;
    }

    @Override
    public int compare(Rect o1, Rect o2) {
        return 0;
    }
}

package com.jihe;

import java.util.HashSet;
import java.util.Scanner;

public class SetTest {
    public static void main(String[] args) {
        HashSet words = new HashSet<String>();
        long totalTime = 0;

        try(Scanner in = new Scanner(System.in))
        {
            while(in.hasNext())
            {
                String word = in.next();
                long callTime = System.currentTimeMillis();
                words.add(word);
                callTime = System.currentTimeMillis() - callTime;
                totalTime += callTime;
            }
        }
        java.util.Iterator<String> iter = words.iterator();
        for(int i = 1;i<=20&& iter.hasNext();i++)
            System.out.println(iter.next());
        System.out.println(". . .");
        System.out.println(words.size() + " distinct words. "+totalTime+" milliseconds");
    }
}

package com.jihe;

import java.util.Comparator;
import java.util.TreeSet;

//class Item{
//    private String name;
//    private Integer id;
//    public Item(String name,Integer id){
//        this.name = name;
//        this.id = id;
//    }
//}
public class TreeSetTest {
    public static void main(String[] args) {
        TreeSet parts = new TreeSet<Item>();
        parts.add(new Item("Toaster",1234));
        parts.add(new Item("Widget",4562));
        parts.add(new Item("Modem",9912));
        System.out.println(parts);

        TreeSet sortByDescription = new TreeSet(Comparator.comparing(Item::getDescription));

        sortByDescription.addAll(parts);
        System.out.println(sortByDescription);
    }
}

标签:java,System,util,add,集合,import,public
From: https://blog.csdn.net/m0_68935893/article/details/141056702

相关文章

  • Java毕业设计-基于ssm框架开发的车辆管理系统-毕业论文(附毕设源代码)
    文章目录前言一、毕设成果演示(源代码在文末)二、毕设摘要展示1、开发说明2、需求/流程分析3、系统功能结构三、系统实现展示1、个人中心2、员工管理3、用户管理4、基础数据管理5、车辆管理6、保养登记管理7、事故登记管理8、维修登记管理9、违章登记管理四、毕设内容和......
  • Java 入门练习:21 - 25
    JAVA21补全构造方法描述有父类Base,内部定义了x、y属性。有子类Sub,继承自父类Base。子类新增了一个z属性,并且定义了calculate方法,在此方法内计算了父类和子类中x、y、z属性三者的乘积。请补全子类构造方法的初始化逻辑,使得该计算逻辑能够正确执行。输入描述......
  • Java 入门练习:26 - 30
    JAVA26实现接口描述已知接口Comparator,内部定义了max函数,用于返回两个整数中的最大值。请定义该接口的实现类,使得main方法中的比较逻辑可以正确执行,要求实现类的名称为ComparatorImpl。输入描述两个整数输出描述两个整数中的最大值示例1输入:13输出:3解......
  • Java 入门练习:16 - 20
    JAVA16数组遍历描述将用户输入的六个数字填入数组并找出数组中最大值和最小值输入描述随机输入的6个整数输出描述输出数组中的最大值,最小值(最大值最小值之间用空格隔开。若有多个最大值或最小值,输出一次即可,如样例2所示)示例1输入:135246输出:61示例2......
  • Java中数组算法的学习
    数组的算法目录数组的算法概述冒泡排序选择排序排序算法库StreamAPI概述简单排序:冒泡排序、选择排序、插入排序高级排序:快速排序、归并排序、希尔排序相关算法知识:划分、递归、二分查找冒泡排序原理:从第一个数据开始,与第二个数据相比较,如果第二个数据小于第一个数据,......
  • Java基础——注解1——注解的定义与使用
    ......
  • java6
    我学习了常见API知识java.lang.Math提供数学常量和方法,如基本的数学运算(加、减、乘、除)、三角函数、对数、幂运算等。常用方法:Math.abs(x):返回x的绝对值。Math.max(a,b):返回a和b中的较大值。Math.min(a,b):返回a和b中的较小值。Math.sqrt(x):返回x的平方根。Math.pow(a,b......
  • Android开发基础07-掌握Java语言
    Android开发广泛使用Java作为编程语言,熟练掌握Java语言是十分必要的。1.基础入门知识a.设置开发环境安装JDK(JavaDevelopmentKit):JDK是进行Java开发的必备工具,务必下载安装并配置相应的环境变量。安装IDE(IntegratedDevelopmentEnvironment):推荐使用IntelliJIDEA、E......
  • 今日Java练习:选择题挑战
    题目选自牛客网1.假设num已经被创建为一个ArrayList对象,并且最初包含以下整数值:[0,0,4,2,5,0,3,0]。执行下面的方法numQuest(),数组num会变成?privateList<Integer>nums;//precondition:nums.size()>0//numscontainsIntegerobjectspublicvoidnumQuest(){int......
  • Java中的8种基本数据类型及其存储方式
    文章目录基本数据类型存储方式整型数据浮点型数据char类型数据布尔类型数据其他数据类型的转换自动转换强制转换基本数据类型Java属于C类语言,有8种数据类型数据类型byteshortintlongfloatdoublecharboolean数据大小8bit16bit32bit64bit32bit64bit8bit/24bit/32bit......