The following article provides an outline for Java Collection Methods. The Java Collections Framework has a member called collections class. The collection class is contained in a package called java.util package. Mainly, the collection class is used along with the static methods which operate on return the collections or on the collections. Each and every method of this class throws the null pointer exception whenever the object or collection passed to any of the methods is null. There are three fields in the collection class, which are Empty_Map, EMPTY_LIST, EMPTY_SET, which can be used to get an immutable set, list, and map.
ADVERTISEMENT Popular Course in this category JAVA MASTERY - Specialization | 78 Course Series | 15 Mock TestsStart Your Free Software Development Course
Web development, programming languages, Software testing & others
Given below is the list of Java Collection Methods:
Java.util.Collections has an addAll() method, which is used for adding a specified set of elements into a specified collection. Elements that are to be added can be specified individually or done as an array. This convenience method is same as the c.addAll(Arrays.asList(elements)), however the addAll() method is faster while doing most of the implementations.
Code:
import java.util.*; public class EDUCBA { public static void main(String[] course) throws Exception { try { List<String> courselist = new ArrayList<String>(); courselist.add("Data Science"); courselist.add("Data Engineering"); courselist.add("Data Analyst"); courselist.add("Data Mining"); System.out.println("\n New Course List with course name : \n" + courselist); boolean price = Collections.addAll(courselist, "22590", "23490", "34590", "54590"); System.out.println("\n Status of the courses on Website : \n" + price); System.out.println("\n New Courses with their respective prices : \n" + courselist); } catch (NullPointerException upcomingcourse) { System.out.println("Upcoming Courses are : " + upcomingcourse); } catch (IllegalArgumentException upcomingcourse) { System.out.println("Upcoming Courses are : " + upcomingcourse); } } }
Output:
The java.util.Collections class has asLifoQueue() method, which is used for returning a view of Deque as Last in first out Queue. For pushing, the method add is mapped and remove is used for pop. This method is majorly important when we require a queue in a Lifo ordering.
Code:
import java.util.*; public class EDUCBA { public static void main(String[] course) throws Exception { try { Deque<String> courselist = new ArrayDeque<String>(10); courselist.add("Data Science"); courselist.add("Data Analysis"); courselist.add("Data Engineering"); courselist.add("Data Mining"); Queue<String> newcourses = Collections.asLifoQueue(courselist); System.out.println("\n New courses added to website are : \n" + newcourses); } catch (IllegalArgumentException upcomingcourses) { System.out.println("\n Upcoming courses are : \n" + upcomingcourses); } } }
Output:
The java.util.Collections have a method java.util.Collections.binarySearch(), which is used to return a specific object’s position in a sorted list. A ClassCastException is thrown by the method when the elements of the list are incomparable by using the specified comparator or when the search key is incomparable to the elements.
Code:
import java.util.ArrayList; import java.util.Collections; import java.util.List; public class EDUCBA { public static void main(String[] course) { List<Integer> newcourseID = new ArrayList<Integer>(); newcourseID.add(112202); newcourseID.add(230042); newcourseID.add(340713); newcourseID.add(104219); newcourseID.add(628973); int IDofCourse = Collections.binarySearch(newcourseID, 340713); System.out.println(IDofCourse); IDofCourse = Collections.binarySearch(newcourseID, 628974); System.out.println(IDofCourse); } }
Output:
The java.util.Collections class has a checkedCollection() method, which is used for returning a dynamic typesafe view of a particular collection. hashCodes are not passed by the returned collection and equate the operations to the backing collection. However, it generally rely on hashCode methods and Object’s equals.
Code:
import java.util.*; public class EDUCBA { public static void main(String[] course) throws Exception { try { List<String> courselist = new ArrayList<String>(); courselist.add("Data Science"); courselist.add("Data Analysis"); courselist.add("Data Engineering"); courselist.add("Data Mining"); System.out.println("\n Best seller courses: \n" + courselist); Collection<String> bestseller = Collections .checkedCollection(courselist, String.class); System.out.println("\n List constitues of bestseller courses, as: \n" + bestseller); } catch (IllegalArgumentException upcomingcourses) { System.out.println("\n Upcoming courses are : \n" + upcomingcourses); } } }
Output:
The java.util.Collections class has a copy() method used to copy the elements of a list to another list.
Code:
import java.util.*; public class EDUCBA { public static void main(String[] course) throws Exception { try { List<String> courselist = new ArrayList<String>(10); List<String> pricelist = new ArrayList<String>(10); courselist.add("Data Science"); courselist.add("Data Analysis"); courselist.add("Data Engineering"); courselist.add("Data Mining"); pricelist.add("11900"); pricelist.add("23450"); pricelist.add("36340"); pricelist.add("44740"); System.out.println("\n Recently added courses: \n" + courselist); System.out.println("\n Price of respective courses: \n" + pricelist); System.out.println("\n Merging these above lists: \n"); Collections.copy(pricelist, courselist); System.out.println(" Recently added courses: " + courselist); System.out.println("\n Price of respective courses, will be displayed shortly: " + pricelist); } catch (IllegalArgumentException upcomingcourse) { System.out.println("\n Upcoming courses are : \n" + upcomingcourse); } catch (IndexOutOfBoundsException upcomingcourse) { System.out.println("\n Upcoming courses are : \n" + upcomingcourse); } } }
Output:
The java.util.Collections class has java.util.Collections.disjoint() method used to check if two specified collections are a disjoint or if they are not. Disjoint is a condition when two collections don’t have any elements in common.
Code:
import java.util.*; public class EDUCBA { public static void main(String[] course) { List<String> DataCourse = new ArrayList<String>(); DataCourse.add("Data Science"); DataCourse.add("Data Analysis"); DataCourse.add("Data Engineering"); DataCourse.add("Data Mining"); List<String> ColudCourse = new Vector<String>(); ColudCourse.add("AWS"); ColudCourse.add("Google Cloud"); ColudCourse.add("Azure"); ColudCourse.add("IBM Cloud"); List UpcomingCourses = new Vector(); UpcomingCourses.add(2); UpcomingCourses.add("Waiting List"); System.out.println("\n You can buy bundle of DataCouse and CloudCourse : \n " + Collections.disjoint(DataCourse, ColudCourse)); System.out.println("You can get deals on bundle of DataCouse and UpcomingCourses : \n " + Collections.disjoint(DataCourse, UpcomingCourses)); } }
Output:
On the basis of this article, we understood the concepts of Java collection methods. This article explains various collection methods with examples. All of the methods mentioned in the article are thoroughly explained with their definitions and usage.
Das obige ist der detaillierte Inhalt vonJava-Erfassungsmethoden. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!