Broccoli Broth

Why not have something nutritious today? All you need to do is quickly prepare a broth! Making broth is as easy as simmering water with vegetables and seasonings. It just tastes delightful and…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Data Structures

In java there is tools and collections to manipulate data for operations like sorting. The collection framework has built with data structure that do such kind of operations.

The arraylist is one of a data structure in collection framework that the elements keep in order as a list. Also, the TreeSet is keeping the elements sorted and without duplicate values. The HashMap is for store and access element as name-value pairs. The LinkedList is creating structure same as stacks or queues. The HashSet is responsible for keep the element in the collection without duplicates and can find an element quickly. Linked HashMap is similar to the HashMap and keep the element in order.

The TreeSet is keep the stings in alphabetical order automatically. But when adding element, it takes some time to place the element in the right place.

Generics are collection related code and can use in other ways. Generics means that type-safe collection. Generics revels the more errors in compile time before going to the runtime.

Generic class instances can make as following.

new ArrayList<Animal>()

also can declare variable of generic types.

List<Animal> catlist = new ArrayList<Animal>();

There are method declaration from generic types.

void foo(List<Animal> list)

x.foo(list)

there are generic class belongs to two areas as the class declaration and the method declaration.

public class ArrayList<E> extends AbstractList<E> {

public boolean add(E o)

}

The E is a generic type of representation and instead of E there can be ant type can pass. E will replace by a real type and add method expecting any type of object.

The generic methods include type parameter in method declaration. The type parameter can use for the defined class declaration in a method. The same parameter comes through the class can use for the method also. When the type parameter not defined in the class declaration still methods can use generic types.

public <T extends Animal> void takeThing(ArrayList<T> list)

public void takeThing(ArrayList<Animal> list)

both of these method declarations are legal. First method is declaring animal type ArrayList with other type like cat or dog. The second one includes only a list from animal type. The first version takes any type of animal ArrayList while second take a list of animal type.

Generics can implement as polymorphic type. If the method gets array of animals and other subtype the animal class has extended by the sub class. Then can call both types.

Then the HashSet is removing duplicate values from the set. How it happens is, HashSet use the object hash code to compare all other object hash sets. If there are no matching hash code the HashSet assume new object don’t have duplicate values. When implementing the HashSet it is a must to override the hashCode() method to prevent having same values. But there can be two objects with the same hash code might not equal. If HashSet found a matching object it calls the equals() method for one of object. After checking equality HashSet add() method returns a Boolean when new object added. If it is false, the value is a duplicated value.

The TreeSet object should has one of two characteristics. The elements in the list must be a type of implements comparable. The TreeSet add() method cannot sort the elements when they add. So, the set should implement the comparable interface. Second one is using the TreeSet’s overloaded constructor that takes a comparable. There is a choose to use the To() method to compare the elements and assume as element type implemented the comparable interface. Also, there are custom comparator to sort the elements in the set.

The Maps are the collection that act as property list. When give the name it gives back the associated value with the name. They can be a string or any Java object. There can be duplicate values but cannot have duplicate keys.

[1] Bates, Bert, and Kathy Sierra. “Head First Java Second Edition.” (2021).

Add a comment

Related posts:

What Is A Bubbler Pipe?

A bubbler pipe is a smaller handheld pipe that falls under the category of water pipes. Unlike bongs, bubbler pipes have smaller water chambers that filter smoke as it passes through. The way the…

Digital Currencies Types

Bitcoin is the main digital currency that everyone is talking about right now. But, as investors close in on the 21 million coins that exist, other currencies are gaining in popularity. Whether this…

COMING on 8th September!

Kristin Knight Pace — Finishing the 2015 Yukon Quest a 1,000 Mile Sled Dog Race and the 2016 Iditarod. In 2009, after a crippling divorce that left her heartbroken and directionless, Kristin decided…