arraylist

Java 8: merging two Lists containing objects by Id

守給你的承諾、 提交于 2020-05-27 04:13:26
问题 I have 2 Lists: // old list List<Employee> oldList = new ArrayList<>(); Employee emp1 = new Employee(); emp1.setPersonalNumber("123"); emp1.setName("old_name1"); emp1.setStatus(Status.OLD); Employee emp2 = new Employee(); emp2.setPersonalNumber("456"); emp2.setName("old_name2"); emp2.setStatus(Status.OLD); oldList.add(emp1); oldList.add(emp2); // new list List<Employee> newList = new ArrayList<>(); Employee newEmp1 = new Employee(); newEmp1.setPersonalNumber("123"); newEmp1.setName("new_name1

Best Way to Convert ArrayList to String in Kotlin

纵然是瞬间 提交于 2020-05-27 02:47:14
问题 I have an ArrayList of String in kotlin private val list = ArrayList<String>() I want to convert it into String with a separator ",". I know we can do it programatically through loop but in other languages we have mapping functions available like in java we have StringUtils.join(list); And in Swift we have array.joined(separator:","); Is there any method available to convert ArrayList to String with a separator in Kotlin? And what about for adding custom separator like "-" etc? 回答1: Kotlin

Best Way to Convert ArrayList to String in Kotlin

|▌冷眼眸甩不掉的悲伤 提交于 2020-05-27 02:46:25
问题 I have an ArrayList of String in kotlin private val list = ArrayList<String>() I want to convert it into String with a separator ",". I know we can do it programatically through loop but in other languages we have mapping functions available like in java we have StringUtils.join(list); And in Swift we have array.joined(separator:","); Is there any method available to convert ArrayList to String with a separator in Kotlin? And what about for adding custom separator like "-" etc? 回答1: Kotlin

How to do sorting in array list without using collections in Java

陌路散爱 提交于 2020-05-26 09:54:46
问题 ArrayList < Integer > arraylist = new ArrayList < Integer > (); arraylist.add(10010); arraylist.add(5); arraylist.add(4); arraylist.add(2); for (int i = 0; i < arraylist.size(); i++) { for (int j = arraylist.size() - 1; j > i; j--) { if (arraylist.get(i) > arraylist.get(j)) { int tmp = arraylist.get(i); arraylist.get(i) = arraylist.get(i); arraylist.get(j) = tmp; } } } for (int i: arraylist) { System.out.println(i); } It is giving error while swapping, The LHS should be variable. I understand

Using ArrayList or HashMap for better speed

ぐ巨炮叔叔 提交于 2020-05-24 21:21:04
问题 I need a 'List' or 'Map',... of object A. This list will be added from another ArrayList. Object A is considered to be equal to another when id parameter of A equals. My problem is I only want add an object that does not exist in my List. I wonder between the two alternatives for implementation. Using ArrayList or HashMap 1. ArrayList: for (A a: source) {if (! (a in ArrayList)) addToArrayList();} 2. HashMap <id, A> for (A a: source) {hasmap.put (a.id, a)} Which will give better speed to add a

Add alphabets to List Java [duplicate]

风格不统一 提交于 2020-05-17 06:56:08
问题 This question already has answers here : Better way to generate array of all letters in the alphabet (16 answers) Closed last month . I want create a list with alphabets with each alphabets for 5 times. I tried a code and it worked, public class AlphabetsTest { public static void main(String[] args) { List<Character> alphabetList = new ArrayList<>(); for (int i=0; i<3; i++){ char chr='a'; if (i==1) chr = 'b'; if (i==2) chr = 'c'; for (int j=0; j<5; j++){ alphabetList.add(chr); } } } } But I

Delete ArrayList from index i to i+3

限于喜欢 提交于 2020-05-17 06:25:08
问题 Sorry if this question is too easy or has been asked before, I'm new to programming, I have an ArrayList called ar declared as ArrayList ar = new ArrayList(); And I have the following code: String delnr = ""; delnr = cin.nextLine(); if(ar.contains(delnr)) { for (int i = 0; i < ar.size(); i++) { if(ar.get(i).equals(delnr)) { ar.remove(i); //Here I want to remove i+1 i+2 and i+3 as well. } } } Any logical attempt I try will give me the IndexOutOfBounds error. My ArrayList is of type [dd1, x, y,

Copying LinkedHashset content to new ArrayList?

天大地大妈咪最大 提交于 2020-05-15 11:18:47
问题 I've a listView that has some content initially. If the same content it gets, i removed the duplication through linkedhashset . Now, i want copy the linkedhashset contents i.e without duplication contents to new ArrayList . I tried to copy through p.addAll(0,lhm); // P is the instance of ArrayList and lhm is linkedHashset instance But, the ArrayList includes the duplication content too. Example : ArrayList<Price> p = new ArrayList<Price>(); p.add(new Price("Banana", 60)); p.add(new Price(

Copying LinkedHashset content to new ArrayList?

一世执手 提交于 2020-05-15 11:17:07
问题 I've a listView that has some content initially. If the same content it gets, i removed the duplication through linkedhashset . Now, i want copy the linkedhashset contents i.e without duplication contents to new ArrayList . I tried to copy through p.addAll(0,lhm); // P is the instance of ArrayList and lhm is linkedHashset instance But, the ArrayList includes the duplication content too. Example : ArrayList<Price> p = new ArrayList<Price>(); p.add(new Price("Banana", 60)); p.add(new Price(

Get attributes value from sql database as user defined object in hashmap

偶尔善良 提交于 2020-05-15 07:52:13
问题 public class Dashboard { int REQUEST_ID, PRICE, PROCESSED; String LOGIN_USER; public int getREQUEST_ID() { return REQUEST_ID; } public void setREQUEST_ID(int rEQUEST_ID) { REQUEST_ID = rEQUEST_ID; } //all getters and setters public class DBConnection { public ArrayList<Dashboard> getStoreResult() { ArrayList<Dashboard> dashRec; try{ Class.forName(""); Connection con=DriverManager.getConnection(""); Statement st=con.createStatement(); ResultSet rs=st.executeQuery(""); HashMap<Object, List