Browsed by
Tag: arraylist

5 Ways to Remove Null from ArrayList

5 Ways to Remove Null from ArrayList

5 Ways to Remove Null from ArrayList: Removing null from arraylist become some realtime business scenario many times. Removing null from arraylist is actually easy as it can be. We have many ways to remove null from arraylist.   5 Ways to Remove Null From ArrayList: Using collections.singleton Using removeAll with one null value list Using Iterator Using while loop (rarely used) Using removeIf (rarely used)   Below program is shared with all the above 5 ways to remove null from arraylist….

Read More Read More

1,009 total views, 1 views today

All Date Type Conversions in Java with Examples

All Date Type Conversions in Java with Examples

All Date Type Conversions in Java with Examples: This article “All Date Type Conversions in Java with Examples” we are writing to help people to quickly find the data type conversions in java with examples. Few of the below conversions are many times asked in interviews also. So bookmark and read often to store it in your mind.   Array to ArrayList convert in Java Example:   Here we are just iterating the states array and adding it to the…

Read More Read More

1,380 total views, 2 views today

Arraylist vs LinkedList Java

Arraylist vs LinkedList Java

ArrayList Vs LinkedList: ArrayList: 1. Search operation is faster, because it maintains the array structure, so get(index) will search and give the value fastly. 2. Less memory consumption because it maintains element and data. Where in linkedlist it maintains two pointer to store the addresses of the neighbour elements. LinkedList: 1. Remove operation is faster. 2. Insertion operation is faster. Because it is doubly linked list, so maintains both the address, where as in arraylist it need to shift the…

Read More Read More

857 total views, no views today

Finding Common values in Arraylist Java Example

Finding Common values in Arraylist Java Example

Finding Common values in Arraylist: Two arraylists are iterated one inside another for loop and common values are inserted in the common arraylist. Input: List 1 contains: [plain gutter=”false”]God .Net PHP Java [/plain] List 2 contains: [plain gutter=”false”]C Java C++ .Net [/plain] Expected Values: [plain gutter=”false”] Common values are :::Java Common values are :::.Net [/plain] Java Program to Find Common values in Arraylist: import java.util.ArrayList; import java.util.List; public class FindCommonValue { public static void main(String[] args) { List<String> myList1 =…

Read More Read More

8,299 total views, no views today

Taking only String values from ArrayList

Taking only String values from ArrayList

Scenario: You are given with a arraylist with all datatype values. Eg: Integer and String values. But you want to move only string values to another arraylist. Solution you can think suddenly: ArrayList can be declared to hold only string and using addAll method we can move. Eg: ArrayList arStr = new ArrayList(); arStr.addAll(“given_arrayList”); But the above one does not throw any error as well which will move all the values(String and Integer) to another list. If you try to…

Read More Read More

737 total views, no views today

ArrayList Modification Java Example

ArrayList Modification Java Example

ArrayList Modification: package in.javadomain; import java.util.ArrayList; import java.util.ListIterator; public class ReverseIteration { public static void main(String[] args) { ArrayList<String> list = new ArrayList<String>(); list.add("Sachin"); list.add("Gambhir"); list.add("Dhoni"); list.add("Sehwag"); list.add("Raina"); list.add("Yuvraj"); ListIterator<String> li = list.listIterator(); System.out.println("ArrayList Values: \n"); for (String val : list) { System.out.println(val); } System.out.println("\nArrayList Values After Modification: \n"); list.set(1, "Dhawan"); list.set(3, "R.Sharma"); for (String val : list) { System.out.println(val); } } } Output: [plain gutter=”false”] ArrayList Values: Sachin Gambhir Dhoni Sehwag Raina Yuvraj ArrayList Values After Modification: Sachin Dhawan…

Read More Read More

772 total views, no views today

ArrayList Reverse Iteration Example

ArrayList Reverse Iteration Example

ArrayList Iteration: package in.javadomain; import java.util.ArrayList; import java.util.ListIterator; public class ReverseIteration { public static void main(String[] args) { ArrayList<String> list = new ArrayList<String>(); list.add("Sachin"); list.add("Gambhir"); list.add("Dhone"); list.add("Sehwag"); list.add("Raina"); list.add("Yuvraj"); ListIterator<String> li = list.listIterator(); System.out.println("ArrayList Iteration: \n"); while (li.hasNext()) { System.out.println(li.next()); } } } Output: [plain gutter=”false”] ArrayList Iteration: Sachin Gambhir Dhone Sehwag Raina Yuvraj [/plain] ArrayList Reverse Iteration: package in.javadomain; import java.util.ArrayList; import java.util.ListIterator; public class ReverseIteration { public static void main(String[] args) { ArrayList<String> list = new ArrayList<String>(); list.add("Sachin");…

Read More Read More

766 total views, no views today

Converting HashMap to ArrayList in Java Example

Converting HashMap to ArrayList in Java Example

Method 1: You can convert HashMap to ArrayList by simply iterating hashmap keys or values and you can add to arraylist. HashMap to ArrayList Java Program: package in.javadomain; import java.util.ArrayList; import java.util.HashMap; public class HashMapTOArrayList { public static void main(String[] args) { HashMap<Integer, String> hashMap = new HashMap<Integer, String>(); hashMap.put(1, "Google"); hashMap.put(2, "Facebook"); hashMap.put(3, "Youtube"); for(Object hashMapValue : hashMap.entrySet()){ System.out.println("Printing From HashMap: "+hashMapValue); } ArrayList Keys = new ArrayList(); for (Object key : hashMap.keySet()) { Keys.add(key.toString()); } for(Object arrayListValue :…

Read More Read More

2,276 total views, no views today

Converting Array to Arraylist in Java Example

Converting Array to Arraylist in Java Example

Array to ArrayList: package in.javadomain; import java.util.ArrayList; import java.util.Collections; import java.util.List; public class ArrayTOArrayList { public static void main(String[] args) { String[] languagesArray = { "Tamil", "English", "Hindi", "Telugu" }; List<String> langArrayList = new ArrayList<String>(); Collections.addAll(langArrayList, languagesArray); for (Object value : langArrayList) { System.out.println("Value in ArrayList :::" + value); } } } Output: [plain] Value in ArrayList :::Tamil Value in ArrayList :::English Value in ArrayList :::Hindi Value in ArrayList :::Telugu [/plain] Moving Array values to ArrayList: Collections.addAll(langArrayList, languagesArray); 833 total views,…

Read More Read More

833 total views, no views today

ArrayList to Array in Java Example

ArrayList to Array in Java Example

ArrayList to Array: The below program converts ArrayList to Array. Java Program: package in.javadomain; import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class ArrayListTOArray { public static void main(String[] args) { List<String> mobilesList = new ArrayList<String>(); mobilesList.add("Nokia"); mobilesList.add("Karbonn"); mobilesList.add("Micromax"); mobilesList.add("Samsung"); Object[] listToObj = mobilesList.toArray(); String[] mobilesArray = Arrays.copyOf(listToObj, listToObj.length, String[].class); System.out.println("Value in Array is ::: " + mobilesArray[2]); } } Output: [plain] Value in Array is ::: Micromax [/plain] Converting Object Array to String Array: Object[] listToObj = mobilesList.toArray(); String[] mobilesArray…

Read More Read More

773 total views, 2 views today