arraylist

How to find an object in an ArrayList by property

为君一笑 提交于 2019-12-27 10:26:52
问题 How can I find an object, Carnet , in a ArrayList<Carnet> knowing its property codeIsin . List<Carnet> listCarnet = carnetEJB.findAll(); public class Carnet { private String codeTitre; private String nomTitre; private String codeIsin; // Setters and getters } 回答1: You can't without an iteration. Option 1 Carnet findCarnet(String codeIsIn) { for(Carnet carnet : listCarnet) { if(carnet.getCodeIsIn().equals(codeIsIn)) { return carnet; } } return null; } Option 2 Override the equals() method of

java int与integer的区别

為{幸葍}努か 提交于 2019-12-27 10:11:19
回顾下这些知识点: Integer和int的区别 1、Integer是int提供的封装类,而int是 Java 的基本数据类型; 2、Integer默认值是null,而int默认值是0; 3、声明为Integer的变量需要实例化,而声明为int的变量不需要实例化; 4、Integer是对象,用一个引用指向这个对象,而int是基本类型,直接存储数值。 java int与integer的区别 int与integer的区别从大的方面来说就是基本数据类型与其包装类的区别: int 是基本类型,直接存数值,而integer是对象,用一个引用指向这个对象 1.Java 中的数据类型分为基本数据类型和复杂数据类型 int 是前者而integer 是后者(也就是一个类);因此在类进行初始化时 int类的变量初始为0.而Integer的变量则初始化为null . 2.初始化时:   int i =1;Integer i= new Integer(1);(要把integer 当做一个类看);但由于有了自动装箱和拆箱   ( http://www.cnblogs.com/shenliang123/archive/2012/04/16/2451996.html ), java 自动装箱与拆箱 这个是jdk1.5以后才引入的新的内容, java语言规范中说道:在许多情况下包装与解包装是由编译器自行完成的

(原)list中null的谨慎使用

一笑奈何 提交于 2019-12-27 03:04:27
今天在刷算法题时,新建了ArrayList变量,发现ArrayList与null结合起来使用时经常出错。 请查看如下几种例子, 1、new一个ArrayList<>类型的数据, 1 import java.util.ArrayList; 2 3 public class Test 4 { 5 6 @SuppressWarnings("unused") 7 public static void main(String[] args) 8 { 9 ArrayList<Integer> al=new ArrayList<Integer>(); 10 //al.add(null); 11 //al=null; 12 if (al==null) 13 { 14 System.out.println("al==null"); 15 } 16 17 if (al.isEmpty()) 18 { 19 System.out.println("al is Empty"); 20 } 21 22 23 System.out.println("al.size(): "+al.size()); 24 25 System.out.println("al.get(0): "+al.get(0)); 26 al.add(5); 27 al.add(4); 28 } 29 } 2、将ArrayList<

sorting a hashmap who's key is another hashmap

試著忘記壹切 提交于 2019-12-27 02:49:12
问题 so this is my hashmap public HashMap<Integer, HashMap<String, Integer>> girls = new HashMap<Integer, HashMap<String, **Integer**>>();; I want to sort the bolded by value. For clarification the outer hashMaps key stands for a year a girl child was born and the inner hashmap stands for a name mapped to the popularity ranking of the name. So let's say that in 2015, the name Abigail was given to 47373 babies and it was the most popular name in that year, I'd want to return the number 1 bc it's

sorting a hashmap who's key is another hashmap

喜夏-厌秋 提交于 2019-12-27 02:49:01
问题 so this is my hashmap public HashMap<Integer, HashMap<String, Integer>> girls = new HashMap<Integer, HashMap<String, **Integer**>>();; I want to sort the bolded by value. For clarification the outer hashMaps key stands for a year a girl child was born and the inner hashmap stands for a name mapped to the popularity ranking of the name. So let's say that in 2015, the name Abigail was given to 47373 babies and it was the most popular name in that year, I'd want to return the number 1 bc it's

41. 和为S的连续正数序列

◇◆丶佛笑我妖孽 提交于 2019-12-27 01:44:37
文章目录 题目描述 1. 滑窗 代码实现 复杂度分析 题目描述 小明很喜欢数学,有一天他在做数学作业时,要求计算出9~16的和,他马上就写出了正确答案是100。但是他并不满足于此,他在想究竟有多少种连续的正数序列的和为100(至少包括两个数)。没多久,他就得到另一组连续正数和为100的序列:18,19,20,21,22。现在把问题交给你,你能不能也很快的找出所有和为S的连续正数序列? Good Luck! 输出所有和为S的连续正数序列。序列内按照从小至大的顺序,序列间按照开始数字从小到大的顺序 1. 滑窗 代码实现 /** * @Classname Solution * @Description TODO * @Date 2019/12/22 13:02 * @Author SonnSei */ public class Solution { public ArrayList < ArrayList < Integer > > FindContinuousSequence ( int sum ) { ArrayList < ArrayList < Integer > > ret = new ArrayList < > ( ) ; if ( sum < 3 ) return ret ; ArrayList < Integer > temp = new ArrayList < > (

Struct复杂数据类型的UDF编写、GenericUDF编写

匆匆过客 提交于 2019-12-26 17:02:21
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> 一、背景介绍: MaxCompute 2.0版本升级后,Java UDF支持的数据类型从原来的BIGINT、STRING、DOUBLE、BOOLEAN扩展了更多基本的数据类型,同时还扩展支持了ARRAY、MAP、STRUCT等复杂类型,以及Writable参数。Java UDF使用复杂数据类型的方法,STRUCT对应com.aliyun.odps.data.Struct。com.aliyun.odps.data.Struct从反射看不出Field Name和Field Type,所以需要用@Resolve注解来辅助。即如果需要在UDF中使用STRUCT,要求在UDF Class上也标注上@Resolve注解。但是当我们Struct类型中的field有很多字段的时候,这个时候需要我们去手动的添加@Resolve注解就不是那么的友好。针对这一个问题,我们可以使用Hive 中的GenericUDF去实现。MaxCompute 2.0支持Hive风格的UDF,部分Hive UDF、UDTF可以直接在MaxCompute上使用。 二、复杂数据类型UDF示例 示例定义了一个有三个复杂数据类型的UDF,其中第一个用ARRAY作为参数,第二个用MAP作为参数,第三个用STRUCT作为参数

ArrayList和LinkedList的区别

痴心易碎 提交于 2019-12-26 11:37:26
ArrayList和LinkedList的区别(Java基础面试题) 面试官问你这个题的关键,是为了考察你的数据结构功底,理解及深入程度。 此处ArrayList和LinkedList是Java语言实现的数据结构,如果你对数组和链表有了解,那这个问题就是简易的。 进入正题,总结几点: ArrayList的实现是基于数组来实现的,LinkedList的基于双向链表来实现。这两个数据结构的逻辑关系是不一样,当然物理存储的方式也会是不一样。 对于随机访问,ArrayList优于LinkedList。 原因:ArrayList基于数组 有独特的索引机制 可以快速查找相对应的数据 对于插入和删除操作,LinkedList优于ArrayList 原因: Linked可以从任意位置插入删除,如果在ArrayList的中间插入或删除一个元素,那么这个列表中剩余的元素都会被移动;而在LinkedList的中间插入或删除一个元素的开销是固定的。 LinkedList比ArrayList更占内存 原因LinkedList的节点除了存储数据,还存储了两个引用,一个指向前一个元素,一个指向后一个元素。 我们假设现在有10000个元素放置在集合中,在这种情况下对集合做元素操作。 package test ; import java . util . ArrayList ; import java . util

How to find the max value of an Arraylist and two of its Index position using Java

*爱你&永不变心* 提交于 2019-12-26 10:15:40
问题 How can I find the maximum value from an Arraylist with its index positions? ArrayList ar = new ArrayList(); ar.add(2); // position 0 ar.add(4); // position 1 ar.add(12); // position 2 ar.add(10); // position 3 ar.add(12); // position 4 String obj = Collections.max(ar); int index = ar.indexOf(obj); System.out.println("obj max value is " + obj + " and index position is " + index); The above program just returns the output as the first max object with value 12 and index position 2 . But my

How to find the max value of an Arraylist and two of its Index position using Java

百般思念 提交于 2019-12-26 10:12:05
问题 How can I find the maximum value from an Arraylist with its index positions? ArrayList ar = new ArrayList(); ar.add(2); // position 0 ar.add(4); // position 1 ar.add(12); // position 2 ar.add(10); // position 3 ar.add(12); // position 4 String obj = Collections.max(ar); int index = ar.indexOf(obj); System.out.println("obj max value is " + obj + " and index position is " + index); The above program just returns the output as the first max object with value 12 and index position 2 . But my