{"id":27326,"date":"2018-01-20T21:21:54","date_gmt":"2018-01-20T15:51:54","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=27326"},"modified":"2018-01-20T21:21:54","modified_gmt":"2018-01-20T15:51:54","slug":"linkedlist-in-java","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/linkedlist-in-java\/","title":{"rendered":"Java Programming &#8211; LinkedList in java"},"content":{"rendered":"<p>In Java, LinkedList class implements the <a href=\"http:\/\/www.geeksforgeeks.org\/list-interface-java-examples\/\">list interface<\/a>.<\/p>\n<p>This class consists of the following methods :<\/p>\n<p>1. <strong>boolean add(Object element)<\/strong> : It appends the element to the end of the list.<\/p>\n<p>2. <strong>void add(int index, Object element)<\/strong>: It inserts the element at the position \u2018index\u2019 in the list.<\/p>\n<p>3. <strong>void addFirst(Object element)<\/strong> : It inserts the element at the beginning of the list.<\/p>\n<p>4. <strong>void addLast(Object element)<\/strong> : It appends the element at the end of the list.<\/p>\n<p>5. <strong>boolean contains(Object element)<\/strong> : It returns true if the element is present in the list.<\/p>\n<p>6. <strong>Object get(int index)<\/strong> : It returns the element at the position \u2018index\u2019 in the list. It throws \u2018IndexOutOfBoundsException\u2019 if the index is out of range of the list.<\/p>\n<p>7. <strong>int indexOf(Object element)<\/strong> : If element is found, it returns the index of the first occurrence of the element. Else, it returns -1.<\/p>\n<p>8. <strong>Object remove(int index)<\/strong> : It removes the element at the position \u2018index\u2019 in this list. It throws \u2018NoSuchElementException\u2019 if the list is empty.<\/p>\n<p>9. <strong>int size()<\/strong> : It returns the number of elements in this list.<\/p>\n<p>10. <strong>void clear()<\/strong> : It removes all of the elements from the list.<\/p>\n[ad type=\u201dbanner\u201d]\n<p><strong>Java programming<\/strong><\/p>\n[pastacode lang=\u201djava\u201d manual=\u201d%2F%2F%20Java%20code%20for%20Linked%20List%20implementation%0A%20%0Aimport%20java.util.*%3B%0A%20%0Apublic%20class%20Test%0A%7B%0A%20%20%20%20public%20static%20void%20main(String%20args%5B%5D)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%2F%2F%20Creating%20object%20of%20class%20linked%20list%0A%20%20%20%20%20%20%20%20LinkedList%3CString%3E%20object%20%3D%20new%20LinkedList%3CString%3E()%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20Adding%20elements%20to%20the%20linked%20list%0A%20%20%20%20%20%20%20%20object.add(%22A%22)%3B%0A%20%20%20%20%20%20%20%20object.add(%22B%22)%3B%0A%20%20%20%20%20%20%20%20object.addLast(%22C%22)%3B%0A%20%20%20%20%20%20%20%20object.addFirst(%22D%22)%3B%0A%20%20%20%20%20%20%20%20object.add(2%2C%20%22E%22)%3B%0A%20%20%20%20%20%20%20%20object.add(%22F%22)%3B%0A%20%20%20%20%20%20%20%20object.add(%22G%22)%3B%0A%20%20%20%20%20%20%20%20System.out.println(%22Linked%20list%20%3A%20%22%20%2B%20object)%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20Removing%20elements%20from%20the%20linked%20list%0A%20%20%20%20%20%20%20%20object.remove(%22B%22)%3B%0A%20%20%20%20%20%20%20%20object.remove(3)%3B%0A%20%20%20%20%20%20%20%20object.removeFirst()%3B%0A%20%20%20%20%20%20%20%20object.removeLast()%3B%0A%20%20%20%20%20%20%20%20System.out.println(%22Linked%20list%20after%20deletion%3A%20%22%20%2B%20object)%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20Finding%20elements%20in%20the%20linked%20list%0A%20%20%20%20%20%20%20%20boolean%20status%20%3D%20object.contains(%22E%22)%3B%0A%20%0A%20%20%20%20%20%20%20%20if(status)%0A%20%20%20%20%20%20%20%20%20%20%20%20System.out.println(%22List%20contains%20the%20element%20\u2019E\u2019%20%22)%3B%0A%20%20%20%20%20%20%20%20else%0A%20%20%20%20%20%20%20%20%20%20%20%20System.out.println(%22List%20doesn\u2019t%20contain%20the%20element%20\u2019E\u2019%22)%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20Number%20of%20elements%20in%20the%20linked%20list%0A%20%20%20%20%20%20%20%20int%20size%20%3D%20object.size()%3B%0A%20%20%20%20%20%20%20%20System.out.println(%22Size%20of%20linked%20list%20%3D%20%22%20%2B%20size)%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20Get%20and%20set%20elements%20from%20linked%20list%0A%20%20%20%20%20%20%20%20Object%20element%20%3D%20object.get(2)%3B%0A%20%20%20%20%20%20%20%20System.out.println(%22Element%20returned%20by%20get()%20%3A%20%22%20%2B%20element)%3B%0A%20%20%20%20%20%20%20%20object.set(2%2C%20%22Y%22)%3B%0A%20%20%20%20%20%20%20%20System.out.println(%22Linked%20list%20after%20change%20%3A%20%22%20%2B%20object)%3B%0A%20%20%20%20%7D%0A%7D\u201d message=\u201d\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p>Output :<\/p>\n<pre>Linked list : [D, A, E, B, C, F, G]\r\nLinked list after deletion: [A, E, F]\r\nList contains the element 'E' \r\nSize of linked list = 3\r\nElement returned by get() : F\r\nLinked list after change : [A, E, Y]<\/pre>\n[ad type=\u201dbanner\u201d]\n","protected":false},"excerpt":{"rendered":"<p>Java Programming &#8211; LinkedList in java &#8211;  Linked List &#8211;  In Java, LinkedList class implements the list interface.This class consists of the following <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1,2139,79476,79478],"tags":[80330,81423,80331,80327,81422,80332,81425,81426,80333,81424,80328,79582],"class_list":["post-27326","post","type-post","status-publish","format-standard","hentry","category-coding","category-java","category-linked-list","category-singly-linked-list","tag-doubly-linked-list-implementation-in-java","tag-java-linkedlist-vs-arraylist","tag-linked-list-data-structure-in-java","tag-linked-list-implementation-c","tag-linked-list-implementation-in-java","tag-linked-list-implementation-in-java-source-code","tag-linked-list-in-java-code","tag-linked-list-in-java-example","tag-linked-list-program-in-java","tag-linkedlist-java-8","tag-singly-linked-list-implementation-in-java","tag-singly-linked-list-in-java"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/27326","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=27326"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/27326\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=27326"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=27326"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=27326"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}